pub trait Bound<T: Sized>: From<Option<T>> + Copy {
// Required method
fn unbound(self) -> Option<T>;
}Expand description
For any type T, Bound<T> is a type, which has set of values bigger than T by one.
- Each value from
Thas unambiguous mapping toBound<T>. - Each value from
Bound<T>, except one called upper, has unambiguous mapping toT. - upper value has no mapping to
T, but can be considered as value equal toT::max_value + 1.
§Examples
-
For any
T, which max value can be get by calling some static live time function,Option<T>`` can be used asBound.None` is upper. Mapping: Some(t) -> t, t -> Some(t). -
When
innerfield max value is always smaller thaninnertype max value, then we can use this variant:
use numerated::Bound;
/// `inner` is a value from 0 to 99.
struct Number { inner: u32 }
/// `inner` is a value from 0 to 100.
#[derive(Clone, Copy)]
struct BoundForNumber { inner: u32 }
impl From<Option<Number>> for BoundForNumber {
fn from(t: Option<Number>) -> Self {
Self { inner: t.map(|t| t.inner).unwrap_or(100) }
}
}
impl Bound<Number> for BoundForNumber {
fn unbound(self) -> Option<Number> {
(self.inner < 100).then_some(Number { inner: self.inner })
}
}Required Methods§
Object Safety§
This trait is not object safe.