Type Alias NiceU16

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

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

That’s it!

§Examples

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

§Traits

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

  • From<u16>
  • From<Option<u16>>
  • From<NonZeroU16>
  • From<Option<NonZeroU16>>

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

Aliased Type§

struct NiceU16 { /* private fields */ }

Implementations§

Source§

impl NiceU16

Source

pub const MIN: Self

§Minimum Value.

The nice equivalent of u16::MIN.

use dactyl::NiceU16;

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

assert_eq!(
    NiceU16::MIN,
    NiceU16::from(u16::MIN),
);
Source

pub const MAX: Self

§Maximum Value.

The nice equivalent of u16::MAX.

use dactyl::NiceU16;

assert_eq!(
    NiceU16::MAX.as_str(),
    "65,535"
);

assert_eq!(
    NiceU16::MAX,
    NiceU16::from(u16::MAX),
);
Source§

impl NiceU16

Source

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

§Examples
use dactyl::NiceU16;

let num = NiceU16::from(31415_u16);
assert_eq!(num.as_str(), "31,415");

let num = NiceU16::with_separator(31415_u16, b'_');
assert_eq!(num.as_str(), "31_415");
§Panics

This method will panic if the separator is invalid ASCII.

Source

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

§Replace.

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

§Examples.
use dactyl::NiceU16;

let mut num = NiceU16::from(123_u16);
assert_eq!(num.as_str(), "123");

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

Note that custom separators, if any, are preserved.

use dactyl::NiceU16;

let mut num = NiceU16::with_separator(123_u16, b'_');
assert_eq!(num.as_str(), "123");

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

Trait Implementations§

Source§

impl Default for NiceU16

Source§

fn default() -> Self

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

impl From<NonZero<u16>> for NiceU16

Source§

fn from(num: NonZeroU16) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for NiceU16

Source§

fn from(num: u16) -> Self

Converts to this type from the input type.