Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made Tag::from_u16, from_u16_exhaustive and to_u16 const #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ macro_rules! tags {

impl $name {
#[inline(always)]
fn __from_inner_type(n: $ty) -> Result<Self, $ty> {
const fn __from_inner_type(n: $ty) -> Result<Self, $ty> {
match n {
$( $val => Ok($name::$tag), )*
n => Err(n),
}
}

#[inline(always)]
fn __to_inner_type(&self) -> $ty {
const fn __to_inner_type(&self) -> $ty {
match *self {
$( $name::$tag => $val, )*
$( $name::Unknown(n) => { $unknown_doc; n }, )*
Expand All @@ -42,20 +42,26 @@ macro_rules! tags {
($name:tt, u16, $($unknown_doc:literal)*) => {
impl $name {
#[inline(always)]
pub fn from_u16(val: u16) -> Option<Self> {
Self::__from_inner_type(val).ok()
pub const fn from_u16(val: u16) -> Option<Self> {
match Self::__from_inner_type(val) {
Ok(v) => Some(v),
Err(_) => None,
}
}

$(
#[inline(always)]
pub fn from_u16_exhaustive(val: u16) -> Self {
pub const fn from_u16_exhaustive(val: u16) -> Self {
$unknown_doc;
Self::__from_inner_type(val).unwrap_or_else(|_| $name::Unknown(val))
match Self::__from_inner_type(val) {
Ok(v) => v,
Err(_) => $name::Unknown(val),
}
}
)*

#[inline(always)]
pub fn to_u16(&self) -> u16 {
pub const fn to_u16(&self) -> u16 {
Self::__to_inner_type(self)
}
}
Expand Down