ascii

Trait AsAsciiStr

Source
pub trait AsAsciiStr {
    // Required methods
    fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
       where R: SliceIndex<[Self::Inner], Output = [Self::Inner]>;
    unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr;

    // Provided methods
    fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> { ... }
    fn get_ascii(&self, index: usize) -> Option<AsciiChar> { ... }
}
Expand description

Convert slices of bytes or AsciiChar to AsciiStr.

Required Methods§

Source

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[Self::Inner], Output = [Self::Inner]>,

Convert a subslice to an ASCII slice.

§Errors

Returns Err if the range is out of bounds or if not all bytes in the slice are ASCII. The value in the error will be the index of the first non-ASCII byte or the end of the slice.

§Examples
use ascii::AsAsciiStr;
assert!("'zoä'".slice_ascii(..3).is_ok());
assert!("'zoä'".slice_ascii(0..4).is_err());
assert!("'zoä'".slice_ascii(5..=5).is_ok());
assert!("'zoä'".slice_ascii(4..).is_err());
assert!(b"\r\n".slice_ascii(..).is_ok());
Source

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Convert to an ASCII slice without checking for non-ASCII characters.

§Safety

Calling this function when self contains non-ascii characters is undefined behavior.

§Examples

Provided Methods§

Source

fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>

Convert to an ASCII slice.

§Errors

Returns Err if not all bytes are valid ascii values.

§Example
use ascii::{AsAsciiStr, AsciiChar};
assert!("ASCII".as_ascii_str().is_ok());
assert!(b"\r\n".as_ascii_str().is_ok());
assert!("'zoä'".as_ascii_str().is_err());
assert!(b"\xff".as_ascii_str().is_err());
assert!([AsciiChar::C][..].as_ascii_str().is_ok()); // infallible
Source

fn get_ascii(&self, index: usize) -> Option<AsciiChar>

Get a single ASCII character from the slice.

Returns None if the index is out of bounds or the byte is not ASCII.

§Examples
use ascii::{AsAsciiStr, AsciiChar};
assert_eq!("'zoä'".get_ascii(4), None);
assert_eq!("'zoä'".get_ascii(5), Some(AsciiChar::Apostrophe));
assert_eq!("'zoä'".get_ascii(6), None);

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 AsAsciiStr for str

Source§

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[u8], Output = [u8]>,

Source§

fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>

Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Source§

impl AsAsciiStr for CStr

Note that the trailing null byte will be removed in the conversion.

Source§

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[u8], Output = [u8]>,

Source§

fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>

Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Source§

impl AsAsciiStr for [AsciiChar]

Source§

impl AsAsciiStr for [u8]

Source§

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[u8], Output = [u8]>,

Source§

fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>

Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Source§

impl<'a, T> AsAsciiStr for &'a T
where T: AsAsciiStr + ?Sized,

Source§

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[Self::Inner], Output = [Self::Inner]>,

Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Source§

impl<'a, T> AsAsciiStr for &'a mut T
where T: AsAsciiStr + ?Sized,

Source§

fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
where R: SliceIndex<[Self::Inner], Output = [Self::Inner]>,

Source§

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

Implementors§