[][src]Struct nonminmax::NonMaxUsize

#[repr(transparent)]pub struct NonMaxUsize { /* fields omitted */ }

An integer of type usize which is known to not equal usize::MAX.

This type allows for niche filling optimization (similar to the existing std::num::NonZero* types) meaning items such as Option<NonMaxUsize> and Result<NonMaxUsize, ()> take up the same amount of space as usize.

// Create using `new`, extract value using `get`
let x = NonMaxUsize::new(123).unwrap();
assert_eq!(x.get(), 123);

// The value cannot be `usize::MAX`
let y = NonMaxUsize::new(usize::MAX);
assert_eq!(y, None);

// Niche filling optimization works!
use std::mem::size_of;
assert_eq!(size_of::<usize>(), size_of::<NonMaxUsize>());
assert_eq!(size_of::<usize>(), size_of::<Option<NonMaxUsize>>());

Implementations

impl NonMaxUsize[src]

pub fn new(value: usize) -> Option<Self>[src]

Creates an instance of NonMaxUsize by checking if the value is not usize::MAX.

pub unsafe fn new_unchecked(value: usize) -> Self[src]

Creates an instance of NonMaxUsize without checking if the value is not usize::MAX.

Safety

The value cannot be equal to usize::MAX.

pub fn get(self) -> usize[src]

Returns the integer value.

Trait Implementations

impl Clone for NonMaxUsize[src]

impl Copy for NonMaxUsize[src]

impl Debug for NonMaxUsize[src]

impl Display for NonMaxUsize[src]

impl Eq for NonMaxUsize[src]

impl From<NonMaxUsize> for usize[src]

impl Hash for NonMaxUsize[src]

impl Ord for NonMaxUsize[src]

impl PartialEq<NonMaxUsize> for NonMaxUsize[src]

impl PartialOrd<NonMaxUsize> for NonMaxUsize[src]

impl StructuralEq for NonMaxUsize[src]

impl StructuralPartialEq for NonMaxUsize[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.