Skip to main content

NicePercent

Struct NicePercent 

Source
pub struct NicePercent { /* private fields */ }
Expand description

§Nice Percent.

This struct can be used to quickly and efficiently stringify a “percent” — a float between 0.0..=1.0 — to a fixed precision of hundredths.

Values outside that range are “clamped” to make them make sense, percentage-wise. (NaN and negative values are treated like zero; large positives like one.)

§Examples

use dactyl::NicePercent;

// From a ready-made float:
assert_eq!(
    NicePercent::from(0.55012345_f32).as_str(),
    "55.01%",
);

// From separate "done" and "total" integers.
assert_eq!(
    NicePercent::from((20_usize, 800_usize)).as_str(),
    "2.50%",
);

// From weird shit.
assert_eq!(
    NicePercent::from(-0.55012345_f32).as_str(), // Negative.
    "0.00%",
);
assert_eq!(
    NicePercent::from(f64::NAN).as_str(), // Not even a number!
    "0.00%",
);
assert_eq!(
    NicePercent::from(f64::NEG_INFINITY).as_str(), // Negative.
    "0.00%",
);
assert_eq!(
    NicePercent::from(55.012345_f32).as_str(), // Wrong scale.
    "100.00%",
);
assert_eq!(
    NicePercent::from(f64::INFINITY).as_str(), // So much more than one.
    "100.00%",
);

Implementations§

Source§

impl NicePercent

Source

pub const MIN: Self

§Minimum Value.

The nice equivalent of 0.0.

§Examples
use dactyl::NicePercent;

assert_eq!(
    NicePercent::MIN.as_str(),
    "0.00%",
);
Source

pub const MAX: Self

§Maximum Value.

The nice equivalent of 1.0.

§Examples
use dactyl::NicePercent;

assert_eq!(
    NicePercent::MAX.as_str(),
    "100.00%",
);
Source§

impl NicePercent

Source

pub const fn as_bytes(&self) -> &[u8]

§As Byte Slice.

Return the value as a byte slice.

§Examples
use dactyl::NicePercent;

assert_eq!(
    NicePercent::from(1.0_f32).as_bytes(),
    b"100.00%",
);
Source

pub const fn as_str(&self) -> &str

§As String Slice.

Return the value as a string slice.

§Examples
use dactyl::NicePercent;

assert_eq!(
    NicePercent::from(1.0_f32).as_str(),
    "100.00%",
);
Source

pub const fn is_empty(&self) -> bool

§Is Empty?

No! Haha. But for consistency, this method exists.

Source

pub const fn len(&self) -> usize

§Length.

Return the length of the nice byte/string representation.

Note this will never be zero.

§Examples
use dactyl::NicePercent;

let nice = NicePercent::MAX;
assert_eq!(
    nice.len(),
    nice.as_str().len(),
);
Source§

impl NicePercent

Source

pub fn replace(&mut self, num: f32)

§Replace.

Reuse the backing storage behind self to hold a new nice percent.

§Examples.
use dactyl::NicePercent;

let mut num = NicePercent::from(0.85);
assert_eq!(num.as_str(), "85.00%");

num.replace(0.334);
assert_eq!(num.as_str(), "33.40%");

Trait Implementations§

Source§

impl AsRef<[u8]> for NicePercent

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<str> for NicePercent

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<str> for NicePercent

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Clone for NicePercent

Source§

fn clone(&self) -> NicePercent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for NicePercent

Source§

impl Debug for NicePercent

Source§

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

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

impl Default for NicePercent

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for NicePercent

Source§

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

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

impl Eq for NicePercent

Source§

impl From<(i8, i8)> for NicePercent

Source§

fn from((e, d): (i8, i8)) -> Self

§Percent From i8/i8.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_i8 for the nitty-gritty.

Source§

impl From<(i16, i16)> for NicePercent

Source§

fn from((e, d): (i16, i16)) -> Self

§Percent From i16/i16.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_i16 for the nitty-gritty.

Source§

impl From<(i32, i32)> for NicePercent

Source§

fn from((e, d): (i32, i32)) -> Self

§Percent From i32/i32.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_i32 for the nitty-gritty.

Source§

impl From<(i64, i64)> for NicePercent

Source§

fn from((e, d): (i64, i64)) -> Self

§Percent From i64/i64.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_i64 for the nitty-gritty.

Source§

impl From<(i128, i128)> for NicePercent

Source§

fn from((e, d): (i128, i128)) -> Self

§Percent From i128/i128.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_i128 for the nitty-gritty.

Source§

impl From<(isize, isize)> for NicePercent

Source§

fn from((e, d): (isize, isize)) -> Self

§Percent From isize/isize.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_isize for the nitty-gritty.

Source§

impl From<(u8, u8)> for NicePercent

Source§

fn from((e, d): (u8, u8)) -> Self

§Percent From u8/u8.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_u8 for the nitty-gritty.

Source§

impl From<(u16, u16)> for NicePercent

Source§

fn from((e, d): (u16, u16)) -> Self

§Percent From u16/u16.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_u16 for the nitty-gritty.

Source§

impl From<(u32, u32)> for NicePercent

Source§

fn from((e, d): (u32, u32)) -> Self

§Percent From u32/u32.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_u32 for the nitty-gritty.

Source§

impl From<(u64, u64)> for NicePercent

Source§

fn from((e, d): (u64, u64)) -> Self

§Percent From u64/u64.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_u64 for the nitty-gritty.

Source§

impl From<(u128, u128)> for NicePercent

Source§

fn from((e, d): (u128, u128)) -> Self

§Percent From u128/u128.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_u128 for the nitty-gritty.

Source§

impl From<(usize, usize)> for NicePercent

Source§

fn from((e, d): (usize, usize)) -> Self

§Percent From usize/usize.

Create a NicePercent from the division of two integers — e.g. a “done” and a “total” — leveraging NiceFloat::div_usize for the nitty-gritty.

Source§

impl From<NicePercent> for String

Source§

fn from(src: NicePercent) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for NicePercent

Source§

fn from(num: f32) -> Self

§Percent From f32.
Source§

impl From<f64> for NicePercent

Source§

fn from(num: f64) -> Self

§Percent From f64.
Source§

impl Hash for NicePercent

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 Ord for NicePercent

Source§

fn cmp(&self, rhs: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NicePercent

Source§

fn eq(&self, rhs: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NicePercent

Source§

fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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

Source§

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.