Struct tor_units::Percentage

source ·
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>

source

pub fn new(value: T) -> Self

Create a new IntPercentage from the underlying percentage.

source

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 ==.
source

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>

source§

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

Returns a copy 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 + Copy + Into<f64>> Debug for Percentage<T>

source§

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

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

impl<T: Hash + Copy + Into<f64>> Hash for Percentage<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: PartialEq + Copy + Into<f64>> PartialEq for Percentage<T>

source§

fn eq(&self, other: &Percentage<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const H: i32, const L: i32> TryFrom<i32> for Percentage<BoundedInt32<H, L>>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(v: i32) -> Result<Self, Error>

Performs the conversion.
source§

impl<T: Copy + Copy + Into<f64>> Copy for Percentage<T>

source§

impl<T: Eq + Copy + Into<f64>> Eq for Percentage<T>

source§

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> 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> 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<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.