1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 23:44:40 +00:00

random fixes

This commit is contained in:
mat 2023-12-01 20:48:45 -06:00
commit 049b604498
5 changed files with 130 additions and 2 deletions

View file

@ -217,6 +217,27 @@ impl From<&str> for Mutf8String {
} }
} }
impl Default for &Mutf8Str {
#[inline]
fn default() -> Self {
Mutf8Str::from_slice(&[])
}
}
impl From<&Mutf8Str> for Mutf8String {
#[inline]
fn from(s: &Mutf8Str) -> Self {
s.to_owned()
}
}
impl From<&Mutf8Str> for String {
#[inline]
fn from(s: &Mutf8Str) -> Self {
s.to_str().into_owned()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::borrow::Cow; use std::borrow::Cow;

View file

@ -5,7 +5,7 @@ use byteorder::ReadBytesExt;
use crate::{ use crate::{
common::{read_string, unchecked_push, unchecked_write_string, END_ID, MAX_DEPTH}, common::{read_string, unchecked_push, unchecked_write_string, END_ID, MAX_DEPTH},
mutf8::Mutf8String, mutf8::Mutf8String,
Error, Mutf8Str, Error, Mutf8Str, ToNbtTag,
}; };
use super::{list::NbtList, NbtTag}; use super::{list::NbtList, NbtTag};
@ -211,10 +211,21 @@ impl NbtCompound {
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.values.clear(); self.values.clear();
} }
pub fn insert(&mut self, name: impl Into<Mutf8String>, tag: NbtTag) { pub fn insert(&mut self, name: impl Into<Mutf8String>, tag: impl ToNbtTag) {
let name = name.into(); let name = name.into();
let tag = tag.to_nbt_tag();
self.values.push((name, tag)); self.values.push((name, tag));
} }
pub fn extend(
&mut self,
other: impl IntoIterator<Item = (impl Into<Mutf8String>, impl ToNbtTag)>,
) {
self.values.extend(
other
.into_iter()
.map(|(name, tag)| (name.into(), tag.to_nbt_tag())),
);
}
pub fn remove(&mut self, name: &str) -> Option<NbtTag> { pub fn remove(&mut self, name: &str) -> Option<NbtTag> {
let name = Mutf8Str::from_str(name); let name = Mutf8Str::from_str(name);
let name = name.as_ref(); let name = name.as_ref();

View file

@ -351,3 +351,69 @@ impl NbtList {
} }
} }
} }
impl From<Vec<i8>> for NbtList {
fn from(bytes: Vec<i8>) -> Self {
NbtList::Byte(bytes)
}
}
impl From<Vec<i16>> for NbtList {
fn from(shorts: Vec<i16>) -> Self {
NbtList::Short(shorts)
}
}
impl From<Vec<i32>> for NbtList {
fn from(ints: Vec<i32>) -> Self {
NbtList::Int(ints)
}
}
impl From<Vec<i64>> for NbtList {
fn from(longs: Vec<i64>) -> Self {
NbtList::Long(longs)
}
}
impl From<Vec<f32>> for NbtList {
fn from(floats: Vec<f32>) -> Self {
NbtList::Float(floats)
}
}
impl From<Vec<f64>> for NbtList {
fn from(doubles: Vec<f64>) -> Self {
NbtList::Double(doubles)
}
}
impl From<Vec<Vec<u8>>> for NbtList {
fn from(byte_arrays: Vec<Vec<u8>>) -> Self {
NbtList::ByteArray(byte_arrays)
}
}
impl From<Vec<Mutf8String>> for NbtList {
fn from(strings: Vec<Mutf8String>) -> Self {
NbtList::String(strings)
}
}
impl From<Vec<String>> for NbtList {
fn from(strings: Vec<String>) -> Self {
NbtList::String(strings.into_iter().map(Mutf8String::from).collect())
}
}
impl From<Vec<NbtList>> for NbtList {
fn from(lists: Vec<NbtList>) -> Self {
NbtList::List(lists)
}
}
impl From<Vec<NbtCompound>> for NbtList {
fn from(compounds: Vec<NbtCompound>) -> Self {
NbtList::Compound(compounds)
}
}
impl From<Vec<Vec<i32>>> for NbtList {
fn from(int_arrays: Vec<Vec<i32>>) -> Self {
NbtList::IntArray(int_arrays)
}
}
impl From<Vec<Vec<i64>>> for NbtList {
fn from(long_arrays: Vec<Vec<i64>>) -> Self {
NbtList::LongArray(long_arrays)
}
}

View file

@ -558,6 +558,24 @@ impl NbtTag {
} }
} }
impl From<NbtCompound> for BaseNbt {
fn from(tag: NbtCompound) -> Self {
Self {
name: Mutf8String::from(""),
tag,
}
}
}
impl From<Nbt> for NbtTag {
fn from(value: Nbt) -> Self {
match value {
Nbt::Some(nbt) => NbtTag::Compound(nbt.tag),
Nbt::None => NbtTag::Compound(NbtCompound::new()),
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::io::Read; use std::io::Read;

View file

@ -181,6 +181,12 @@ impl ToNbtTag for String {
} }
} }
impl ToNbtTag for &str {
fn to_nbt_tag(self) -> crate::owned::NbtTag {
crate::owned::NbtTag::String(self.into())
}
}
// unsigned integers // unsigned integers
impl FromNbtTag for u8 { impl FromNbtTag for u8 {
fn from_nbt_tag(tag: &crate::borrow::NbtTag) -> Option<Self> { fn from_nbt_tag(tag: &crate::borrow::NbtTag) -> Option<Self> {
@ -324,3 +330,9 @@ impl ToNbtTag for bool {
crate::owned::NbtTag::Byte(if self { 1 } else { 0 }) crate::owned::NbtTag::Byte(if self { 1 } else { 0 })
} }
} }
impl ToNbtTag for crate::owned::NbtList {
fn to_nbt_tag(self) -> crate::owned::NbtTag {
crate::owned::NbtTag::List(self)
}
}