Struct tor_units::Percentage[][src]

pub struct Percentage<T: Copy + Into<f64>> { /* fields omitted */ }
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

Create a new IntPercentage from the underlying percentage.

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.