重命名一些东西

This commit is contained in:
shenjack 2024-03-10 15:35:40 +08:00
parent c367042d58
commit d134379f0e
Signed by: shenjack
GPG Key ID: 7B1134A979775551
9 changed files with 115 additions and 11 deletions

View File

@ -43,5 +43,6 @@
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
},
// "rust-analyzer.cargo.features": "all",
}

View File

@ -14,7 +14,8 @@
- [x] JavaNetAfter1_20_2
- [x] BedrockDisk
- [x] BedrockNetVarInt
- [ ] `Serde` 支持
- [ ] `Serde` 支持 (等待 PR, 我不会写了)
- [ ] `Serialize`
- [ ] `Deserialize`
- [ ] `from_value`
@ -87,6 +88,7 @@ writen in rust!
- `shen-nbt5` (编写中)
- 一年左右的技术积累
- 4000 mb/s ?
- 也就 2400 ms/s
- 支持 `serde` 序列化/反序列化
## 测试数据

View File

@ -258,7 +258,7 @@ fn cli_read_test() {
std::thread::sleep(std::time::Duration::from_secs(1));
let start_time = std::time::Instant::now();
// let nbt_data = shen_nbt4::Value::from_vec(data);
let nbt_data =
shen_nbt5::NbtValue::from_binary::<shen_nbt5::nbt_version::Java>(data.as_mut_slice())
.unwrap();

View File

@ -1,3 +1,5 @@
#![feature(core_intrinsics)]
use std::borrow::Cow;
#[cfg(feature = "internal_opt")]
use std::intrinsics::unlikely;
@ -473,7 +475,7 @@ impl<'value> Value<'value> {
let mut nbt_data = NbtData::new(data);
let _type_id = nbt_data.read_byte();
let _name_len = nbt_data.read_short();
let name = String::from_utf8(nbt_data.read_bytes(_name_len as usize)).unwrap();
let _name = String::from_utf8(nbt_data.read_bytes(_name_len as usize)).unwrap();
Value::read_compound(&mut nbt_data)
}
}

View File

@ -468,7 +468,7 @@ impl<'value> Value<'value> {
let mut nbt_data = NbtData::new(data);
let _type_id = nbt_data.read_byte();
let _name_len = nbt_data.read_short();
let name = String::from_utf8(nbt_data.read_bytes(_name_len as usize)).unwrap();
let _name = String::from_utf8(nbt_data.read_bytes(_name_len as usize)).unwrap();
Value::read_compound(&mut nbt_data)
}
}

View File

@ -6,3 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] , optional = true}
[features]
default = []
serde = ["dep:serde"]

View File

