dactyl

Type Alias NiceU32

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

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

That’s it!

§Examples

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

§Traits

Rustdoc doesn’t do a good job at documenting type alias implementations, but NiceU32 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 NiceU32 with:

  • From<u32>
  • From<Option<u32>>
  • From<NonZeroU32>
  • From<Option<NonZeroU32>>

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

Aliased Type§

struct NiceU32 { /* private fields */ }

Implementations§

Source§

impl NiceU32

Source

pub const MIN: Self = _

§Minimum Value.

The nice equivalent of u32::MIN.

use dactyl::NiceU32;

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

assert_eq!(
    NiceU32::MIN,
    NiceU32::from(u32::MIN),
);
Source

pub const MAX: Self = _

§Maximum Value.

The nice equivalent of u32::MAX.

use dactyl::NiceU32;

assert_eq!(
    NiceU32::MAX.as_str(),
    "4,294,967,295"
);

assert_eq!(
    NiceU32::MAX,
    NiceU32::from(u32::MAX),
);
Source§

impl NiceU32

Source

pub fn with_separator(num: u32, 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 [NiceU32::from] instead.

§Examples
use dactyl::NiceU32;

let num = NiceU32::from(3141592653_u32);
assert_eq!(num.as_str(), "3,141,592,653");

let num = NiceU32::with_separator(3141592653_u32, b'_');
assert_eq!(num.as_str(), "3_141_592_653");
§Panics

This method will panic if the separator is invalid ASCII.

Source

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

§Replace.

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

§Examples.
use dactyl::NiceU32;

let mut num = NiceU32::from(3141592653_u32);
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::NiceU32;

let mut num = NiceU32::with_separator(3141592653_u32, 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 NiceU32

Source§

fn default() -> Self

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

impl From<NonZero<u32>> for NiceU32

Source§

fn from(num: NonZeroU32) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for NiceU32

Source§

fn from(num: u32) -> Self

Converts to this type from the input type.