pub struct Percentage<T: Copy + Into<f64>> { /* private fields */ }
Expand description
A percentage value represented as a number.
This type wraps an underlying numeric type, and ensures that callers are clear whether they want a fraction, or a percentage.
Implementations§
Source§impl<T: Copy + Into<f64>> Percentage<T>
impl<T: Copy + Into<f64>> Percentage<T>
Sourcepub fn as_fraction(self) -> f64
pub fn as_fraction(self) -> f64
Return this value as a (possibly improper) fraction.
use tor_units::Percentage;
let pct_200 = Percentage::<u8>::new(200);
let pct_100 = Percentage::<u8>::new(100);
let pct_50 = Percentage::<u8>::new(50);
assert_eq!(pct_200.as_fraction(), 2.0);
assert_eq!(pct_100.as_fraction(), 1.0);
assert_eq!(pct_50.as_fraction(), 0.5);
// Note: don't actually compare f64 with ==.
Sourcepub fn as_percent(self) -> T
pub fn as_percent(self) -> T
Return this value as a percentage.
use tor_units::Percentage;
let pct_200 = Percentage::<u8>::new(200);
let pct_100 = Percentage::<u8>::new(100);
let pct_50 = Percentage::<u8>::new(50);
assert_eq!(pct_200.as_percent(), 200);
assert_eq!(pct_100.as_percent(), 100);
assert_eq!(pct_50.as_percent(), 50);
Trait Implementations§
Source§impl<T: Clone + Copy + Into<f64>> Clone for Percentage<T>
impl<T: Clone + Copy + Into<f64>> Clone for Percentage<T>
Source§fn clone(&self) -> Percentage<T>
fn clone(&self) -> Percentage<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>>
impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>>
impl<T: Copy + Copy + Into<f64>> Copy for Percentage<T>
impl<T: Eq + Copy + Into<f64>> Eq for Percentage<T>
impl<T: Copy + Into<f64>> StructuralPartialEq for Percentage<T>
Auto Trait Implementations§
impl<T> Freeze for Percentage<T>where
T: Freeze,
impl<T> RefUnwindSafe for Percentage<T>where
T: RefUnwindSafe,
impl<T> Send for Percentage<T>where
T: Send,
impl<T> Sync for Percentage<T>where
T: Sync,
impl<T> Unpin for Percentage<T>where
T: Unpin,
impl<T> UnwindSafe for Percentage<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.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>
Converts
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>
Converts
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> Pointable for T
impl<T> Pointable for T
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
impl<T> ErasedDestructor for Twhere
T: 'static,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.