1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 07:26:04 +00:00

clippy and uncomment disabled examples

This commit is contained in:
mat 2024-06-16 00:49:46 +00:00
parent d420d3fb37
commit 62cf6c721c
4 changed files with 137 additions and 143 deletions

View file

@ -1,113 +1,110 @@
// use std::{collections::HashMap, hint::black_box, io::Cursor};
use std::{collections::HashMap, hint::black_box, io::Cursor};
// use simdnbt::borrow::BaseNbt;
use simdnbt::borrow::BaseNbt;
// #[derive(Clone, PartialEq, Debug)]
// pub struct Item {
// pub id: i16,
// pub damage: i16,
// pub count: i8,
#[derive(Clone, PartialEq, Debug)]
pub struct Item {
pub id: i16,
pub damage: i16,
pub count: i8,
// pub head_texture_id: Option<String>,
pub head_texture_id: Option<String>,
// pub skyblock_id: Option<String>,
// pub reforge: Option<String>,
pub skyblock_id: Option<String>,
pub reforge: Option<String>,
// pub display: ItemDisplay,
pub display: ItemDisplay,
// pub enchantments: HashMap<String, i32>,
// pub timestamp: Option<String>,
// }
pub enchantments: HashMap<String, i32>,
pub timestamp: Option<String>,
}
// #[derive(Clone, PartialEq, Debug)]
// pub struct ItemDisplay {
// pub name: String,
// pub lore: Vec<String>,
#[derive(Clone, PartialEq, Debug)]
pub struct ItemDisplay {
pub name: String,
pub lore: Vec<String>,
// pub has_glint: bool,
pub has_glint: bool,
// pub color: Option<i32>,
// }
pub color: Option<i32>,
}
// fn items_from_nbt(nbt: BaseNbt) -> Option<Vec<Option<Item>>> {
// let mut items = Vec::new();
// for item_nbt in nbt
// .compound()
// .list("i")
// .and_then(|list| list.compounds())
// .unwrap_or_default()
// {
// // check if "id" is present, if not, skip
// if !item_nbt.contains("id") {
// // this just means the item isn't present
// items.push(None);
// continue;
// }
fn items_from_nbt(nbt: BaseNbt) -> Option<Vec<Option<Item>>> {
let mut items = Vec::new();
for item_nbt in nbt
.list("i")
.and_then(|list| list.compounds())
.unwrap_or_default()
{
// check if "id" is present, if not, skip
if !item_nbt.contains("id") {
// this just means the item isn't present
items.push(None);
continue;
}
// let item_tag = item_nbt.compound("tag")?;
// let item_extra_attributes = item_tag.compound("ExtraAttributes");
// let item_display = item_tag.compound("display");
let item_tag = item_nbt.compound("tag")?;
let item_extra_attributes = item_tag.compound("ExtraAttributes");
let item_display = item_tag.compound("display");
// items.push(Some(Item {
// id: item_nbt.short("id")?,
// damage: item_nbt.short("Damage")?,
// count: item_nbt.byte("Count")?,
items.push(Some(Item {
id: item_nbt.short("id")?,
damage: item_nbt.short("Damage")?,
count: item_nbt.byte("Count")?,
// head_texture_id: item_tag
// .compound("SkullOwner")
// .and_then(|skull_owner| skull_owner.compound("Properties"))
// .and_then(|properties| properties.list("textures"))
// .and_then(|textures| textures.compounds())
// .and_then(|mut textures| textures.next())
// .and_then(|texture| texture.string("Value"))
// // the real program does some base64+json decoding here but that's unnecessary for the benchmark
// .map(|value| value.to_string()),
// skyblock_id: item_extra_attributes
// .and_then(|e| e.string("id"))
// .map(|id| id.to_string()),
// reforge: item_extra_attributes
// .and_then(|e| e.string("modifier"))
// .map(|id| id.to_string()),
head_texture_id: item_tag
.compound("SkullOwner")
.and_then(|skull_owner| skull_owner.compound("Properties"))
.and_then(|properties| properties.list("textures"))
.and_then(|textures| textures.compounds())
.and_then(|textures| textures.get(0))
.and_then(|texture| texture.string("Value"))
// the real program does some base64+json decoding here but that's unnecessary for the benchmark
.map(|value| value.to_string()),
skyblock_id: item_extra_attributes
.and_then(|e| e.string("id"))
.map(|id| id.to_string()),
reforge: item_extra_attributes
.and_then(|e| e.string("modifier"))
.map(|id| id.to_string()),
// display: ItemDisplay {
// name: item_display
// .and_then(|d| d.string("Name"))
// .map(|n| n.to_string())
// .unwrap_or_default(),
// lore: item_display
// .and_then(|d| d.list("Lore"))
// .and_then(|l| l.strings())
// .map(|l| l.iter().map(|s| s.to_string()).collect())
// .unwrap_or_default(),
// color: item_display.and_then(|d| d.int("color")),
// has_glint: item_extra_attributes
// .map(|e| e.contains("ench"))
// .unwrap_or_default(),
// },
// enchantments: item_extra_attributes
// .and_then(|e| e.compound("enchantments"))
// .map(|e| {
// e.iter()
// .map(|(k, v)| (k.to_string(), v.int().unwrap_or_default()))
// .collect()
// })
// .unwrap_or_default(),
// timestamp: item_extra_attributes
// .and_then(|e| e.string("timestamp"))
// .map(|t| t.to_string()),
// }));
// }
// Some(items)
// }
display: ItemDisplay {
name: item_display
.and_then(|d| d.string("Name"))
.map(|n| n.to_string())
.unwrap_or_default(),
lore: item_display
.and_then(|d| d.list("Lore"))
.and_then(|l| l.strings())
.map(|l| l.iter().map(|s| s.to_string()).collect())
.unwrap_or_default(),
color: item_display.and_then(|d| d.int("color")),
has_glint: item_extra_attributes
.map(|e| e.contains("ench"))
.unwrap_or_default(),
},
enchantments: item_extra_attributes
.and_then(|e| e.compound("enchantments"))
.map(|e| {
e.iter()
.map(|(k, v)| (k.to_string(), v.int().unwrap_or_default()))
.collect()
})
.unwrap_or_default(),
timestamp: item_extra_attributes
.and_then(|e| e.string("timestamp"))
.map(|t| t.to_string()),
}));
}
Some(items)
}
// fn main() {
// let input = black_box(include_bytes!("../tests/hypixel.nbt"));
fn main() {
let input = black_box(include_bytes!("../tests/hypixel.nbt"));
// for _ in 0..1 {
// let nbt = simdnbt::borrow::read(&mut Cursor::new(input));
// let nbt = black_box(nbt.unwrap().unwrap());
// black_box(items_from_nbt(nbt));
// }
// }
fn main() {}
for _ in 0..1 {
let nbt = simdnbt::borrow::read(&mut Cursor::new(input));
let nbt = black_box(nbt.unwrap().unwrap());
black_box(items_from_nbt(nbt));
}
}

