devela::all

Trait NumToStr

Source
pub trait NumToStr<T> {
    // Required methods
    fn to_bytes_base(self, base: T, string: &mut [u8]) -> &[u8] ;
    fn to_str_base(self, base: T, buf: &mut [u8]) -> &str ;
}
Available on crate feature fmt only.
Expand description

Converts a number into a string representation, storing it into a byte slice.

§Features

It makes use of the unsafe_str feature for faster unchecked conversion of the resulting bytes to a string slice.

Required Methods§

Source

fn to_bytes_base(self, base: T, string: &mut [u8]) -> &[u8]

Given a base for encoding and a mutable byte slice, write the number into the byte slice and return the indice where the inner string begins. The inner string can be extracted by slicing the byte slice from that indice.

§Panics

If the supplied buffer is smaller than the number of bytes needed to write the integer, this will panic. On debug builds, this function will perform a check on base 10 conversions to ensure that the input array is large enough to hold the largest possible value in digits.

§Example
use devela::text::NumToStr;
use std::io::{self, Write};

let stdout = io::stdout();
let stdout = &mut io::stdout();

// Allocate a buffer that will be reused in each iteration.
let mut buffer = [0u8; 20];

let number = 15325;
let _ = stdout.write(number.to_bytes_base(10, &mut buffer));

let number = 1241;
let _ = stdout.write(number.to_bytes_base(10, &mut buffer));

assert_eq!(12345.to_bytes_base(10, &mut buffer), b"12345");
Source

fn to_str_base(self, base: T, buf: &mut [u8]) -> &str

Convenience method for quickly getting a string from the input’s array buffer.

Implementations on Foreign Types§

Source§

impl NumToStr<i8> for i8

Source§

fn to_bytes_base(self, base: i8, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: Self, buf: &mut [u8]) -> &str

Source§

impl NumToStr<i16> for i16

Source§

fn to_bytes_base(self, base: i16, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: i16, buf: &mut [u8]) -> &str

Source§

impl NumToStr<i32> for i32

Source§

fn to_bytes_base(self, base: i32, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: i32, buf: &mut [u8]) -> &str

Source§

impl NumToStr<i64> for i64

Source§

fn to_bytes_base(self, base: i64, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: i64, buf: &mut [u8]) -> &str

Source§

impl NumToStr<isize> for isize

Source§

fn to_bytes_base(self, base: isize, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: isize, buf: &mut [u8]) -> &str

Source§

impl NumToStr<u8> for u8

Source§

fn to_bytes_base(self, base: u8, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: Self, buf: &mut [u8]) -> &str

Source§

impl NumToStr<u16> for u16

Source§

fn to_bytes_base(self, base: u16, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: u16, buf: &mut [u8]) -> &str

Source§

impl NumToStr<u32> for u32

Source§

fn to_bytes_base(self, base: u32, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: u32, buf: &mut [u8]) -> &str

Source§

impl NumToStr<u64> for u64

Source§

fn to_bytes_base(self, base: u64, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: u64, buf: &mut [u8]) -> &str

Source§

impl NumToStr<usize> for usize

Source§

fn to_bytes_base(self, base: usize, string: &mut [u8]) -> &[u8]

Source§

fn to_str_base(self, base: usize, buf: &mut [u8]) -> &str

Implementors§