进行一个unsafe

This commit is contained in:
shenjack 2024-03-06 22:33:46 +08:00
parent c4410c77de
commit 20889e1fd3
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,5 +1,47 @@
/// 用于读取 NBT 数据
pub struct NbtReader<'data> {
data: &'data [u8],
cursor: usize,
}
impl NbtReader<'_> {
pub fn new(data: &[u8]) -> NbtReader {
NbtReader {
data,
cursor: 0,
}
}
pub fn read_i8(&mut self) -> i8 {
let value = self.data[self.cursor] as i8;
self.cursor += 1;
value
}
pub fn read_i16(&mut self) -> i16 {
unsafe {
let value = *(self.data[self.cursor..].as_ptr() as *const i16);
self.cursor += 2;
value.to_be()
}
}
pub fn read_i32(&mut self) -> i32 {
unsafe {
let value = *(self.data[self.cursor..].as_ptr() as *const i32);
self.cursor += 4;
value.to_be()
}
}
pub fn read_i64(&mut self) -> i64 {
unsafe {
let value = *(self.data[self.cursor..].as_ptr() as *const i64);
self.cursor += 8;
value.to_be()
}
}
}
#[derive(Debug, Clone)]
pub enum NbtValue<'value> {
// end: 0
@ -15,17 +57,18 @@ pub enum NbtValue<'value> {
Float(f32),
/// 6
Double(f64),
/// 8
String(&'value str),
/// 7
ByteArray(Vec<i8>),
/// 11
IntArray(Vec<i32>),
/// 12
LongArray(Vec<i64>),
/// 8
/// 或者叫 u8 array
String(&'value str),
/// 9
List(Vec<NbtValue<'value>>),
/// 10
Compound(Vec<(String, NbtValue<'value>)>),
/// 11
IntArray(Vec<i32>),
/// 12
LongArray(Vec<i64>),
}