pub struct ArrayString<const CAP: usize> { /* private fields */ }
Available on crate feature non_basic only.
Expand description

For precomputing a panic message.

Implementations§

source§

impl<const CAP: usize> ArrayString<CAP>

source

pub const fn new(string: &str) -> Self

Constructs an ArrayString from a &str

Panics

Panics if string is larger than CAP.

Example
use const_panic::ArrayString;

assert_eq!(ArrayString::<16>::new("Hello, world!"), "Hello, world!");
source

pub const fn concat(strings: &[&str]) -> Self

Constructs an ArrayString by concatenating zero or more &strs

Panics

Panics if the concatenated string would be longer than CAP.

Example
use const_panic::ArrayString;

assert_eq!(
    ArrayString::<99>::concat(&["This ", "is ", "a string"]),
    "This is a string"
);
source

pub const fn concat_panicvals(args: &[&[PanicVal<'_>]]) -> Option<Self>

Constructs this string from a &[&[PanicVal<'_>]].

Returns None if the formatted args would be larger than CAP.

Example
use const_panic::{ArrayString, FmtArg, flatten_panicvals};

assert_eq!(
    ArrayString::<17>::concat_panicvals(&[
        &flatten_panicvals!(FmtArg::DEBUG; 1u8, ("hello")),
        &flatten_panicvals!(FmtArg::DEBUG; &[3u8, 5, 8]),
    ]).unwrap(),
    "1\"hello\"[3, 5, 8]",
);

assert!(
    ArrayString::<16>::concat_panicvals(&[
        &flatten_panicvals!(FmtArg::DEBUG; 1u8, ("hello")),
        &flatten_panicvals!(FmtArg::DEBUG; &[3u8, 5, 8]),
    ]).is_none(),
);
source

pub const fn from_panicvals(args: &[PanicVal<'_>]) -> Option<Self>

Constructs this string from a &[PanicVal<'_>].

Returns None if the formatted args would be larger than CAP.

Example
use const_panic::{ArrayString, FmtArg, flatten_panicvals};

assert_eq!(
    ArrayString::<8>::from_panicvals(
        &flatten_panicvals!(FmtArg::DEBUG; 100u8, "hello")
    ).unwrap(),
    "100hello",
);

// trying to format panicvals into too small an ArrayString
assert!(
    ArrayString::<7>::from_panicvals(
        &flatten_panicvals!(FmtArg::DEBUG; 100u8, "hello")
    ).is_none(),
);
source

pub const fn len(&self) -> usize

How long the string is in bytes.

Example
use const_panic::ArrayString;

assert_eq!(ArrayString::<16>::new("foo").len(), 3);
assert_eq!(ArrayString::<16>::new("foo bar").len(), 7);
assert_eq!(ArrayString::<16>::new("Hello, world!").len(), 13);
source

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

Accesses the string as a byte slice.

Performance

When the “rust_1_64” feature is disabled, this takes a linear amount of time to run, proportional to CAP - self.len().

When the “rust_1_64” feature is enabled, this takes a constant amount of time to run.

Example
use const_panic::ArrayString;

assert_eq!(ArrayString::<16>::new("foo").as_bytes(), b"foo");
assert_eq!(ArrayString::<16>::new("foo bar").as_bytes(), b"foo bar");
assert_eq!(ArrayString::<16>::new("Hello, world!").as_bytes(), b"Hello, world!");
source

pub const fn to_str(&self) -> &str

Gets the string.

Performance

This takes a linear amount of time to run.

Example
use const_panic::ArrayString;

assert_eq!(ArrayString::<16>::new("foo").to_str(), "foo");
assert_eq!(ArrayString::<16>::new("foo bar").to_str(), "foo bar");
assert_eq!(ArrayString::<16>::new("Hello, world!").to_str(), "Hello, world!");
source

pub const fn to_panicvals(&self, f: FmtArg) -> [PanicVal<'_>; 1]

Creates a single element PanicVal borrowing from this string.

source

pub const fn to_panicval(&self, f: FmtArg) -> PanicVal<'_>

Creates a PanicVal borrowing from this string.

Trait Implementations§

source§

impl<const CAP: usize> Clone for ArrayString<CAP>

source§

fn clone(&self) -> ArrayString<CAP>

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<const CAP: usize> Debug for ArrayString<CAP>

source§

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

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

impl<const CAP: usize> PanicFmt for ArrayString<CAP>

§

type This = ArrayString<CAP>

The type after dereferencing all references. Read more
§

type Kind = IsCustomType

Whether this is a user-defined type or standard library type. Read more
source§

const PV_COUNT: usize = 1usize

The length of the array returned in Self::to_panicvals (an inherent method that formats the type for panic messages).
source§

const PROOF: IsPanicFmt<Self, Self::This, Self::Kind> = IsPanicFmt::NEW

A marker type that proves that Self implements PanicFmt. Read more
source§

impl<const CAP: usize> PartialEq<&str> for ArrayString<CAP>

source§

fn eq(&self, str: &&str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>

source§

fn eq(&self, str: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const CAP: usize> Copy for ArrayString<CAP>

Auto Trait Implementations§

§

impl<const CAP: usize> RefUnwindSafe for ArrayString<CAP>

§

impl<const CAP: usize> Send for ArrayString<CAP>

§

impl<const CAP: usize> Sync for ArrayString<CAP>

§

impl<const CAP: usize> Unpin for ArrayString<CAP>

§

impl<const CAP: usize> UnwindSafe for ArrayString<CAP>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, W> HasTypeWitness<W> for Twhere W: MakeTypeWitness<Arg = T>, T: ?Sized,

source§

const WITNESS: W = W::MAKE

A constant of the type witness
source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.