pub struct AsciiStr<'a>(/* private fields */);
Expand description
An ascii string slice.
You can also construct an AsciiStr
at compile-time with the ascii_str
macro,
erroring at compile if the constant isn’t ascii.
§Example
use const_format::wrapper_types::{AsciiStr, NotAsciiError};
use const_format::ascii_str;
const HELLO: AsciiStr = unwrap_ascii(AsciiStr::new(b"hello"));
const EURO: AsciiStr = unwrap_ascii(AsciiStr::new("foo €".as_bytes()));
assert_eq!(HELLO.as_str(), "hello");
assert_eq!(EURO.as_str(), "<error>");
assert_eq!(AsciiStr::new("foo €".as_bytes()), Err(NotAsciiError{invalid_from: 4}));
const fn unwrap_ascii(res: Result<AsciiStr<'_>, NotAsciiError>) -> AsciiStr<'_> {
match res {
Ok(x) => x,
Err(_) => ascii_str!("<error>"),
}
}
Implementations§
Source§impl<'a> AsciiStr<'a>
impl<'a> AsciiStr<'a>
Sourcepub const fn from_str(s: &'a str) -> Result<Self, NotAsciiError>
pub const fn from_str(s: &'a str) -> Result<Self, NotAsciiError>
Constructs this AsciiStr from a possibly non-ascii str slice.
Returns a NonAsciiError
error on the first non-ascii byte.
§Example
use const_format::wrapper_types::{AsciiStr, NotAsciiError};
let ok = AsciiStr::from_str("foo bar").unwrap();
assert_eq!(ok.as_str(), "foo bar");
assert_eq!(AsciiStr::from_str("foo bar ½"), Err(NotAsciiError{invalid_from: 8}));
Sourcepub const fn new(s: &'a [u8]) -> Result<Self, NotAsciiError>
pub const fn new(s: &'a [u8]) -> Result<Self, NotAsciiError>
Constructs this AsciiStr from a possibly non-ascii byte slice.
Returns a NonAsciiError
error on the first non-ascii byte.
§Example
use const_format::wrapper_types::{AsciiStr, NotAsciiError};
let ok = AsciiStr::new(b"foo bar").unwrap();
assert_eq!(ok.as_str(), "foo bar");
assert_eq!(AsciiStr::new(b"foo bar \x80"), Err(NotAsciiError{invalid_from: 8}));
Sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Constructs an empty AsciiStr
§Example
use const_format::AsciiStr;
assert_eq!(AsciiStr::empty().as_str(), "");
Sourcepub const fn len(self) -> usize
pub const fn len(self) -> usize
Queries the length of the AsciiStr
§Example
use const_format::{AsciiStr, ascii_str};
assert_eq!(AsciiStr::empty().len(), 0);
assert_eq!(ascii_str!("hello").len(), 5);
Sourcepub const fn is_empty(self) -> bool
pub const fn is_empty(self) -> bool
Queries whether this AsciiStr
is empty.
§Example
use const_format::{AsciiStr, ascii_str};
assert_eq!(AsciiStr::empty().is_empty(), true);
assert_eq!(ascii_str!("hello").is_empty(), false);
Trait Implementations§
Source§impl FormatMarker for AsciiStr<'_>
impl FormatMarker for AsciiStr<'_>
Source§impl<'a> Ord for AsciiStr<'a>
impl<'a> Ord for AsciiStr<'a>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a> PartialOrd for AsciiStr<'a>
impl<'a> PartialOrd for AsciiStr<'a>
impl<'a> Copy for AsciiStr<'a>
impl<'a> Eq for AsciiStr<'a>
impl<'a> StructuralPartialEq for AsciiStr<'a>
Auto Trait Implementations§
impl<'a> Freeze for AsciiStr<'a>
impl<'a> RefUnwindSafe for AsciiStr<'a>
impl<'a> Send for AsciiStr<'a>
impl<'a> Sync for AsciiStr<'a>
impl<'a> Unpin for AsciiStr<'a>
impl<'a> UnwindSafe for AsciiStr<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 16 bytes