From 40a0c8acfbfb88be791c295a14014468e2fd4298 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 11 Mar 2023 16:38:13 -0600 Subject: slightly optimize azalea_nbt::Tag::id --- azalea-nbt/src/tag.rs | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) (limited to 'azalea-nbt/src') diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index 23bed1d4..87a7374d 100755 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -5,43 +5,34 @@ use serde::{Deserialize, Serialize}; /// An NBT value. #[derive(Clone, Debug, PartialEq, Default)] +#[repr(u8)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))] pub enum Tag { #[default] - End, // 0 - Byte(i8), // 1 - Short(i16), // 2 - Int(i32), // 3 - Long(i64), // 4 - Float(f32), // 5 - Double(f64), // 6 - ByteArray(Vec), // 7 - String(String), // 8 - List(Vec), // 9 - Compound(AHashMap), // 10 - IntArray(Vec), // 11 - LongArray(Vec), // 12 + End = 0, + Byte(i8) = 1, + Short(i16) = 2, + Int(i32) = 3, + Long(i64) = 4, + Float(f32) = 5, + Double(f64) = 6, + ByteArray(Vec) = 7, + String(String) = 8, + List(Vec) = 9, + Compound(AHashMap) = 10, + IntArray(Vec) = 11, + LongArray(Vec) = 12, } impl Tag { /// Get the numerical ID of the tag type. #[inline] pub fn id(&self) -> u8 { - match self { - Tag::End => 0, - Tag::Byte(_) => 1, - Tag::Short(_) => 2, - Tag::Int(_) => 3, - Tag::Long(_) => 4, - Tag::Float(_) => 5, - Tag::Double(_) => 6, - Tag::ByteArray(_) => 7, - Tag::String(_) => 8, - Tag::List(_) => 9, - Tag::Compound(_) => 10, - Tag::IntArray(_) => 11, - Tag::LongArray(_) => 12, - } + // 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::() } } /// If the type is a byte, return the [`i8`]. -- cgit v1.2.3