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

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

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 conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.