Skip to main content

PrimitiveNumBuffer

Trait PrimitiveNumBuffer 

Source
pub trait PrimitiveNumBuffer:
    'static
    + Sealed
    + Debug
    + Send
    + Sized
    + Sync
    + Unpin
    + RefUnwindSafe
    + UnwindSafe {
    // Required method
    fn new() -> Self;
}
Expand description

Trait for NumBuffer<T> for the decimal formatting of a primitive integer type.

In particular, this is used as a bound for the associated type PrimitiveInteger::NumBuffer, passed as an argument to the format_into method. The main use for this trait is just to create a buffer with new.

This trait is sealed with a private trait to prevent downstream implementations, so we may continue to expand along with the standard library without worrying about breaking changes for implementors.

§Examples

use num_primitive::{PrimitiveInteger, PrimitiveNumBuffer};

fn check_format_into<T: PrimitiveInteger>(x: T) {
    assert!(size_of::<T::NumBuffer>() > size_of::<T>());

    let mut buf = T::NumBuffer::new();
    assert_eq!(x.format_into(&mut buf), x.to_string());

    // Note that the buffer can be reused for multiple calls.
    assert_eq!(T::default().format_into(&mut buf), "0");
    assert_eq!(T::as_from(1).format_into(&mut buf), "1");
    assert_eq!(T::MIN.format_into(&mut buf), T::MIN.to_string());
    assert_eq!(T::MAX.format_into(&mut buf), T::MAX.to_string());
}

check_format_into(123_u64);
check_format_into(-42_i32);

assert!(size_of::<<u64 as PrimitiveInteger>::NumBuffer>()
      > size_of::<<i32 as PrimitiveInteger>::NumBuffer>());

Required Methods§

Source

fn new() -> Self

Creates a buffer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl PrimitiveNumBuffer for NumBuffer<i8>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<i16>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<i32>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<i64>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<i128>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<isize>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<u8>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<u16>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<u32>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<u64>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<u128>

Source§

fn new() -> Self

See the inherent new method.

Source§

impl PrimitiveNumBuffer for NumBuffer<usize>

Source§

fn new() -> Self

See the inherent new method.

Implementors§