[][src]Trait ascii::AsAsciiStr

pub trait AsAsciiStr {
    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; fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> { ... }
fn get_ascii(&self, index: usize) -> Option<AsciiChar> { ... } }

Convert slices of bytes or AsciiChar to AsciiStr.

Required methods

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.

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());

unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr

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

Examples

Loading content...

Provided methods

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

Convert to an ASCII slice.

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

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);
Loading content...

Implementors

impl AsAsciiStr for AsciiStr[src]

type Inner = AsciiChar

impl AsAsciiStr for [AsciiChar][src]

type Inner = AsciiChar

impl AsAsciiStr for [u8][src]

type Inner = u8

impl AsAsciiStr for str[src]

type Inner = u8

impl<'a, T: ?Sized> AsAsciiStr for &'a T where
    T: AsAsciiStr
[src]

type Inner = <T as AsAsciiStr>::Inner

impl<'a, T: ?Sized> AsAsciiStr for &'a mut T where
    T: AsAsciiStr
[src]

type Inner = <T as AsAsciiStr>::Inner

Loading content...