Trait const_panic::fmt::PanicFmt[][src]

pub trait PanicFmt {
    type This: ?Sized;
    type Kind;

    const PV_COUNT: usize;
    const PROOF: IsPanicFmt<Self, Self::This, Self::Kind>;
Expand description

Trait for types that can be formatted by const panics.


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.

enum Foo {

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")],

Associated Types

The type after dereferencing all references.

User-defined types should generally set this to Self.

Whether this is a user-defined type or standard library type.

User-defined types should generally set this to IsCustomType.

Associated Constants

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

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.

Implementations on Foreign Types

Note: there is only to_panicvals methods for Options of standard library types for now.