Made Tag::from_u16, from_u16_exhaustive and to_u16 const

(cherry picked from commit 5c3c25ec2d152a672b0b1307efc69ccfcb0bea29)
This commit is contained in:
Dario Bartussek authored and A. Molzer committed 2026-01-05 18:34:36 +01:00
1 parent efd2a6dbd8
commit a63a01319c
1 file changed
+13 -7
+13 -7
View File
@@ -20,7 +20,7 @@ macro_rules! tags {
impl $name { impl $name {
#[inline(always)] #[inline(always)]
fn __from_inner_type(n: $ty) -> Result<Self, $ty> { const fn __from_inner_type(n: $ty) -> Result<Self, $ty> {
match n { match n {
$( $val => Ok($name::$tag), )* $( $val => Ok($name::$tag), )*
n => Err(n), n => Err(n),
@@ -28,7 +28,7 @@ macro_rules! tags {
} }
#[inline(always)] #[inline(always)]
fn __to_inner_type(&self) -> $ty { const fn __to_inner_type(&self) -> $ty {
match *self { match *self {
$( $name::$tag => $val, )* $( $name::$tag => $val, )*
$( $name::Unknown($unknown_doc) => { $unknown_doc }, )* $( $name::Unknown($unknown_doc) => { $unknown_doc }, )*
@@ -42,19 +42,25 @@ macro_rules! tags {
($name:tt, u16, $($unknown_doc:ident)*) => { ($name:tt, u16, $($unknown_doc:ident)*) => {
impl $name { impl $name {
#[inline(always)] #[inline(always)]
pub fn from_u16(val: u16) -> Option<Self> { pub const fn from_u16(val: u16) -> Option<Self> {
Self::__from_inner_type(val).ok() match Self::__from_inner_type(val) {
Ok(v) => Some(v),
Err(_) => None,
}
} }
$( $(
#[inline(always)] #[inline(always)]
pub fn from_u16_exhaustive($unknown_doc: u16) -> Self { pub const fn from_u16_exhaustive($unknown_doc: u16) -> Self {
Self::__from_inner_type($unknown_doc).unwrap_or_else(|_| $name::Unknown($unknown_doc)) match Self::__from_inner_type($unknown_doc) {
Ok(v) => v,
Err(_) => $name::Unknown($unknown_doc),
}
} }
)* )*
#[inline(always)] #[inline(always)]
pub fn to_u16(&self) -> u16 { pub const fn to_u16(&self) -> u16 {
Self::__to_inner_type(self) Self::__to_inner_type(self)
} }
} }