pub trait FormatMarker {
    type Kind;
    type This: ?Sized;
}
Available on crate feature fmt only.
Expand description

Marker trait for types that implement the const formatting methods.

Debug formatting can be derived using the ConstDebug derive macro.

Implementors

Types that implement this trait are also expected to implement at least one of these inherent methods:

// use const_format::{Error, Format};

const fn const_debug_fmt(&self, &mut Formatter<'_>) -> Result<(), Error>

const fn const_display_fmt(&self, &mut Formatter<'_>) -> Result<(), Error>

Coercions

The Kind and This associated types are used in the IsAFormatMarker marker type to automatically wrap types in PWrapper if they’re from the standard library, otherwise leaving them unwrapped.

Examples

Display formatting

This example demonstrates how you can implement display formatting, without using the impl_fmt macro.

#![feature(const_mut_refs)]

use const_format::{
    marker_traits::{FormatMarker, IsNotStdKind},
    Error, Formatter, StrWriter,
    formatc, writec,
};

use std::cmp::Ordering;


struct Compared(u32, Ordering, u32);

// This is what the `impl_fmt` macro implements implicitly for all non-std types
impl FormatMarker for Compared {
    type Kind = IsNotStdKind;
    type This = Self;
}

impl Compared {
    pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        let op = match self.1 {
            Ordering::Less => "<",
            Ordering::Equal => "==",
            Ordering::Greater => ">",
        };
        writec!(f, "{} {} {}", self.0, op, self.2)
    }
}

const S_0: &str = formatc!("{}", Compared(0, Ordering::Less, 1));
const S_1: &str = formatc!("{}", Compared(1, Ordering::Equal, 1));
const S_2: &str = formatc!("{}", Compared(2, Ordering::Greater, 1));

assert_eq!(S_0, "0 < 1");
assert_eq!(S_1, "1 == 1");
assert_eq!(S_2, "2 > 1");

Debug formatting

For examples of using the ConstDebug derive macro, look here.

These are examples of implementing debug formatting using the impl_fmt macro for:

Required Associated Types§

source

type Kind

What kind of type this is, this can be one of:

source

type This: ?Sized

The type after dereferencing, implemented as type This = Self; for all non-reference types

Implementations on Foreign Types§

source§

impl FormatMarker for Option<i128>

source§

impl FormatMarker for Option<i8>

source§

impl FormatMarker for Option<NonZeroI128>

source§

impl<T> FormatMarker for &mut Twhere T: ?Sized + FormatMarker,

§

type Kind = <T as FormatMarker>::Kind

§

type This = <T as FormatMarker>::This

source§

impl FormatMarker for Option<isize>

source§

impl FormatMarker for Option<u8>

source§

impl FormatMarker for Option<NonZeroU32>

source§

impl FormatMarker for usize

source§

impl<T: ?Sized> FormatMarker for PhantomData<T>

source§

impl FormatMarker for NonZeroU32

source§

impl<'a> FormatMarker for Option<&'a str>

§

type Kind = IsStdKind

§

type This = Option<&'a str>

source§

impl FormatMarker for str

§

type Kind = IsStdKind

§

type This = str

source§

impl<T> FormatMarker for Option<NonNull<T>>

source§

impl FormatMarker for u16

§

type Kind = IsStdKind

§

type This = u16

source§

impl FormatMarker for PhantomPinned

source§

impl FormatMarker for Option<i64>

source§

impl<T, const N: usize> FormatMarker for [T; N]

source§

impl FormatMarker for NonZeroI16

source§

impl FormatMarker for NonZeroU128

source§

impl FormatMarker for u8

§

type Kind = IsStdKind

§

type This = u8

source§

impl FormatMarker for Option<NonZeroI16>

source§

impl FormatMarker for NonZeroI8

source§

impl FormatMarker for NonZeroI128

source§

impl FormatMarker for NonZeroUsize

source§

impl<T> FormatMarker for [T]

§

type Kind = IsArrayKind<T>

§

type This = [T]

source§

impl FormatMarker for i8

§

type Kind = IsStdKind

§

type This = i8

source§

impl FormatMarker for Option<i16>

source§

impl FormatMarker for Option<NonZeroUsize>

source§

impl FormatMarker for Option<bool>

source§

impl FormatMarker for u32

§

type Kind = IsStdKind

§

type This = u32

source§

impl FormatMarker for Option<NonZeroI8>

source§

impl FormatMarker for isize

source§

impl FormatMarker for i16

§

type Kind = IsStdKind

§

type This = i16

source§

impl FormatMarker for i128

source§

impl FormatMarker for Option<NonZeroU128>

source§

impl<T> FormatMarker for *mut T

source§

impl FormatMarker for Range<usize>

source§

impl FormatMarker for char

source§

impl FormatMarker for ()

§

type Kind = IsStdKind

§

type This = ()

source§

impl FormatMarker for Option<NonZeroIsize>

source§

impl<T> FormatMarker for NonNull<T>

§

type Kind = IsStdKind

§

type This = NonNull<T>

source§

impl FormatMarker for Ordering

source§

impl FormatMarker for Option<usize>

source§

impl FormatMarker for u128

source§

impl FormatMarker for RangeToInclusive<usize>

source§

impl FormatMarker for u64

§

type Kind = IsStdKind

§

type This = u64

source§

impl FormatMarker for NonZeroU64

source§

impl FormatMarker for Option<u64>

source§

impl FormatMarker for i64

§

type Kind = IsStdKind

§

type This = i64

source§

impl FormatMarker for RangeTo<usize>

source§

impl<T> FormatMarker for &Twhere T: ?Sized + FormatMarker,

§

type Kind = <T as FormatMarker>::Kind

§

type This = <T as FormatMarker>::This

source§

impl FormatMarker for NonZeroU16

source§

impl FormatMarker for RangeFull

source§

impl FormatMarker for bool

source§

impl FormatMarker for NonZeroI64

source§

impl FormatMarker for Option<char>

source§

impl FormatMarker for RangeFrom<usize>

source§

impl FormatMarker for RangeInclusive<usize>

source§

impl FormatMarker for NonZeroU8

source§

impl<T> FormatMarker for *const T

source§

impl FormatMarker for Option<NonZeroU8>

source§

impl FormatMarker for Option<NonZeroI32>

source§

impl FormatMarker for i32

§

type Kind = IsStdKind

§

type This = i32

source§

impl FormatMarker for NonZeroIsize

source§

impl FormatMarker for Option<NonZeroU64>

source§

impl FormatMarker for AtomicOrdering

source§

impl FormatMarker for Option<NonZeroI64>

source§

impl FormatMarker for Option<u128>

source§

impl FormatMarker for Option<i32>

source§

impl FormatMarker for Option<NonZeroU16>

source§

impl FormatMarker for Option<u32>

source§

impl FormatMarker for NonZeroI32

source§

impl FormatMarker for Option<u16>

Implementors§

source§

impl FormatMarker for Point3

source§

impl FormatMarker for Unit

source§

impl FormatMarker for AsciiStr<'_>

source§

impl<'a> FormatMarker for Sliced<&'a str, Range<usize>>

source§

impl<'a> FormatMarker for Sliced<&'a str, RangeFrom<usize>>

source§

impl<'a> FormatMarker for Sliced<&'a str, RangeFull>

source§

impl<'a> FormatMarker for Sliced<&'a str, RangeInclusive<usize>>

source§

impl<'a> FormatMarker for Sliced<&'a str, RangeTo<usize>>

source§

impl<'a> FormatMarker for Sliced<&'a str, RangeToInclusive<usize>>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, Range<usize>>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, RangeFrom<usize>>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, RangeFull>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, RangeInclusive<usize>>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, RangeTo<usize>>

source§

impl<'a> FormatMarker for Sliced<AsciiStr<'a>, RangeToInclusive<usize>>

source§

impl<P> FormatMarker for PWrapper<P>