Reduction

Struct Reduction 

Source
#[repr(transparent)]
pub struct Reduction<T>(pub T);
Expand description

A newtype representing a reduction factor of (1 - T).

See the module-level documentation for more information.

Note that this type does not implement the num_traits::One trait. This is intentional as the trait requires one() to return the multiplicative identity, which for Reduction is Reduction::none(). However, having Reduction::one() return a Reduction(T::zero()) would be highly confusing.

Tuple Fields§

§0: T

Implementations§

Source§

impl<T> Reduction<T>

Source

pub const fn new(value: T) -> Self

Creates a new Reduction from the given inner value.

Source

pub fn none() -> Self
where T: Zero,

Creates a Reduction representing no reduction (0%).

This is the multiplicative identity for composition: any reduction composed with none remains unchanged.

Its multiplier is 1.

Source

pub fn full() -> Self
where T: One,

Creates a Reduction representing a full reduction (100%).

This is the absorbing element for composition: any reduction composed with full results in full.

Note that for floats, this property may not hold exactly.

Its multiplier is 0.

Source

pub fn inner(self) -> T

Consumes the Reduction and returns the inner value x.

Source§

impl<T: One + Sub<Output = T>> Reduction<T>

Source

pub fn multiplier(self) -> T

Calculates the multiplicative factor, 1 - self.0.

This is the value that is multiplied with a base value when the reduction is applied.

§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
assert_eq!(r.multiplier(), 0.75);
Source

pub fn reduce(self, value: T) -> T

Applies the reduction to a given value.

This is equivalent to value * self.multiplier().

This operation is also available through multiplication: reduction * value.

§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
assert_eq!(r.reduce(100.0), 75.0);
assert_eq!(r * 100.0, 75.0);
Source

pub fn complement(self) -> Self

Returns the complement of the reduction.

The complement of Reduction(x) is Reduction(1 - x). For example, the complement of a 25% reduction is an 75% reduction.

§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
let complement = r.complement();
assert_eq!(*complement, 0.75);
Source§

impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> Reduction<T>

Source

pub fn compose(self, other: Self) -> Self

Composes this reduction with another, returning a new Reduction.

This is equivalent to applying one reduction and then the other.

The formula for the new inner value is x + y - xy where x and y are the inner values of the two reductions.

This operation is also available through multiplication: r1 * r2.

§Example
use reduction_factor::Reduction;
let r1 = Reduction(0.20f32);
let r2 = Reduction(0.10f32);
let stacked = r1.compose(r2);
assert_eq!(*stacked, 0.28);
assert_eq!(r1 * r2, stacked); // Equivalent
Source

pub fn compose_inplace(&mut self, other: Self)
where T: Default,

In-place version of compose.

Source

pub fn pow(&self, exponent: usize) -> Self
where T: Zero,

Composes the reduction with itself exponent times.

pow(0) returns Reduction(0) the identity reduction, Reduction::none().

§Example
use reduction_factor::Reduction;
let r = Reduction(0.5f32);
// Applying a 50% reduction twice results in a 75% reduction.
let r2 = r.pow(2);
assert_eq!(*r2, 0.75);
assert_eq!(r * r, r2);

Zero exponent returns the identity reduction.

use reduction_factor::Reduction;
let r = Reduction(0.5f32);

let r0 = r.pow(0);
assert_eq!(*r0, 0.0);
assert_eq!(r0, Reduction::none());
Source

pub fn pow_nonzero(&self, exponent: NonZeroUsize) -> Self

Composes the reduction with itself exponent times for a non-zero exponent.

The main difference from Reduction::pow() is that T: Zero isn’t required.

§Example
use reduction_factor::Reduction;
use core::num::NonZeroUsize;

let r = Reduction(0.5f32);
// Applying a 50% reduction twice results in a 75% reduction.
let r2 = r.pow_nonzero(NonZeroUsize::new(2).unwrap());
assert_eq!(*r2, 0.75);
assert_eq!(r * r, r2);

Trait Implementations§

Source§

impl<T> AsMut<T> for Reduction<T>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for Reduction<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for Reduction<T>

Source§

fn clone(&self) -> Reduction<T>

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<T: Debug> Debug for Reduction<T>

Source§

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

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

impl<T: Zero> Default for Reduction<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for Reduction<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for Reduction<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: Display> Display for Reduction<T>

Source§

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

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

impl<T> From<T> for Reduction<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for Reduction<T>

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<T: One + Sub<Output = T>> Mul<T> for Reduction<T>

Source§

fn mul(self, rhs: T) -> Self::Output

The multiplication operator is overloaded to reduce the reduction to a value of type T.

Source§

type Output = T

The resulting type after applying the * operator.
Source§

impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> Mul for Reduction<T>

Source§

fn mul(self, rhs: Self) -> Self::Output

The multiplication operator is overloaded for two Reductions to perform compose.

This is the idiomatic way to compose two reductions.

Source§

type Output = Reduction<T>

The resulting type after applying the * operator.
Source§

impl<T: Default + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> MulAssign for Reduction<T>

Source§

fn mul_assign(&mut self, rhs: Self)

The *= operator is overloaded for two Reductions to perform compose_inplace.

Source§

impl<T: Ord> Ord for Reduction<T>

Source§

fn cmp(&self, other: &Reduction<T>) -> 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<T: PartialEq> PartialEq for Reduction<T>

Source§

fn eq(&self, other: &Reduction<T>) -> 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<T: PartialOrd> PartialOrd for Reduction<T>

Source§

fn partial_cmp(&self, other: &Reduction<T>) -> 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<T: Copy> Copy for Reduction<T>

Source§

impl<T: Eq> Eq for Reduction<T>

Source§

impl<T> StructuralPartialEq for Reduction<T>

Auto Trait Implementations§

§

impl<T> Freeze for Reduction<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Reduction<T>
where T: RefUnwindSafe,

§

impl<T> Send for Reduction<T>
where T: Send,

§

impl<T> Sync for Reduction<T>
where T: Sync,

§

impl<T> Unpin for Reduction<T>
where T: Unpin,

§

impl<T> UnwindSafe for Reduction<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.