View file

@ -1,37 +1,35 @@
// use std::{collections::HashMap, io::Cursor};
use std::{collections::HashMap, io::Cursor};
// use simdnbt::{Deserialize, Serialize};
use simdnbt::{Deserialize, Serialize};
// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
// #[simdnbt(deny_unknown_fields)]
// pub struct TrimMaterialElement {
// pub asset_name: String,
// pub item_model_index: f32,
// pub override_armor_materials: HashMap<String, String>,
// pub description: Option<String>,
// }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[simdnbt(deny_unknown_fields)]
pub struct TrimMaterialElement {
pub asset_name: String,
pub item_model_index: f32,
pub override_armor_materials: HashMap<String, String>,
pub description: Option<String>,
}
// fn main() {
// let original = TrimMaterialElement {
// asset_name: "asset name".to_string(),
// item_model_index: 0.0,
// override_armor_materials: HashMap::from_iter(vec![
// ("asdf".into(), "fdsa".into()),
// ("dsfgdgh".into(), "fgjrtiu".into()),
// ]),
// description: Some("description".to_string()),
// };
fn main() {
let original = TrimMaterialElement {
asset_name: "asset name".to_string(),
item_model_index: 0.0,
override_armor_materials: HashMap::from_iter(vec![
("asdf".into(), "fdsa".into()),
("dsfgdgh".into(), "fgjrtiu".into()),
]),
description: Some("description".to_string()),
};
// let nbt = original.clone().to_nbt();
// let mut buf = Vec::new();
// nbt.write(&mut buf);
let nbt = original.clone().to_nbt();
let mut buf = Vec::new();
nbt.write(&mut buf);
// let nbt = simdnbt::borrow::read(&mut Cursor::new(&buf[..]))
// .unwrap()
// .unwrap();
// let rewritten = TrimMaterialElement::from_nbt(&nbt).unwrap();
let nbt = simdnbt::borrow::read(&mut Cursor::new(&buf[..]))
.unwrap()
.unwrap();
let rewritten = TrimMaterialElement::from_nbt(&nbt).unwrap();
// assert_eq!(original, rewritten);
// }
fn main() {}
assert_eq!(original, rewritten);
}

