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> { ... }
}
Required Methods§
Sourcefn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
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());
Sourceunsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
Provided Methods§
Sourcefn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
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
Sourcefn get_ascii(&self, index: usize) -> Option<AsciiChar>
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
impl AsAsciiStr for str
fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
Source§impl AsAsciiStr for CStr
impl AsAsciiStr for CStr
Note that the trailing null byte will be removed in the conversion.