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

remove repr u8 from NbtTag so it can be another byte smaller

This commit is contained in:
mat 2024-05-13 01:14:26 +00:00
parent 97b9d5c76a
commit 2fb9ba2629

View file

@ -120,31 +120,39 @@ impl<'a> BaseNbt<'a> {
} }
/// A single NBT tag. /// A single NBT tag.
#[repr(u8)]
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum NbtTag<'a> { pub enum NbtTag<'a> {
Byte(i8) = BYTE_ID, Byte(i8),
Short(i16) = SHORT_ID, Short(i16),
Int(i32) = INT_ID, Int(i32),
Long(i64) = LONG_ID, Long(i64),
Float(f32) = FLOAT_ID, Float(f32),
Double(f64) = DOUBLE_ID, Double(f64),
ByteArray(&'a [u8]) = BYTE_ARRAY_ID, ByteArray(&'a [u8]),
String(&'a Mutf8Str) = STRING_ID, String(&'a Mutf8Str),
List(NbtList<'a>) = LIST_ID, List(NbtList<'a>),
Compound(NbtCompound<'a>) = COMPOUND_ID, Compound(NbtCompound<'a>),
IntArray(RawList<'a, i32>) = INT_ARRAY_ID, IntArray(RawList<'a, i32>),
LongArray(RawList<'a, i64>) = LONG_ARRAY_ID, LongArray(RawList<'a, i64>),
} }
impl<'a> NbtTag<'a> { impl<'a> NbtTag<'a> {
/// Get the numerical ID of the tag type. /// Get the numerical ID of the tag type.
#[inline] #[inline]
pub fn id(&self) -> u8 { pub fn id(&self) -> u8 {
// SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` match self {
// `union` between `repr(C)` structs, each of which has the `u8` NbtTag::Byte(_) => BYTE_ID,
// discriminant as its first field, so we can read the discriminant NbtTag::Short(_) => SHORT_ID,
// without offsetting the pointer. NbtTag::Int(_) => INT_ID,
unsafe { *<*const _>::from(self).cast::<u8>() } 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( fn read_with_type(