RangedNonZeroI8

Struct RangedNonZeroI8 

Source
pub struct RangedNonZeroI8<const MIN: i8, const MAX: i8>(/* private fields */);
Expand description

i8 not to equal zero with a specified minimum and maximum value

Implementations§

Source§

impl RangedNonZeroI8<{ i8::MIN }, { i8::MAX }>

Source

pub const fn from_nonzero(nonzero: NonZero<i8>) -> Self

Convert from NonZero.

assert_eq!(
    NonZeroI8::from_nonzero(NonZero::new(42).unwrap()),
    NonZeroI8::new::<42>(),
);
Source§

impl<const MIN: i8, const MAX: i8> RangedNonZeroI8<MIN, MAX>

Source

pub const fn from_ranged(ranged: RangedI8<MIN, MAX>) -> Self

Convert from RangedI8.

Won’t compile if the range contains zero. If you need to check at runtime for zero instead of at compile-time, try using RangedI8::to_ranged_nonzero().

assert_eq!(
    RangedNonZeroI8::from_ranged(RangedI8::<1, 100>::new::<42>()),
    RangedNonZeroI8::<1, 100>::new::<42>(),
);

RangedNonZeroI8::from_ranged(RangedI8::<0, 100>::new::<42>())
Source§

impl<const MIN: i8, const MAX: i8> RangedNonZeroI8<MIN, MAX>

Source

pub const fn checked_div_nonzero( self, rhs: impl AsRepr<NonZero<i8>>, ) -> Result<Option<Self>>

Checked integer division by a non-zero number.

Returns None on overflow.

let a = RangedNonZeroI8::<1, 50>::new::<50>();
let b = RangedNonZeroI8::<1, 50>::new::<2>();

assert_eq!(
    a.checked_div_nonzero(b).unwrap().unwrap(),
    RangedNonZeroI8::new::<25>(),
);
Source§

impl<const MIN: i8, const MAX: i8> RangedNonZeroI8<MIN, MAX>

Source

pub const BITS: u32 = 8u32

The size of this integer type in bits.

Source

pub const MAX: Self

The largest value that can be represented by this integer type.

Source

pub const MIN: Self

The smallest value that can be represented by this integer type.

Source

pub const fn new<const N: i8>() -> Self

Create a new ranged integer.

Won’t compile if out of bounds.

Compiles:

RangedNonZeroI8::<-1, 3>::new::<1>();
RangedNonZeroI8::<-1, 3>::new::<2>();
RangedNonZeroI8::<-1, 3>::new::<3>();

Does not compile:

RangedNonZeroI8::<-1, 3>::new::<-2>();
RangedNonZeroI8::<-1, 3>::new::<0>();
RangedNonZeroI8::<-1, 3>::new::<4>();
Source

pub const fn with_i8(value: impl AsRepr<i8>) -> Result<Option<Self>>

Try to create a new ranged integer.

Returns Err if out of bounds, Ok(None) if zero.

RangedNonZeroI8::<-1, 1>::with_i8(-1).unwrap().unwrap();
RangedNonZeroI8::<-1, 1>::with_i8(1).unwrap().unwrap();
assert_eq!(RangedNonZeroI8::<-1, 1>::with_i8(0), Ok(None));
assert_eq!(RangedNonZeroI8::<-1, 1>::with_i8(-2).unwrap_err(), Error::NegOverflow);
assert_eq!(RangedNonZeroI8::<-1, 1>::with_i8(2).unwrap_err(), Error::PosOverflow);
Source

pub const fn with_nonzero(nonzero: impl AsRepr<NonZero<i8>>) -> Result<Self>

Convert from NonZero.

assert_eq!(
    RangedNonZeroI8::<1, 100>::with_nonzero(NonZero::new(42).unwrap()).unwrap(),
    RangedNonZeroI8::<1, 100>::new::<42>(),
);
Source

pub const fn get(self) -> i8

Return the contained value as a primitive type.

assert_eq!(42, RangedNonZeroI8::<1, 100>::new::<42>().get());
Source

pub const fn to_nonzero(self) -> NonZero<i8>

Convert to NonZero.

assert_eq!(
    NonZero::new(42).unwrap(),
    RangedNonZeroI8::<1, 100>::new::<42>().to_nonzero(),
);
Source

pub const fn to_ranged(self) -> RangedI8<MIN, MAX>

Convert to RangedI8.

assert_eq!(
    RangedI8::<1, 100>::new::<42>(),
    RangedNonZeroI8::<1, 100>::new::<42>().to_ranged(),
);
Source

pub const fn leading_zeros(self) -> RangedU32<0, { i8::BITS }>

Return the number of leading zeros in the binary representation of self.

let n = RangedNonZeroI8::<{ i8::MIN }, { i8::MAX }>::MAX;

assert_eq!(n.leading_zeros().get(), 1);
Source

pub const fn trailing_zeros(self) -> RangedU32<0, { i8::BITS }>

Return the number of trailing zeros in the binary representation of self.

let n = RangedNonZeroI8::<-128, 127>::new::<0b0101000>();

