From 2fb9ba26291a10d38f761c4f26333e067cfbfe8a Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 13 May 2024 01:14:26 +0000 Subject: [PATCH] remove repr u8 from NbtTag so it can be another byte smaller --- simdnbt/src/borrow/mod.rs | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index 881e725..143ca9b 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -120,31 +120,39 @@ impl<'a> BaseNbt<'a> { } /// A single NBT tag. -#[repr(u8)] #[derive(Debug, PartialEq, Clone)] pub enum NbtTag<'a> { - Byte(i8) = BYTE_ID, - Short(i16) = SHORT_ID, - Int(i32) = INT_ID, - Long(i64) = LONG_ID, - Float(f32) = FLOAT_ID, - Double(f64) = DOUBLE_ID, - ByteArray(&'a [u8]) = BYTE_ARRAY_ID, - String(&'a Mutf8Str) = STRING_ID, - List(NbtList<'a>) = LIST_ID, - Compound(NbtCompound<'a>) = COMPOUND_ID, - IntArray(RawList<'a, i32>) = INT_ARRAY_ID, - LongArray(RawList<'a, i64>) = LONG_ARRAY_ID, + Byte(i8), + Short(i16), + Int(i32), + Long(i64), + Float(f32), + Double(f64), + ByteArray(&'a [u8]), + String(&'a Mutf8Str), + List(NbtList<'a>), + Compound(NbtCompound<'a>), + IntArray(RawList<'a, i32>), + LongArray(RawList<'a, i64>), } impl<'a> NbtTag<'a> { /// Get the numerical ID of the tag type. #[inline] pub fn id(&self) -> u8 { - // SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` - // `union` between `repr(C)` structs, each of which has the `u8` - // discriminant as its first field, so we can read the discriminant - // without offsetting the pointer. - unsafe { *<*const _>::from(self).cast::() } + match self { + NbtTag::Byte(_) => BYTE_ID, + NbtTag::Short(_) => SHORT_ID, + NbtTag::Int(_) => INT_ID, + NbtTag::Long(_) => LONG_ID, + NbtTag::Float(_) => FLOAT_ID, + NbtTag::Double(_) => DOUBLE_ID, + NbtTag::ByteArray(_) => BYTE_ARRAY_ID, + NbtTag::String(_) => STRING_ID, + NbtTag::List(_) => LIST_ID, + NbtTag::Compound(_) => COMPOUND_ID, + NbtTag::IntArray(_) => INT_ARRAY_ID, + NbtTag::LongArray(_) => LONG_ARRAY_ID, + } } fn read_with_type(