View file

@ -33,7 +33,7 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
stack: &mut ParsingStack,
) -> Result<(), Error> {
let tag_type = data.read_u8()?;
Ok(match tag_type {
match tag_type {
END_ID => {
// the length is unused for this type of lists
data.skip(4)?;
@ -222,7 +222,8 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
}
}
_ => return Err(Error::UnknownTagId(tag_type)),
})
};
Ok(())
}
pub fn write(&self, data: &mut Vec<u8>) {
@ -424,9 +425,8 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
let length = u32::from(unsafe { value.list_list.0 }) as usize;
let max_tape_offset = u32::from(unsafe { value.list_list.1 }) as usize;
let tape_slice = unsafe {
std::slice::from_raw_parts((self.element as *const TapeElement).add(1), max_tape_offset)
};
let tape_slice =
unsafe { std::slice::from_raw_parts(self.element.add(1), max_tape_offset) };
Some(ListList {
iter: ListListIter {
@ -448,9 +448,8 @@ impl<'a, 'tape> NbtList<'a, 'tape> {
let length = u32::from(unsafe { value.compound_list.0 }) as usize;
let max_tape_offset = u32::from(unsafe { value.compound_list.1 }) as usize;
let tape_slice = unsafe {
std::slice::from_raw_parts((self.element as *const TapeElement).add(1), max_tape_offset)
};
let tape_slice =
unsafe { std::slice::from_raw_parts(self.element.add(1), max_tape_offset) };
Some(CompoundList {
iter: CompoundListIter {
@ -667,7 +666,7 @@ impl<'a: 'tape, 'tape> Iterator for ListListIter<'a, 'tape> {
if self.current_tape_offset + 1 >= self.max_tape_offset {
return None;
}
let element = &self.tape[self.current_tape_offset as usize];
let element = &self.tape[self.current_tape_offset];
let (kind, value) = unsafe { element.kind };
debug_assert!(kind.is_list());
@ -679,7 +678,7 @@ impl<'a: 'tape, 'tape> Iterator for ListListIter<'a, 'tape> {
};
self.current_tape_offset += offset;
return Some(nbt_list);
Some(nbt_list)
}
}
impl Default for ListListIter<'_, '_> {
@ -770,7 +769,7 @@ impl<'a: 'tape, 'tape> Iterator for CompoundListIter<'a, 'tape> {
return None;
}
let element = &self.tape[self.current_tape_offset as usize];
let element = &self.tape[self.current_tape_offset];
let (kind, value) = unsafe { element.kind };
debug_assert_eq!(kind, TapeTagKind::Compound);
@ -782,7 +781,7 @@ impl<'a: 'tape, 'tape> Iterator for CompoundListIter<'a, 'tape> {
};
self.current_tape_offset += offset;
return Some(compound);
Some(compound)
}
}
impl Default for CompoundListIter<'_, '_> {

View file

@ -200,7 +200,7 @@ impl<'a> BaseNbt<'a> {
self.as_compound().get(key)
}
/// Returns whether there is a tag with the given name.
pub fn contains<'tape>(&'a self, key: &str) -> bool {
pub fn contains(&'a self, key: &str) -> bool {
self.as_compound().contains(key)
}
pub fn byte(&self, name: &str) -> Option<i8> {
@ -377,7 +377,7 @@ impl<'a: 'tape, 'tape> NbtTag<'a, 'tape> {
return None;
}
// the value is in the next element because longs are too big to fit in a single element
let value = unsafe { (self.element as *const TapeElement).add(1) };
let value = unsafe { self.element.add(1) };
Some(unsafe { (*value).long })
}
pub fn float(&self) -> Option<f32> {
@ -393,7 +393,7 @@ impl<'a: 'tape, 'tape> NbtTag<'a, 'tape> {
return None;
}
// the value is in the next element because doubles are too big to fit in a single element
let value = unsafe { (self.element as *const TapeElement).add(1) };
let value = unsafe { self.element.add(1) };
Some(unsafe { (*value).double })
}
pub fn byte_array(&self) -> Option<&'a [u8]> {