add From<Vec<bool/i32/i64/NbtItem>> for NbtList

This commit is contained in:
shenjack 2023-08-05 01:38:19 +08:00
parent b5dda9657a
commit 18d1f3f566
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 32 additions and 6 deletions

View File

@ -71,6 +71,31 @@ impl NbtItem {
}
}
impl From<Vec<NbtItem>> for NbtList {
#[inline]
fn from(value: Vec<NbtItem>) -> Self { Self::List(Rc::new(RefCell::new(value))) }
}
impl From<HashMap<Arc<str>, NbtItem>> for NbtList {
#[inline]
fn from(value: HashMap<Arc<str>, NbtItem>) -> Self { Self::Compound(Rc::new(RefCell::new(value))) }
}
impl From<Vec<bool>> for NbtList {
#[inline]
fn from(value: Vec<bool>) -> Self { Self::BoolArray(Rc::new(RefCell::new(value))) }
}
impl From<Vec<i32>> for NbtList {
#[inline]
fn from(value: Vec<i32>) -> Self { Self::IntArray(Rc::new(RefCell::new(value))) }
}
impl From<Vec<i64>> for NbtList {
#[inline]
fn from(value: Vec<i64>) -> Self { Self::LongArray(Rc::new(RefCell::new(value))) }
}
macro_rules! export_data {
($name:ident, $nbt_name:ident, $type:ty) => {
#[inline]

View File

@ -108,8 +108,7 @@ pub mod read {
[0x07] => {
// ByteArray
for _ in 0..len {
let arr = Rc::new(RefCell::new(from_bool_array(value)));
vec.push(NbtItem::Array(NbtList::BoolArray(arr)));
vec.push(NbtItem::Array(NbtList::from(from_bool_array(value))));
}
}
[0x08] => {
@ -121,6 +120,10 @@ pub mod read {
[0x09] => {
// NbtList
// 要命 (虽说没 Compound 那么麻烦)
// 直接递归就行
for _ in 0..len {
vec.push(NbtItem::Array(NbtList::from(read_nbt_list(value))));
}
}
[0x0A] => {
// Compound
@ -129,15 +132,13 @@ pub mod read {
[0x0B] => {
// IntArray
for _ in 0..len {
let arr = Rc::new(RefCell::new(from_i32_array(value)));
vec.push(NbtItem::Array(NbtList::IntArray(arr)));
vec.push(NbtItem::Array(NbtList::from(from_i32_array(value))));
}
}
[0x0C] => {
// LongArray
for _ in 0..len {
let arr = Rc::new(RefCell::new(from_i64_array(value)));
vec.push(NbtItem::Array(NbtList::LongArray(arr)));
vec.push(NbtItem::Array(NbtList::from(from_i64_array(value))));
}
}
_ => {