pub trait PanicFmt {
type This: ?Sized;
type Kind;
const PV_COUNT: usize;
const PROOF: IsPanicFmt<Self, Self::This, Self::Kind> = IsPanicFmt::NEW;
}Expand description
Trait for types that can be formatted by const panics.
§Implementor
Implementors are expected to also define this inherent method to format the type:
const fn to_panicvals<'a>(&'a self, f: FmtArg) -> [PanicVal<'a>; <Self as PanicFmt>::PV_COUNT]The returned PanicVal can also be PanicVal<'static>.
§Implementation examples
This trait can be implemented in these ways (in increasing order of verbosity):
- Using the
PanicFmtderive macro (requires the opt-in"derive"feature) - Using the
impl_panicfmtmacro (requires the default-enabled"non_basic"feature) - Using the
flatten_panicvalsmacro (requires the default-enabled"non_basic"feature) - Using no macros at all
§Macro-less impl
Implementing this trait for a simple enum without using macros.
use const_panic::{ArrayString, FmtArg, PanicFmt, PanicVal};
// `ArrayString` requires the "non_basic" crate feature (enabled by default),
// everything else in this example works with no enabled crate features.
assert_eq!(
ArrayString::<99>::concat_panicvals(&[
&Foo::Bar.to_panicvals(FmtArg::DEBUG),
&[PanicVal::write_str(",")],
&Foo::Baz.to_panicvals(FmtArg::DEBUG),
&[PanicVal::write_str(",")],
&Foo::Qux.to_panicvals(FmtArg::DEBUG),
]).unwrap(),
"Bar,Baz,Qux",
);
enum Foo {
Bar,
Baz,
Qux,
}
impl PanicFmt for Foo {
type This = Self;
type Kind = const_panic::IsCustomType;
const PV_COUNT: usize = 1;
}
impl Foo {
pub const fn to_panicvals(self, _: FmtArg) -> [PanicVal<'static>; Foo::PV_COUNT] {
match self {
Self::Bar => [PanicVal::write_str("Bar")],
Self::Baz => [PanicVal::write_str("Baz")],
Self::Qux => [PanicVal::write_str("Qux")],
}
}
}
Required Associated Constants§
Provided Associated Constants§
Sourceconst PROOF: IsPanicFmt<Self, Self::This, Self::Kind> = IsPanicFmt::NEW
const PROOF: IsPanicFmt<Self, Self::This, Self::Kind> = IsPanicFmt::NEW
A marker type that proves that Self implements PanicFmt.
Used by const_panic macros to coerce both standard library and
user-defined types into some type that has a to_panicvals method.
Required Associated Types§
Sourcetype This: ?Sized
type This: ?Sized
The type after dereferencing all references.
User-defined types should generally set this to Self.
Sourcetype Kind
type Kind
Whether this is a user-defined type or standard library type.
User-defined types should generally set this to IsCustomType.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl PanicFmt for Infallible
Available on crate feature non_basic only.
impl PanicFmt for Infallible
non_basic only.Source§impl PanicFmt for FromBytesWithNulError
Available on crate features non_basic and rust_1_88 only.
impl PanicFmt for FromBytesWithNulError
non_basic and rust_1_88 only.Source§impl PanicFmt for IntErrorKind
Available on crate feature rust_1_82 only.
impl PanicFmt for IntErrorKind
rust_1_82 only.Source§impl PanicFmt for FromBytesUntilNulError
Available on crate features non_basic and rust_1_88 only.
impl PanicFmt for FromBytesUntilNulError
non_basic and rust_1_88 only.Source§impl PanicFmt for PhantomPinned
Available on crate feature non_basic only.
impl PanicFmt for PhantomPinned
non_basic only.Source§impl PanicFmt for ParseIntError
Available on crate feature rust_1_82 only.
impl PanicFmt for ParseIntError
rust_1_82 only.Source§impl PanicFmt for RangeInclusive<usize>
Available on crate feature non_basic only.
impl PanicFmt for RangeInclusive<usize>
non_basic only.Source§impl PanicFmt for RangeToInclusive<usize>
Available on crate feature non_basic only.
impl PanicFmt for RangeToInclusive<usize>
non_basic only.Source§impl PanicFmt for NonZeroI16
Available on crate feature non_basic only.
impl PanicFmt for NonZeroI16
non_basic only.Source§impl PanicFmt for NonZeroI32
Available on crate feature non_basic only.
impl PanicFmt for NonZeroI32
non_basic only.Source§impl PanicFmt for NonZeroI64
Available on crate feature non_basic only.
impl PanicFmt for NonZeroI64
non_basic only.Source§impl PanicFmt for NonZeroI128
Available on crate feature non_basic only.
impl PanicFmt for NonZeroI128
non_basic only.Source§impl PanicFmt for NonZeroIsize
Available on crate feature non_basic only.
impl PanicFmt for NonZeroIsize
non_basic only.Source§impl PanicFmt for NonZeroU16
Available on crate feature non_basic only.
impl PanicFmt for NonZeroU16
non_basic only.Source§impl PanicFmt for NonZeroU32
Available on crate feature non_basic only.
impl PanicFmt for NonZeroU32
non_basic only.Source§impl PanicFmt for NonZeroU64
Available on crate feature non_basic only.
impl PanicFmt for NonZeroU64
non_basic only.Source§impl PanicFmt for NonZeroU128
Available on crate feature non_basic only.
impl PanicFmt for NonZeroU128
non_basic only.Source§impl PanicFmt for NonZeroUsize
Available on crate feature non_basic only.
impl PanicFmt for NonZeroUsize
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [&'s str; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [&'s str; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [bool; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [bool; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [char; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [char; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [i128; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [i128; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [isize; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [isize; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [u128; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [u128; LEN]
non_basic only.Source§impl<'s, const LEN: usize> PanicFmt for [usize; LEN]
Available on crate feature non_basic only.
impl<'s, const LEN: usize> PanicFmt for [usize; LEN]
non_basic only.Source§impl<T> PanicFmt for Option<T>where
T: PanicFmt,
Available on crate feature non_basic only.Note: there is only to_panicvals methods for Options of standard library types
for now.
impl<T> PanicFmt for Option<T>where
T: PanicFmt,
non_basic only.Note: there is only to_panicvals methods for Options of standard library types
for now.
Source§impl<T: ?Sized> PanicFmt for PhantomData<T>
Available on crate feature non_basic only.
impl<T: ?Sized> PanicFmt for PhantomData<T>
non_basic only.