Trait Bound

Source
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 T has unambiguous mapping to Bound<T>.
  • Each value from Bound<T>, except one called upper, has unambiguous mapping to T.
  • upper value has no mapping to T, but can be considered as value equal to T::max_value + 1.

§Examples

  1. For any T, which max value can be get by calling some static live time function, Option<T>`` can be used as Bound. None` is upper. Mapping: Some(t) -> t, t -> Some(t).

  2. When inner field max value is always smaller than inner type 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§

Source

fn unbound(self) -> Option<T>

Unbound means mapping bound back to value if possible.

  • In case bound is upper, then returns None.
  • Otherwise returns Some(p), p: T.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Copy> Bound<T> for OptionBound<T>