Struct tor_units::Percentage
source · [−]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
sourceimpl<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
sourceimpl<T: Clone + Copy + Into<f64>> Clone for Percentage<T>
impl<T: Clone + Copy + Into<f64>> Clone for Percentage<T>
sourcefn clone(&self) -> Percentage<T>
fn clone(&self) -> Percentage<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T: PartialEq + Copy + Into<f64>> PartialEq<Percentage<T>> for Percentage<T>
impl<T: PartialEq + Copy + Into<f64>> PartialEq<Percentage<T>> for Percentage<T>
sourcefn eq(&self, other: &Percentage<T>) -> bool
fn eq(&self, other: &Percentage<T>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &Percentage<T>) -> bool
fn ne(&self, other: &Percentage<T>) -> bool
This method tests for !=
.
sourceimpl<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>> StructuralEq for Percentage<T>
impl<T: Copy + Into<f64>> StructuralPartialEq for Percentage<T>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more