From 049b60449833b4ea5e54dd5b937b801c9499192c Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 1 Dec 2023 20:48:45 -0600 Subject: [PATCH] random fixes --- simdnbt/src/mutf8.rs | 21 +++++++++++ simdnbt/src/owned/compound.rs | 15 ++++++-- simdnbt/src/owned/list.rs | 66 +++++++++++++++++++++++++++++++++++ simdnbt/src/owned/mod.rs | 18 ++++++++++ simdnbt/src/traits.rs | 12 +++++++ 5 files changed, 130 insertions(+), 2 deletions(-) diff --git a/simdnbt/src/mutf8.rs b/simdnbt/src/mutf8.rs index b35993e..54835b7 100644 --- a/simdnbt/src/mutf8.rs +++ b/simdnbt/src/mutf8.rs @@ -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)] mod tests { use std::borrow::Cow; diff --git a/simdnbt/src/owned/compound.rs b/simdnbt/src/owned/compound.rs index 36ff795..39337b1 100644 --- a/simdnbt/src/owned/compound.rs +++ b/simdnbt/src/owned/compound.rs @@ -5,7 +5,7 @@ use byteorder::ReadBytesExt; use crate::{ common::{read_string, unchecked_push, unchecked_write_string, END_ID, MAX_DEPTH}, mutf8::Mutf8String, - Error, Mutf8Str, + Error, Mutf8Str, ToNbtTag, }; use super::{list::NbtList, NbtTag}; @@ -211,10 +211,21 @@ impl NbtCompound { pub fn clear(&mut self) { self.values.clear(); } - pub fn insert(&mut self, name: impl Into, tag: NbtTag) { + pub fn insert(&mut self, name: impl Into, tag: impl ToNbtTag) { let name = name.into(); + let tag = tag.to_nbt_tag(); self.values.push((name, tag)); } + pub fn extend( + &mut self, + other: impl IntoIterator, 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 { let name = Mutf8Str::from_str(name); let name = name.as_ref(); diff --git a/simdnbt/src/owned/list.rs b/simdnbt/src/owned/list.rs index a71cfc4..4bdf3a7 100644 --- a/simdnbt/src/owned/list.rs +++ b/simdnbt/src/owned/list.rs @@ -351,3 +351,69 @@ impl NbtList { } } } + +impl From> for NbtList { + fn from(bytes: Vec) -> Self { + NbtList::Byte(bytes) + } +} +impl From> for NbtList { + fn from(shorts: Vec) -> Self { + NbtList::Short(shorts) + } +} +impl From> for NbtList { + fn from(ints: Vec) -> Self { + NbtList::Int(ints) + } +} +impl From> for NbtList { + fn from(longs: Vec) -> Self { + NbtList::Long(longs) + } +} +impl From> for NbtList { + fn from(floats: Vec) -> Self { + NbtList::Float(floats) + } +} +impl From> for NbtList { + fn from(doubles: Vec) -> Self { + NbtList::Double(doubles) + } +} +impl From>> for NbtList { + fn from(byte_arrays: Vec>) -> Self { + NbtList::ByteArray(byte_arrays) + } +} +impl From> for NbtList { + fn from(strings: Vec) -> Self { + NbtList::String(strings) + } +} +impl From> for NbtList { + fn from(strings: Vec) -> Self { + NbtList::String(strings.into_iter().map(Mutf8String::from).collect()) + } +} +impl From> for NbtList { + fn from(lists: Vec) -> Self { + NbtList::List(lists) + } +} +impl From> for NbtList { + fn from(compounds: Vec) -> Self { + NbtList::Compound(compounds) + } +} +impl From>> for NbtList { + fn from(int_arrays: Vec>) -> Self { + NbtList::IntArray(int_arrays) + } +} +impl From>> for NbtList { + fn from(long_arrays: Vec>) -> Self { + NbtList::LongArray(long_arrays) + } +} diff --git a/simdnbt/src/owned/mod.rs b/simdnbt/src/owned/mod.rs index b39d6f7..1e77326 100644 --- a/simdnbt/src/owned/mod.rs +++ b/simdnbt/src/owned/mod.rs @@ -558,6 +558,24 @@ impl NbtTag { } } +impl From for BaseNbt { + fn from(tag: NbtCompound) -> Self { + Self { + name: Mutf8String::from(""), + tag, + } + } +} + +impl From for NbtTag { + fn from(value: Nbt) -> Self { + match value { + Nbt::Some(nbt) => NbtTag::Compound(nbt.tag), + Nbt::None => NbtTag::Compound(NbtCompound::new()), + } + } +} + #[cfg(test)] mod tests { use std::io::Read; diff --git a/simdnbt/src/traits.rs b/simdnbt/src/traits.rs index 606bc93..ef22fe7 100644 --- a/simdnbt/src/traits.rs +++ b/simdnbt/src/traits.rs @@ -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 impl FromNbtTag for u8 { fn from_nbt_tag(tag: &crate::borrow::NbtTag) -> Option { @@ -324,3 +330,9 @@ impl ToNbtTag for bool { 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) + } +}