pub struct Percent(/* private fields */);
Expand description
Re-export top-level arithmetic stuff. A fixed point representation of a number in the range [0, 1].
Percent
Implementations§
Source§impl Percent
impl Percent
Sourcepub const fn from_parts(parts: u8) -> Percent
pub const fn from_parts(parts: u8) -> Percent
From an explicitly defined number of parts per maximum of the type.
Sourcepub const fn from_percent(x: u8) -> Percent
pub const fn from_percent(x: u8) -> Percent
Converts a percent into Self
. Equal to x / 100
.
This can be created at compile time.
Sourcepub const fn one() -> Percent
pub const fn one() -> Percent
See PerThing::one
Sourcepub fn is_one(&self) -> bool
pub fn is_one(&self) -> bool
See PerThing::is_one
.
Sourcepub const fn zero() -> Percent
pub const fn zero() -> Percent
See PerThing::zero
.
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
See PerThing::is_zero
.
Sourcepub const fn deconstruct(self) -> u8
pub const fn deconstruct(self) -> u8
Sourcepub fn square(self) -> Percent
pub fn square(self) -> Percent
See PerThing::square
.
Sourcepub fn from_fraction(x: f64) -> Percent
pub fn from_fraction(x: f64) -> Percent
Sourcepub fn from_rational_approximation<N>(p: N, q: N) -> Percent
pub fn from_rational_approximation<N>(p: N, q: N) -> Percent
Sourcepub fn mul_floor<N>(self, b: N) -> N
pub fn mul_floor<N>(self, b: N) -> N
See PerThing::mul_floor
.
Sourcepub fn mul_ceil<N>(self, b: N) -> N
pub fn mul_ceil<N>(self, b: N) -> N
See PerThing::mul_ceil
.
Sourcepub fn saturating_reciprocal_mul<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul<N>(self, b: N) -> N
Sourcepub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
Sourcepub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
Trait Implementations§
Source§impl CompactAs for Percent
Implementation makes any compact encoding of PerThing::Inner
valid,
when decoding it will saturate up to PerThing::ACCURACY
.
impl CompactAs for Percent
Implementation makes any compact encoding of PerThing::Inner
valid,
when decoding it will saturate up to PerThing::ACCURACY
.
Source§impl Decode for Percent
impl Decode for Percent
Source§impl<'de> Deserialize<'de> for Percent
impl<'de> Deserialize<'de> for Percent
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Percent, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Percent, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Encode for Percent
impl Encode for Percent
Source§fn encode_to<__CodecOutputEdqy>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy,
)
fn encode_to<__CodecOutputEdqy>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy, )
Source§fn using_encoded<R, F>(&self, f: F) -> R
fn using_encoded<R, F>(&self, f: F) -> R
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
Source§impl<N> Mul<N> for Percent
Non-overflow multiplication.
impl<N> Mul<N> for Percent
Non-overflow multiplication.
This is tailored to be used with a balance type.
Source§impl Ord for Percent
impl Ord for Percent
Source§impl PartialOrd for Percent
impl PartialOrd for Percent
Source§impl PerThing for Percent
impl PerThing for Percent
Source§fn deconstruct(self) -> <Percent as PerThing>::Inner
fn deconstruct(self) -> <Percent as PerThing>::Inner
Consume self and return the number of parts per thing.
Source§fn from_parts(parts: <Percent as PerThing>::Inner) -> Percent
fn from_parts(parts: <Percent as PerThing>::Inner) -> Percent
Build this type from a number of parts per thing.
Source§fn from_fraction(x: f64) -> Percent
fn from_fraction(x: f64) -> Percent
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
Source§const ACCURACY: <Percent as PerThing>::Inner = {transmute(0x64): <arithmetic::Percent as arithmetic::PerThing>::Inner}
const ACCURACY: <Percent as PerThing>::Inner = {transmute(0x64): <arithmetic::Percent as arithmetic::PerThing>::Inner}
Source§type Upper = u16
type Upper = u16
Self::Inner
, used to avoid overflow in some computations.
It must be able to compute ACCURACY^2
.Source§fn from_rational_approximation<N>(p: N, q: N) -> Percent
fn from_rational_approximation<N>(p: N, q: N) -> Percent
p/q
into a per-thing fraction. This will never overflow. Read moreSource§fn from_percent(x: Self::Inner) -> Self
fn from_percent(x: Self::Inner) -> Self
Self::from_parts(x * Self::ACCURACY / 100)
but more accurate and can cope with potential type overflows.Source§fn mul_floor<N>(self, b: N) -> N
fn mul_floor<N>(self, b: N) -> N
Mul
rounds to the
nearest whole number. Read moreSource§fn mul_ceil<N>(self, b: N) -> N
fn mul_ceil<N>(self, b: N) -> N
Mul
rounds to the nearest whole number. Read moreSource§fn saturating_reciprocal_mul<N>(self, b: N) -> N
fn saturating_reciprocal_mul<N>(self, b: N) -> N
self
. The result is rounded to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
self
. The result is rounded down to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
self
. The result is rounded up to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§impl Saturating for Percent
impl Saturating for Percent
Source§fn saturating_add(self, rhs: Percent) -> Percent
fn saturating_add(self, rhs: Percent) -> Percent
Saturating addition. Compute self + rhs
, saturating at the numeric bounds instead of
overflowing. This operation is lossless if it does not saturate.
Source§fn saturating_sub(self, rhs: Percent) -> Percent
fn saturating_sub(self, rhs: Percent) -> Percent
Saturating subtraction. Compute self - rhs
, saturating at the numeric bounds instead of
overflowing. This operation is lossless if it does not saturate.
Source§fn saturating_mul(self, rhs: Percent) -> Percent
fn saturating_mul(self, rhs: Percent) -> Percent
Saturating multiply. Compute self * rhs
, saturating at the numeric bounds instead of
overflowing. This operation is lossy.
Source§fn saturating_pow(self, exp: usize) -> Percent
fn saturating_pow(self, exp: usize) -> Percent
Saturating exponentiation. Computes self.pow(exp)
, saturating at the numeric
bounds instead of overflowing. This operation is lossy.
Source§impl Serialize for Percent
impl Serialize for Percent
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Copy for Percent
impl EncodeLike for Percent
impl Eq for Percent
impl StructuralPartialEq for Percent
Auto Trait Implementations§
impl Freeze for Percent
impl RefUnwindSafe for Percent
impl Send for Percent
impl Sync for Percent
impl Unpin for Percent
impl UnwindSafe for Percent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> HasCompact for T
impl<T> HasCompact for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
Source§impl<T> LowerBounded for Twhere
T: Bounded,
impl<T> LowerBounded for Twhere
T: Bounded,
Source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
Source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
Source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T
. Read moreSource§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
Source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.Source§impl<T, S> UniqueSaturatedFrom<T> for S
impl<T, S> UniqueSaturatedFrom<T> for S
Source§fn unique_saturated_from(t: T) -> S
fn unique_saturated_from(t: T) -> S
T
into an equivalent instance of Self
.Source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
Source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.