@ -1,5 +1,11 @@
pub mod reader;
#[cfg(feature = "serde")]
pub mod ser;
pub mod writer;
#[cfg(feature = "serde")]
use serde;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use reader::NbtReader;
@ -43,7 +49,7 @@ pub mod nbt_version {
fn write_to(value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()>;
fn write_to_with_name(name: &str, value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()>;
fn to_binary(value: &NbtValue) -> NbtResult<Vec<u8>> {
fn to_bytes(value: &NbtValue) -> NbtResult<Vec<u8>> {
let mut buff = Vec::new();
Self::write_to(value, &mut buff)?;
Ok(buff)
@ -137,7 +143,9 @@ pub enum NbtError {
ListTypeNotSame(Vec<NbtTypeId>),
}
pub type NbtResult<T> = Result<T, NbtError>;
pub type NbtResult<T> = std::result::Result<T, NbtError>;
impl std::error::Error for NbtError {}
impl std::fmt::Display for NbtError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -183,6 +191,9 @@ impl std::fmt::Display for NbtError {
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum NbtValue {
// end: 0
/// 1: Byte
@ -261,6 +272,6 @@ impl NbtValue {
where
W: nbt_version::NbtWriteTrait,
{
W::to_binary(self)
W::to_bytes(self)
}
}

82
shen-nbt5/src/ser.rs Normal file
View File

@ -0,0 +1,82 @@
// use serde::{de as serde_de, forward_to_deserialize_any, ser as serde_ser, Deserialize};
use crate::{nbt_version, NbtError, NbtResult, NbtValue};
compile_error!("Serde support is not yet implemented, awaiting for PR");
// impl serde_ser::Error for NbtError {
// fn custom<T: Display>(msg: T) -> Self { NbtError::UnknownErr(msg.to_string()) }
// }
// impl serde_de::Error for NbtError {
// fn custom<T: Display>(msg: T) -> Self { NbtError::UnknownErr(msg.to_string()) }
// }
// pub fn from_bytes<'de, T, V>(bytes: &'de mut [u8]) -> NbtResult<T>
// where
// T: serde_de::Deserialize<'de>,
// V: nbt_version::NbtReadTrait,
// {
// let value = crate::NbtValue::from_binary::<V>(bytes)?;
// let deserializer = DeserializeNbtValue { value: &value };
// T::deserialize(deserializer)
// }
// pub struct DeserializeNbtValue<'de> {
// value: &'de NbtValue,
// }
// impl<'de> DeserializeNbtValue<'de> {
// pub fn new(value: &'de NbtValue) -> Self { Self { value } }
// }
// pub struct ArrayDeserializer<'de, T> {
// iter: std::slice::Iter<'de, T>,
// }
// impl <'de, T> ArrayDeserializer<'de, T> {
// pub fn new(iter: std::slice::Iter<'de, T>) -> Self {
// Self { iter }
// }
// }
// impl <'de, T> serde_de::SeqAccess<'de> for ArrayDeserializer<'de, T> {
// type Error = NbtError;
// fn next_element_seed<U>(&mut self, seed: U) -> NbtResult<Option<U::Value>>
// where
// U: serde_de::DeserializeSeed<'de>,
// {
// match self.iter.next() {
// Some(v) => seed.deserialize(DeserializeNbtValue::new(v)).map(Some),
// None => Ok(None),
// }
// }
// }
// impl<'de, 'a> serde_de::Deserializer for DeserializeNbtValue<'de> {
// forward_to_deserialize_any! {
// bool i8 i16 i32 i64 f32 f64 char string bytes
// }
// fn deserialize_any<V>(self, visitor: V) -> NbtResult<V::Value>
// where
// V: serde_de::Visitor<'de>,
// {
// match self.value {
// NbtValue::Byte(v) => visitor.visit_i8(*v),
// NbtValue::Short(v) => visitor.visit_i16(*v),
// NbtValue::Int(v) => visitor.visit_i32(*v),
// NbtValue::Long(v) => visitor.visit_i64(*v),
// NbtValue::Float(v) => visitor.visit_f32(*v),
// NbtValue::Double(v) => visitor.visit_f64(*v),
// NbtValue::ByteArray(v) => visitor.visit_seq(ArrayDeserializer::new(v.iter())),
// NbtValue::String(v) => visitor.visit_string(v.clone()),
// NbtValue::List(v) => visitor.visit_seq(ListDeserializer::new(v)),
// NbtValue::Compound(name, v) => visitor.visit_map(CompoundDeserializer::new(v)),
// NbtValue::IntArray(v) => visitor.visit_seq(ArrayDeserializer::new(v.iter())),
// NbtValue::LongArray(v) => visitor.visit_seq(ArrayDeserializer::new(v.iter())),
// }
// }
// }

View File

@ -196,7 +196,7 @@ impl NbtWriteTrait for JavaNetAfter1_20_2 {
JavaNetAfter1_20_2::write_to(value, buff)
}
#[inline]
fn to_binary(value: &NbtValue) -> NbtResult<Vec<u8>> {
fn to_bytes(value: &NbtValue) -> NbtResult<Vec<u8>> {
let mut buff = Vec::new();
JavaNetAfter1_20_2::write_to(value, &mut buff)?;
Ok(buff)
@ -336,7 +336,7 @@ impl NbtWriteTrait for BedrockDisk {
Ok(())
}
fn to_binary(value: &NbtValue) -> NbtResult<Vec<u8>> {
fn to_bytes(value: &NbtValue) -> NbtResult<Vec<u8>> {
let mut buff = Vec::new();
BedrockDisk::write_to(value, &mut buff)?;
Ok(buff)
@ -532,7 +532,7 @@ impl NbtWriteTrait for BedrockNetVarInt {
Self::write_to(value, buff)?;
Ok(())
}
fn to_binary(value: &NbtValue) -> NbtResult<Vec<u8>> {
fn to_bytes(value: &NbtValue) -> NbtResult<Vec<u8>> {
let mut buff = Vec::new();
BedrockNetVarInt::write_to(value, &mut buff)?;
Ok(buff)