From 1dee92147d2db42f610484242de39994c2dbb50d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 6 Mar 2024 23:32:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96NbtReader=E7=9A=84read=5Fint?= =?UTF-8?q?=5Farray=E5=92=8Cread=5Flong=5Farray=E6=96=B9=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shen-nbt5/src/lib.rs | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index cbd4e99..60332a5 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -61,6 +61,7 @@ impl NbtReader<'_> { read!(read_u64, u64, 8); read!(read_f32, f32, 4, false); read!(read_f64, f64, 8, false); + pub fn read_u8_array(&mut self, len: usize) -> &[u8] { let value = &self.data[self.cursor..self.cursor + len]; self.cursor += len; @@ -78,31 +79,17 @@ impl NbtReader<'_> { self.cursor += len; value.into_owned() } - pub fn read_byte_array(&mut self, len: usize) -> Vec { - let value = self.data[self.cursor..self.cursor + len].to_vec(); - self.cursor += len; - value + pub fn read_int_array(&mut self, len: usize) -> &[i32] { + let datas = self.read_u8_array(len * 4); + let value = unsafe { + std::slice::from_raw_parts(datas.as_ptr() as *const i32, len) + }; } - pub fn read_int_array(&mut self, len: usize) -> Vec { - let mut value = Vec::with_capacity(len); - for _ in 0..len { - value.push(self.read_i32()); - } - value - } - pub fn read_long_array(&mut self, len: usize) -> Vec { - let mut value = Vec::with_capacity(len); - for _ in 0..len { - value.push(self.read_i64()); - } - value - } - pub fn read_byte_array_array(&mut self, len: usize) -> Vec> { - let mut value = Vec::with_capacity(len); - for _ in 0..len { - value.push(self.read_byte_array(self.read_u8() as usize)); - } - value + pub fn read_long_array(&mut self, len: usize) -> &[i64] { + let datas = self.read_u8_array(len * 8); + let value = unsafe { + std::slice::from_raw_parts(datas.as_ptr() as *const i64, len) + }; } }