From c6191367529b9ccafadcd277ff1297ff8439279c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Sun, 24 May 2020 15:07:50 +0300 Subject: [PATCH] Remove Eq bound of ReservedValue trait A full Eq implementation is no needed for ReservedValue, as we only need to check whether a value is the reserved one. For entities (defined with `entity_impl!`) this doesn't make much difference, but for more complicated types this avoids generating redundant `Eq`s. --- cranelift/entity/src/lib.rs | 4 ++++ cranelift/entity/src/packed_option.rs | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) 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]