FromAscii

Trait FromAscii 

Source
pub trait FromAscii: Sized {
    // Required method
    fn bytes_to_int(s: &[u8]) -> Result<Self, ParseIntErr>;

    // Provided method
    fn atoi(s: impl AsRef<[u8]>) -> Result<Self, ParseIntErr> { ... }
}
Expand description

This trait converts bytes to integers, and is implemented on all integer types, except u128 and i128.

The most important method on this trait is FromAscii::atoi, which can be called in a function-like style. As argument, it takes anything that implements AsRef<[u8]>. The return type is a Result, indicating whether the convertion succeeded or failed

Required Methods§

Provided Methods§

Source

fn atoi(s: impl AsRef<[u8]>) -> Result<Self, ParseIntErr>

The function performing the conversion from a byteslice to a number. It takes anything that can be transformed into a byte-slice. An empty slice returns the number 0.

§Examples
use byte_num::{
    from_ascii::FromAscii,
    error::ParseIntErr,
};

fn main() {
    assert_eq!(u32::atoi("1928"), Ok(1928));
    assert_eq!(u32::atoi("12e3"), Err(ParseIntErr::with_byte(b'e')));
}
§Safety

It should be noted that trying to convert a slice that does not fit in the chosen integer type, wraps around. For example:

use byte_num::from_ascii::FromAscii;

fn main () {
    let n = u8::atoi("256");
    assert_eq!(n, Ok(0));
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromAscii for i8

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for i16

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for i32

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for i64

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for isize

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for u8

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for u16

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for u32

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for u64

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Source§

impl FromAscii for usize

Source§

fn bytes_to_int(bytes: &[u8]) -> Result<Self, ParseIntErr>

Implementors§