diff --git a/cranelift/entity/src/lib.rs b/cranelift/entity/src/lib.rs index f9062a8c51..4adf4a42cc 100644 --- a/cranelift/entity/src/lib.rs +++ b/cranelift/entity/src/lib.rs @@ -85,6 +85,10 @@ macro_rules! entity_impl { fn reserved_value() -> $entity { $entity($crate::__core::u32::MAX) } + + fn is_reserved_value(&self) -> bool { + self.0 == $crate::__core::u32::MAX + } } impl $entity { diff --git a/cranelift/entity/src/packed_option.rs b/cranelift/entity/src/packed_option.rs index 63764406c0..3bca32f8a0 100644 --- a/cranelift/entity/src/packed_option.rs +++ b/cranelift/entity/src/packed_option.rs @@ -11,9 +11,11 @@ use core::fmt; use core::mem; /// Types that have a reserved value which can't be created any other way. -pub trait ReservedValue: Eq { +pub trait ReservedValue { /// Create an instance of the reserved value. fn reserved_value() -> Self; + /// Checks whether value is the reserved one. + fn is_reserved_value(&self) -> bool; } /// Packed representation of `Option`. @@ -23,12 +25,12 @@ pub struct PackedOption(T); impl PackedOption { /// Returns `true` if the packed option is a `None` value. pub fn is_none(&self) -> bool { - self.0 == T::reserved_value() + self.0.is_reserved_value() } /// Returns `true` if the packed option is a `Some` value. pub fn is_some(&self) -> bool { - self.0 != T::reserved_value() + !self.0.is_reserved_value() } /// Expand the packed option into a normal `Option`. @@ -75,7 +77,7 @@ impl From for PackedOption { /// Convert `t` into a packed `Some(x)`. fn from(t: T) -> Self { debug_assert!( - t != T::reserved_value(), + !t.is_reserved_value(), "Can't make a PackedOption from the reserved value." ); Self(t) @@ -123,6 +125,10 @@ mod tests { fn reserved_value() -> Self { NoC(13) } + + fn is_reserved_value(&self) -> bool { + self.0 == 13 + } } #[test] @@ -145,6 +151,10 @@ mod tests { fn reserved_value() -> Self { Ent(13) } + + fn is_reserved_value(&self) -> bool { + self.0 == 13 + } } #[test]