better type assertion ergonomic

This commit is contained in:
2026-04-20 17:57:54 +08:00
parent 581c333070
commit 11b0b8a78e
13 changed files with 399 additions and 258 deletions
+24 -8
View File
@@ -160,6 +160,22 @@ enum TagVal {
pub(crate) struct RawTag(TagVal);
impl RawTag {
pub(crate) const P1: RawTag = RawTag(TagVal::_P1);
pub(crate) const P2: RawTag = RawTag(TagVal::_P2);
pub(crate) const P3: RawTag = RawTag(TagVal::_P3);
pub(crate) const P4: RawTag = RawTag(TagVal::_P4);
pub(crate) const P5: RawTag = RawTag(TagVal::_P5);
pub(crate) const P6: RawTag = RawTag(TagVal::_P6);
pub(crate) const P7: RawTag = RawTag(TagVal::_P7);
pub(crate) const N1: RawTag = RawTag(TagVal::_N1);
pub(crate) const N2: RawTag = RawTag(TagVal::_N2);
pub(crate) const N3: RawTag = RawTag(TagVal::_N3);
pub(crate) const N4: RawTag = RawTag(TagVal::_N4);
pub(crate) const N5: RawTag = RawTag(TagVal::_N5);
pub(crate) const N6: RawTag = RawTag(TagVal::_N6);
pub(crate) const N7: RawTag = RawTag(TagVal::_N7);
#[inline]
#[must_use]
pub(crate) fn new(neg: bool, val: NonZeroU8) -> RawTag {
@@ -195,7 +211,7 @@ impl RawTag {
/// `val` must be in the range `1..8`
#[inline]
#[must_use]
pub(crate) unsafe fn new_unchecked(neg: bool, val: u8) -> RawTag {
pub(crate) const unsafe fn new_unchecked(neg: bool, val: u8) -> RawTag {
RawTag(match (neg, val) {
(false, 1) => TagVal::_P1,
(false, 2) => TagVal::_P2,
@@ -244,7 +260,7 @@ impl RawTag {
#[inline]
#[must_use]
pub(crate) fn neg_val(self) -> (bool, u8) {
pub(crate) const fn neg_val(self) -> (bool, u8) {
match self.0 {
TagVal::_P1 => (false, 1),
TagVal::_P2 => (false, 2),
@@ -285,17 +301,17 @@ impl Header {
}
#[inline]
fn tag(self) -> RawTag {
const fn tag(self) -> RawTag {
unsafe { RawTag::new_unchecked(self.get_sign(), self.get_tag()) }
}
#[inline]
fn get_sign(self) -> bool {
const fn get_sign(self) -> bool {
self.0 & 0x8000 != 0
}
#[inline]
fn get_tag(self) -> u8 {
const fn get_tag(self) -> u8 {
(self.0 & 0x0007) as u8
}
@@ -341,7 +357,7 @@ impl Value {
#[inline]
#[must_use]
pub(crate) fn tag(&self) -> RawTag {
pub(crate) const fn tag(&self) -> RawTag {
self.header.tag()
}
@@ -417,7 +433,7 @@ impl RawBox {
#[inline]
#[must_use]
pub(crate) fn tag(&self) -> Option<RawTag> {
pub(crate) const fn tag(&self) -> Option<RawTag> {
if self.is_value() {
Some(unsafe { self.value.tag() })
} else {
@@ -433,7 +449,7 @@ impl RawBox {
#[inline]
#[must_use]
pub(crate) fn is_value(&self) -> bool {
pub(crate) const fn is_value(&self) -> bool {
(unsafe { self.float.is_nan() } && unsafe { self.bits & SIGN_MASK != QUIET_NAN })
}