assert_eq!(n.trailing_zeros().get(), 3);
Source

pub const fn count_ones(self) -> RangedU32<0, { i8::BITS }>

Return the number of ones in the binary representation of self.

let a = RangedNonZeroI8::<-128, 127>::new::<0b100_0000>();
let b = RangedNonZeroI8::<-128, 127>::new::<0b100_0011>();

assert_eq!(a.count_ones().get(), 1);
assert_eq!(b.count_ones().get(), 3);
Source

pub const fn checked_add(self, other: impl AsRepr<i8>) -> Result<Option<Self>>

Add two ranged integers together.

Returns an Error on overflow.

let a = RangedNonZeroI8::<1, 100>::new::<50>();
let b = RangedNonZeroI8::<1, 100>::new::<5>();
let c = a.checked_add(b).unwrap().unwrap();

assert!(c.checked_add(a).is_err());
assert_eq!(c.get(), 55);
assert_eq!(a.checked_add(a).unwrap().unwrap().get(), 100);
Source

pub const fn checked_mul(self, other: impl AsRepr<i8>) -> Result<Option<Self>>

Multiply two ranged integers together.

Returns an Error on overflow.

let a = RangedNonZeroI8::<-100, 100>::new::<50>();
let b = RangedNonZeroI8::<-100, 100>::new::<5>();
let c = RangedNonZeroI8::<-100, 100>::new::<-75>();

assert_eq!(b.checked_mul(b).unwrap().unwrap().get(), 25);
assert_eq!(a.checked_mul(c).unwrap_err(), Error::NegOverflow);
assert_eq!(c.checked_mul(c).unwrap_err(), Error::PosOverflow);
Source

pub const fn checked_pow(self, other: impl AsRepr<u32>) -> Result<Self>

Raise to an integer power.

Returns an Error on overflow.

let a = RangedNonZeroI8::<-100, 100>::new::<50>();
let b = RangedNonZeroI8::<-100, 100>::new::<5>();
let c = RangedNonZeroI8::<-100, 100>::new::<-75>();
let d = RangedNonZeroI8::<-100, 100>::new::<2>();

assert_eq!(a.checked_pow(2).unwrap_err(), Error::PosOverflow);
assert_eq!(b.checked_pow(2).unwrap().get(), 25);
assert_eq!(c.checked_pow(3).unwrap_err(), Error::NegOverflow);
assert_eq!(d.checked_pow(3).unwrap().get(), 8);
Source

pub const fn checked_div( self, rhs: impl AsRepr<i8>, ) -> Result<Option<Quotient<Self>>>

Checked integer division.

Returns an Error on overflow; Quotient::Nan if rhs == 0.

let a = RangedNonZeroI8::<-100, 10>::new::<-50>();
let b = RangedNonZeroI8::<-10, 100>::new::<50>();

assert_eq!(
    a.checked_div(2),
    Ok(Some(Quotient::Number(RangedNonZeroI8::new::<-25>()))),
);
assert_eq!(a.checked_div(0), Ok(Some(Quotient::Nan)));
assert_eq!(a.checked_div(-1), Err(Error::PosOverflow));
assert_eq!(b.checked_div(-2), Err(Error::NegOverflow));
Source

pub const fn checked_sub(self, other: impl AsRepr<i8>) -> Result<Option<Self>>

Subtract a ranged integers from another.

Returns an Error on overflow.

let a = RangedNonZeroI8::<1, 100>::new::<50>();
let b = a.checked_sub(5).unwrap().unwrap();

assert_eq!(a.checked_sub(-51), Err(Error::PosOverflow));
assert_eq!(b.get(), 45);
assert_eq!(a.checked_sub(a), Err(Error::NegOverflow));
Source

pub const fn is_negative(self) -> bool

Return true if self is negative; false if zero or positive.

assert!(!RangedNonZeroI8::<-100, 100>::new::<10>().is_negative());
assert!(RangedNonZeroI8::<-100, 100>::new::<-10>().is_negative());
Source

pub const fn is_positive(self) -> bool

Return true if self is positive; false if zero or negative.

assert!(RangedNonZeroI8::<-100, 100>::new::<10>().is_positive());
assert!(!RangedNonZeroI8::<-100, 100>::new::<-10>().is_positive());
Source

pub const fn mul_ranged<const RHS_MIN: i8, const RHS_MAX: i8, const OUTPUT_MIN: i8, const OUTPUT_MAX: i8>( self, rhs: RangedNonZeroI8<RHS_MIN, RHS_MAX>, ) -> RangedNonZeroI8<OUTPUT_MIN, OUTPUT_MAX>

Multiply two numbers together.

let a = RangedNonZeroI8::<-2, 3>::new::<1>();
let b = RangedNonZeroI8::<-1, 3>::new::<2>();
let output: RangedNonZeroI8::<-6, 9> = a.mul_ranged(b);

assert_eq!(output.get(), 2);

Does not compile:

let a = RangedNonZeroI8::<-2, 3>::new::<1>();
let b = RangedNonZeroI8::<-1, 3>::new::<2>();
let output: RangedNonZeroI8::<0, 9> = a.mul_ranged(b);

