Type Alias NiceU64

Source
pub type NiceU64 = NiceWrapper<SIZE>;
Expand description

NiceU64 provides a quick way to convert a u64 into a formatted byte string for e.g. printing. Commas are added for every thousand.

That’s it!

§Examples

use dactyl::NiceU64;
assert_eq!(
    NiceU64::from(33231_u64).as_str(),
    "33,231"
);

§Traits

Rustdoc doesn’t do a good job at documenting type alias implementations, but NiceU64 has a bunch, including:

  • AsRef<[u8]>
  • AsRef<str>
  • Borrow<[u8]>
  • Borrow<str>
  • Clone
  • Copy
  • Default
  • Deref<Target=[u8]>
  • Display
  • Eq / PartialEq
  • Hash
  • Ord / PartialOrd

You can instantiate a NiceU64 with:

  • From<u64>
  • From<Option<u64>>
  • From<NonZeroU64>
  • From<Option<NonZeroU64>>
  • From<usize>
  • From<Option<usize>>
  • From<NonZeroUsize>
  • From<Option<NonZeroUsize>>

When converting from a None, the result will be equivalent to zero.

For targets with 128-bit pointers, usize values cannot exceed u64::MAX or a panic will ensue.

Aliased Type§

struct NiceU64 { /* private fields */ }

Implementations§

Source§

impl NiceU64

Source

pub const MIN: Self

§Minimum Value.

The nice equivalent of u64::MIN.

use dactyl::NiceU64;

assert_eq!(
    NiceU64::MIN.as_str(),
    "0"
);

assert_eq!(
    NiceU64::MIN,
    NiceU64::from(u64::MIN),
);
Source

pub const MAX: Self

§Maximum Value.

The nice equivalent of u64::MAX.

use dactyl::NiceU64;

assert_eq!(
    NiceU64::MAX.as_str(),
    "18,446,744,073,709,551,615"
);

assert_eq!(
    NiceU64::MAX,
    NiceU64::from(u64::MAX),
);
Source§

impl NiceU64

Source

pub fn with_separator(num: u64, sep: u8) -> Self

§New Instance w/ Custom Separator.

Create a new instance, defining any arbitrary ASCII byte as the thousands separator.

If you’re good with commas, just use [NiceU64::from] instead.

§Examples
use dactyl::NiceU64;

let num = NiceU64::from(3141592653589793238_u64);
assert_eq!(num.as_str(), "3,141,592,653,589,793,238");

let num = NiceU64::with_separator(3141592653589793238_u64, b'_');
assert_eq!(num.as_str(), "3_141_592_653_589_793_238");
§Panics

This method will panic if the separator is invalid ASCII.

Source

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

§Replace.

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

§Examples.
use dactyl::NiceU64;

let mut num = NiceU64::from(3141592653_u64);
assert_eq!(num.as_str(), "3,141,592,653");

num.replace(12345);
assert_eq!(num.as_str(), "12,345");

Note that custom separators, if any, are preserved.

use dactyl::NiceU64;

let mut num = NiceU64::with_separator(3141592653_u64, b'_');
assert_eq!(num.as_str(), "3_141_592_653");

num.replace(12345);
assert_eq!(num.as_str(), "12_345");

Trait Implementations§

Source§

impl Default for NiceU64

Source§

fn default() -> Self

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

impl From<NonZero<u64>> for NiceU64

Source§

fn from(num: NonZeroU64) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<usize>> for NiceU64

Source§

fn from(num: NonZeroUsize) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for NiceU64

Source§

fn from(num: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for NiceU64

Source§

fn from(num: usize) -> Self

Converts to this type from the input type.