const_panic::fmt

Trait PanicFmt

Source
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 PanicFmt derive macro (requires the opt-in "derive" feature)
  • Using the impl_panicfmt macro (requires the default-enabled "non_basic" feature)
  • Using the flatten_panicvals macro (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§

Source

const PV_COUNT: usize

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

Provided Associated Constants§

Source

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§

Source

type This: ?Sized

The type after dereferencing all references.

User-defined types should generally set this to Self.

Source

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", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PanicFmt for bool

Source§

impl PanicFmt for char

Source§

impl PanicFmt for i8

Source§

impl PanicFmt for i16

Source§

impl PanicFmt for i32

Source§

impl PanicFmt for i64

Source§

impl PanicFmt for i128

Source§

impl PanicFmt for isize

Source§

impl PanicFmt for str

Source§

impl PanicFmt for u8

Source§

impl PanicFmt for u16

Source§

impl PanicFmt for u32

Source§

impl PanicFmt for u64

Source§

impl PanicFmt for u128

Source§

impl PanicFmt for usize

Source§

impl PanicFmt for Utf8Error

Source§

impl<'a> PanicFmt for [PanicVal<'a>]

Source§

const PV_COUNT: usize = 18_446_744_073_709_551_615usize

Source§

type This = [PanicVal<'a>]

Source§

type Kind = IsStdType

Source§

impl<'a, T: PanicFmt + ?Sized> PanicFmt for &'a T

Source§

const PV_COUNT: usize = T::PV_COUNT

Source§

type This = <T as PanicFmt>::This

Source§

type Kind = <T as PanicFmt>::Kind

Source§

impl<'a, const N: usize> PanicFmt for [PanicVal<'a>; N]

Implementors§