assert_eq!(output.get(), 2);
Source

pub const fn pow_ranged<const RHS_MIN: u32, const RHS_MAX: u32, const OUTPUT_MIN: i8, const OUTPUT_MAX: i8>( self, rhs: RangedU32<RHS_MIN, RHS_MAX>, ) -> RangedNonZeroI8<OUTPUT_MIN, OUTPUT_MAX>

Raise to an integer power.

let a = RangedNonZeroI8::<-1, 3>::new::<2>();
let b = RangedU32::<2, 3>::new::<2>();
let output: RangedNonZeroI8::<-1, 27> = a.pow_ranged(b);

assert_eq!(output.get(), 4);

Does not compile:

let a = RangedNonZeroI8::<1, 3>::new::<2>();
let b = RangedU32::<2, 3>::new::<2>();
let output: RangedNonZeroI8::<0, 27> = a.pow_ranged(b);

assert_eq!(output.get(), 4);

Trait Implementations§

Source§

impl<const MIN: i8, const MAX: i8> Binary for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> Clone for RangedNonZeroI8<MIN, MAX>

Source§

fn clone(&self) -> RangedNonZeroI8<MIN, MAX>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const MIN: i8, const MAX: i8> Debug for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> Display for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> Div<RangedNonZeroI8<MIN, MAX>> for RangedI8<MIN, MAX>

Source§

type Output = RangedI8<MIN, MAX>

The resulting type after applying the / operator.
Source§

fn div(self, other: RangedNonZeroI8<MIN, MAX>) -> Self

Performs the / operation. Read more
Source§

impl<const VAL: i8> Extend<RangedNonZeroI8<VAL, VAL>> for UnitNonZeroI8<VAL>

Source§

fn extend<T: IntoIterator<Item = Self>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<const VAL: i8> From<RangedNonZeroI8<VAL, VAL>> for ()

Source§

fn from(_: UnitNonZeroI8<VAL>)

Converts to this type from the input type.
Source§

impl<const VAL: i8> FromIterator<RangedNonZeroI8<VAL, VAL>> for UnitNonZeroI8<VAL>

Source§

fn from_iter<I: IntoIterator<Item = UnitNonZeroI8<VAL>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<const MIN: i8, const MAX: i8> FromStr for RangedNonZeroI8<MIN, MAX>

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(src: &str) -> ParsingResult<Self>

Parses a string s to return a value of this type. Read more
Source§

impl<const MIN: i8, const MAX: i8> Hash for RangedNonZeroI8<MIN, MAX>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<const MIN: i8, const MAX: i8> LowerExp for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> LowerHex for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> Octal for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> Ord for RangedNonZeroI8<MIN, MAX>

Source§

fn cmp(&self, other: &RangedNonZeroI8<MIN, MAX>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const MIN: i8, const MAX: i8> PartialEq for RangedNonZeroI8<MIN, MAX>

Source§

fn eq(&self, other: &RangedNonZeroI8<MIN, MAX>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const MIN: i8, const MAX: i8> PartialOrd for RangedNonZeroI8<MIN, MAX>

Source§

fn partial_cmp(&self, other: &RangedNonZeroI8<MIN, MAX>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const MIN: i8, const MAX: i8> UpperExp for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> UpperHex for RangedNonZeroI8<MIN, MAX>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const MIN: i8, const MAX: i8> AsRepr<NonZero<i8>> for RangedNonZeroI8<MIN, MAX>

Source§

impl<const MIN: i8, const MAX: i8> AsRepr<i8> for RangedNonZeroI8<MIN, MAX>

Source§

impl<const MIN: i8, const MAX: i8> Copy for RangedNonZeroI8<MIN, MAX>

Source§

impl<const MIN: i8, const MAX: i8> Eq for RangedNonZeroI8<MIN, MAX>

Source§

impl<const MIN: i8, const MAX: i8> StructuralPartialEq for RangedNonZeroI8<MIN, MAX>

Auto Trait Implementations§

§

impl<const MIN: i8, const MAX: i8> Freeze for RangedNonZeroI8<MIN, MAX>

§

impl<const MIN: i8, const MAX: i8> RefUnwindSafe for RangedNonZeroI8<MIN, MAX>

§

impl<const MIN: i8, const MAX: i8> Send for RangedNonZeroI8<MIN, MAX>

§

impl<const MIN: i8, const MAX: i8> Sync for RangedNonZeroI8<MIN, MAX>

§

impl<const MIN: i8, const MAX: i8> Unpin for RangedNonZeroI8<MIN, MAX>

§

impl<const MIN: i8, const MAX: i8> UnwindSafe for RangedNonZeroI8<MIN, MAX>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AsRepr<[[[[T; 1]; 1]; 1]; 1]> for T

Source§

impl<T> AsRepr<[[[T; 1]; 1]; 1]> for T

Source§

impl<T> AsRepr<[[T; 1]; 1]> for T

Source§

impl<T> AsRepr<[T; 1]> for T

Source§

impl<T> AsRepr<T> for T