Struct AsciiStr

Source
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>

Source

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}));
Source

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}));
Source

pub const fn empty() -> Self

Constructs an empty AsciiStr

§Example
use const_format::AsciiStr;

assert_eq!(AsciiStr::empty().as_str(), "");
Source

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

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

pub const fn as_bytes(self) -> &'a [u8]

Accessor for the wrapped ascii string.

§Example
use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().as_bytes(), b"");
assert_eq!(ascii_str!("hello").as_bytes(), b"hello");
Source

pub fn as_str(self) -> &'a str

Accessor for the wrapped ascii string.

§Example
use const_format::{AsciiStr, ascii_str};

assert_eq!(AsciiStr::empty().as_str(), "");
assert_eq!(ascii_str!("hello").as_str(), "hello");
Source§

impl AsciiStr<'_>

Source

pub const fn const_display_fmt( &self, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source

pub const fn const_debug_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Trait Implementations§

Source§

impl<'a> Clone for AsciiStr<'a>

Source§

fn clone(&self) -> AsciiStr<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for AsciiStr<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FormatMarker for AsciiStr<'_>

Source§

type Kind = IsNotStdKind

What kind of type this is, this can be one of: Read more
Source§

type This = AsciiStr<'_>

The type after dereferencing, implemented as type This = Self; for all non-reference types
Source§

impl<'a> Hash for AsciiStr<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for AsciiStr<'a>

Source§

fn cmp(&self, other: &AsciiStr<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for AsciiStr<'a>

Source§

fn eq(&self, other: &AsciiStr<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for AsciiStr<'a>

Source§

fn partial_cmp(&self, other: &AsciiStr<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Copy for AsciiStr<'a>

Source§

impl<'a> Eq for AsciiStr<'a>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

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