@ -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 ]