Struct const_panic::ArrayString
source · 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>
impl<const CAP: usize> ArrayString<CAP>
sourcepub const fn concat_panicvals(args: &[&[PanicVal<'_>]]) -> Option<Self>
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(),
);
sourcepub const fn from_panicvals(args: &[PanicVal<'_>]) -> Option<Self>
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(),
);
sourcepub const fn len(&self) -> usize
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);
sourcepub const fn as_bytes(&self) -> &[u8]
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!");
sourcepub const fn to_str(&self) -> &str
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!");
sourcepub const fn to_panicvals(&self, f: FmtArg) -> [PanicVal<'_>; 1]
pub const fn to_panicvals(&self, f: FmtArg) -> [PanicVal<'_>; 1]
Creates a single element PanicVal
borrowing from this string.
sourcepub const fn to_panicval(&self, f: FmtArg) -> PanicVal<'_>
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>
impl<const CAP: usize> Clone for ArrayString<CAP>
source§fn clone(&self) -> ArrayString<CAP>
fn clone(&self) -> ArrayString<CAP>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<const CAP: usize> Debug for ArrayString<CAP>
impl<const CAP: usize> Debug for ArrayString<CAP>
source§impl<const CAP: usize> PanicFmt for ArrayString<CAP>
impl<const CAP: usize> PanicFmt for ArrayString<CAP>
§type This = ArrayString<CAP>
type This = ArrayString<CAP>
The type after dereferencing all references. Read more
§type Kind = IsCustomType
type Kind = IsCustomType
Whether this is a user-defined type or standard library type. Read more