const_panic/
wrapper.rs

1use crate::PanicFmt;
2
3/// A wrapper type used to define methods for std types.
4///
5/// Std types are coerced to this type through the approach used in the
6/// [`coerce_fmt`] macro.
7///
8/// # Example
9///
10/// Formatting std types with this type's `to_panicvals` methods,
11/// without using macros.
12///
13#[cfg_attr(feature = "non_basic", doc = "```rust")]
14#[cfg_attr(not(feature = "non_basic"), doc = "```ignore")]
15/// use const_panic::{ArrayString, FmtArg, StdWrapper};
16///
17/// assert_eq!(
18///     ArrayString::<99>::from_panicvals(
19///         &StdWrapper("hello").to_panicvals(FmtArg::DEBUG)
20///     ).unwrap(),
21///     r#""hello""#
22/// );
23///
24/// assert_eq!(
25///     ArrayString::<99>::from_panicvals(
26///         &StdWrapper(&[3u8, 5, 8]).to_panicvals(FmtArg::ALT_DEBUG)
27///     ).unwrap(),
28///     "[\n    3,\n    5,\n    8,\n]"
29/// );
30///
31/// ```
32#[derive(Copy, Clone)]
33#[repr(transparent)]
34pub struct StdWrapper<T>(pub T);
35
36impl<T> PanicFmt for StdWrapper<T>
37where
38    T: PanicFmt,
39{
40    type This = Self;
41    type Kind = crate::fmt::IsCustomType;
42
43    const PV_COUNT: usize = T::PV_COUNT;
44}