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.
This commit is contained in:
committed by
Benjamin Bouvier
parent
eeb1e141ba
commit
c619136752
@@ -85,6 +85,10 @@ macro_rules! entity_impl {
|
|||||||
fn reserved_value() -> $entity {
|
fn reserved_value() -> $entity {
|
||||||
$entity($crate::__core::u32::MAX)
|
$entity($crate::__core::u32::MAX)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_reserved_value(&self) -> bool {
|
||||||
|
self.0 == $crate::__core::u32::MAX
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $entity {
|
impl $entity {
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ use core::fmt;
|
|||||||
use core::mem;
|
use core::mem;
|
||||||
|
|
||||||
/// Types that have a reserved value which can't be created any other way.
|
/// 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.
|
/// Create an instance of the reserved value.
|
||||||
fn reserved_value() -> Self;
|
fn reserved_value() -> Self;
|
||||||
|
/// Checks whether value is the reserved one.
|
||||||
|
fn is_reserved_value(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Packed representation of `Option<T>`.
|
/// Packed representation of `Option<T>`.
|
||||||
@@ -23,12 +25,12 @@ pub struct PackedOption<T: ReservedValue>(T);
|
|||||||
impl<T: ReservedValue> PackedOption<T> {
|
impl<T: ReservedValue> PackedOption<T> {
|
||||||
/// Returns `true` if the packed option is a `None` value.
|
/// Returns `true` if the packed option is a `None` value.
|
||||||
pub fn is_none(&self) -> bool {
|
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.
|
/// Returns `true` if the packed option is a `Some` value.
|
||||||
pub fn is_some(&self) -> bool {
|
pub fn is_some(&self) -> bool {
|
||||||
self.0 != T::reserved_value()
|
!self.0.is_reserved_value()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expand the packed option into a normal `Option`.
|
/// Expand the packed option into a normal `Option`.
|
||||||
@@ -75,7 +77,7 @@ impl<T: ReservedValue> From<T> for PackedOption<T> {
|
|||||||
/// Convert `t` into a packed `Some(x)`.
|
/// Convert `t` into a packed `Some(x)`.
|
||||||
fn from(t: T) -> Self {
|
fn from(t: T) -> Self {
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
t != T::reserved_value(),
|
!t.is_reserved_value(),
|
||||||
"Can't make a PackedOption from the reserved value."
|
"Can't make a PackedOption from the reserved value."
|
||||||
);
|
);
|
||||||
Self(t)
|
Self(t)
|
||||||
@@ -123,6 +125,10 @@ mod tests {
|
|||||||
fn reserved_value() -> Self {
|
fn reserved_value() -> Self {
|
||||||
NoC(13)
|
NoC(13)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_reserved_value(&self) -> bool {
|
||||||
|
self.0 == 13
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -145,6 +151,10 @@ mod tests {
|
|||||||
fn reserved_value() -> Self {
|
fn reserved_value() -> Self {
|
||||||
Ent(13)
|
Ent(13)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_reserved_value(&self) -> bool {
|
||||||
|
self.0 == 13
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user