Trait Unsigned

Source
pub trait Unsigned: Unsigned {
    type Signed: TryFrom<Self> + UnsignedAbs<Unsigned = Self> + CheckedNeg;

    // Required method
    fn diff(self, other: Self) -> Self;

    // Provided methods
    fn to_signed(&self) -> Result<Self::Signed>
       where Self: Clone { ... }
    fn to_signed_with_sign(&self, negative: bool) -> Result<Self::Signed>
       where Self: Clone,
             Self::Signed: CheckedSub { ... }
    fn to_opposite_signed(&self) -> Result<Self::Signed>
       where Self: Clone,
             Self::Signed: CheckedSub { ... }
    fn checked_signed_sub(self, other: Self) -> Result<Self::Signed>
       where Self: Ord + Clone,
             Self::Signed: CheckedSub { ... }
    fn checked_add_with_signed(&self, other: &Self::Signed) -> Option<Self>
       where Self: CheckedAdd + CheckedSub { ... }
    fn checked_sub_with_signed(&self, other: &Self::Signed) -> Option<Self>
       where Self: CheckedAdd + CheckedSub { ... }
    fn checked_mul_with_signed(
        &self,
        other: &Self::Signed,
    ) -> Option<Self::Signed>
       where Self: CheckedMul { ... }
    fn as_divisor_to_round_up_magnitude_div(
        &self,
        dividend: &Self::Signed,
    ) -> Option<Self::Signed>
       where Self: Clone,
             Self::Signed: CheckedSub + CheckedAdd + CheckedDiv { ... }
    fn checked_round_up_div(&self, divisor: &Self) -> Option<Self>
       where Self: CheckedAdd + CheckedSub + Clone + CheckedDiv { ... }
    fn bound_magnitude(
        value: &Self::Signed,
        min: &Self,
        max: &Self,
    ) -> Result<Self::Signed>
       where Self: Ord + Clone,
             Self::Signed: Clone + CheckedSub { ... }
}
Expand description

Unsigned value that cannot be negative.

Required Associated Types§

Source

type Signed: TryFrom<Self> + UnsignedAbs<Unsigned = Self> + CheckedNeg

The signed type.

Required Methods§

Source

fn diff(self, other: Self) -> Self

Compute the absolute difference of two values.

Provided Methods§

Source

fn to_signed(&self) -> Result<Self::Signed>
where Self: Clone,

Convert to a signed value

Source

fn to_signed_with_sign(&self, negative: bool) -> Result<Self::Signed>
where Self: Clone, Self::Signed: CheckedSub,

Convert to a signed value with the given sign.

Source

fn to_opposite_signed(&self) -> Result<Self::Signed>
where Self: Clone, Self::Signed: CheckedSub,

Convert to opposite signed.

Source

fn checked_signed_sub(self, other: Self) -> Result<Self::Signed>
where Self: Ord + Clone, Self::Signed: CheckedSub,

Compute signed self - other.

Source

fn checked_add_with_signed(&self, other: &Self::Signed) -> Option<Self>
where Self: CheckedAdd + CheckedSub,

Checked signed add.

Source

fn checked_sub_with_signed(&self, other: &Self::Signed) -> Option<Self>
where Self: CheckedAdd + CheckedSub,

Checked signed sub.

Source

fn checked_mul_with_signed(&self, other: &Self::Signed) -> Option<Self::Signed>
where Self: CheckedMul,

Checked signed mul.

Source

fn as_divisor_to_round_up_magnitude_div( &self, dividend: &Self::Signed, ) -> Option<Self::Signed>
where Self: Clone, Self::Signed: CheckedSub + CheckedAdd + CheckedDiv,

As divisor to checked divide other and round up magnitude.

Source

fn checked_round_up_div(&self, divisor: &Self) -> Option<Self>

Checked round up division.

Source

fn bound_magnitude( value: &Self::Signed, min: &Self, max: &Self, ) -> Result<Self::Signed>
where Self: Ord + Clone, Self::Signed: Clone + CheckedSub,

Bound the magnitude of a signed value.

§Errors

Return error if

  • min > max
  • min is greater than the maximum value representable by Self::Signed
§Examples

This method can be used to bound the magnitude of a signed value:

let a = -123i64;
// Original value within bounds
assert_eq!(Unsigned::bound_magnitude(&a, &0, &124u64).unwrap(), -123);
// Value clamped to max magnitude
assert_eq!(Unsigned::bound_magnitude(&a, &0, &120u64).unwrap(), -120);
// Value clamped to min magnitude
assert_eq!(Unsigned::bound_magnitude(&a, &124, &256u64).unwrap(), -124);

let b = 123i64;
// Original value within bounds
assert_eq!(Unsigned::bound_magnitude(&b, &0, &124u64).unwrap(), 123);
// Value clamped to max magnitude
assert_eq!(Unsigned::bound_magnitude(&b, &0, &120u64).unwrap(), 120);
// Value clamped to min magnitude
assert_eq!(Unsigned::bound_magnitude(&b, &124, &256u64).unwrap(), 124);

Returns an error if min > max:

let result = Unsigned::bound_magnitude(&0, &1u64, &0);
assert!(result.is_err());

Returns an error if min is greater than the maximum value representable by Self::Signed:

let result = Unsigned::bound_magnitude(&0, &(u64::MAX / 2 + 1), &u64::MAX);
assert!(result.is_err());

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.

Implementations on Foreign Types§

Source§

impl Unsigned for u64

Source§

type Signed = i64

Source§

fn diff(self, other: Self) -> Self

Source§

impl Unsigned for u128

Source§

type Signed = i128

Source§

fn diff(self, other: Self) -> Self

Implementors§