Struct ArrayString

Source
pub struct ArrayString<const CAP: usize> { /* private fields */ }
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 duplicate 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>

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§

type This = ArrayString<CAP>

The type after dereferencing all references. Read more
Source§

type Kind = IsCustomType

Whether this is a user-defined type or standard library type. Read more
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

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

Source§

fn eq(&self, str: &str) -> 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<const CAP: usize> Copy for ArrayString<CAP>

Auto Trait Implementations§

§

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

§

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 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
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.