Struct dactyl::GreaterThanZero [−][src]
pub struct GreaterThanZero<T>(_)
where
T: GtZero;
Expand description
Greater Than Zero
GreaterThanZero
is a memory-saving replacement for Option<T>
where
T
is only “some” if it is > 0
(and not NAN
or NEG_INFINITY
). In
other words, GreaterThanZero<T>
has the same size as T
.
It is similar to the std::num::NonZeroX
types, except it is its own
Option
so does not need to be wrapped (it is infallible). Instead of
is_some
, you’d use GreaterThanZero::is_gt_zero
.
To obtain the underlying primitive, you can use GreaterThanZero::get
to
ensure the value is “some”, or GreaterThanZero::get_unchecked
to
return the value regardless of zeroness. (Dereferencing also returns the
unchecked value, albeit as a reference.)
Non-qualifying values are stored regardless and can be obtained by
dereferencing or GreaterThanZero::get_unchecked
.
Other neat things: this struct implements Add
, Div
, Mul
, Rem
,
Sub
, and the corresponding *Assign
traits, so you can manipulate
values in place. Zeroness is always evaluated in realtime, so this works
even in cases where the result crosses that line.
It also implements Option
-like methods GreaterThanZero::filter
,
GreaterThanZero::map
, GreaterThanZero::replace
, and
GreaterThanZero::zip
.
Examples
use dactyl::GreaterThanZero; let gt0 = GreaterThanZero::from(-3_isize); assert!(! gt0.is_gt_zero()); assert_eq!(gt0.get(), None); assert_eq!(gt0.get_unchecked(), -3_isize); let gt1 = GreaterThanZero::from(5.5_f64); assert!(gt1.is_gt_zero()); assert_eq!(gt1.get(), Some(5.5_f64)); assert_eq!(gt1.get_unchecked(), 5.5_f64);
Implementations
Filter.
Like std::option::Option::filter
, this will evaluate the instance
value with your callback, leaving it as-was if true
, or zeroing it
out if false
.
Examples
use dactyl::GreaterThanZero; // This filter is not applied as the value is not > 0. assert_eq!( GreaterThanZero::from(-3_isize).filter(|x| x != 4), GreaterThanZero::from(-3_isize) ); // This filter is applied and fails, zeroing the value. assert_eq!( GreaterThanZero::from(4_u8).filter(|x| x != 4), GreaterThanZero::from(0_u8) ); // This filter is applied and passes, keeping the original value. assert_eq!( GreaterThanZero::from(32_u32).filter(|x| x != 4), GreaterThanZero::from(32_u32) );
Get Inner Value.
This will return Some(T)
if the value is greater than zero, otherwise
None
.
Get Unchecked.
This will return the inner value regardless of its greater-than- zeroness. This is equivalent to dereferencing with copy.
Is Greater Than Zero.
This will return true
if the value is > 0
, not NAN
, and not
NEG_INFINITY
.
Map.
Operate on the inner value if it is greater than zero, otherwise do
nothing. This is equivalent to std::option::Option::map
, except
that the returned value can be less than or equal to zero (our
equivalent of None
).
Examples
use dactyl::GreaterThanZero; // Turn 4 to 8. assert_eq!( GreaterThanZero::from(4_u8).map(|_| 8), GreaterThanZero::from(8_u8) );
Replace.
This is a convenience method allowing the inner value to be replaced
with any arbitrary value. As this type is Copy
, you could avoid the
&mut
and just reassign.
Zip.
Produce a tuple of the inner value of A and B if both are greater than
zero. This is comparable to std::option::Option::zip
except that
the inner types of A and B are allowed to be different.
Trait Implementations
Performs the +=
operation. Read more
Performs the /=
operation. Read more
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the *=
operation. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Performs the %=
operation. Read more
Performs the -=
operation. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for GreaterThanZero<T> where
T: RefUnwindSafe,
impl<T> Send for GreaterThanZero<T> where
T: Send,
impl<T> Sync for GreaterThanZero<T> where
T: Sync,
impl<T> Unpin for GreaterThanZero<T> where
T: Unpin,
impl<T> UnwindSafe for GreaterThanZero<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more