This commit is contained in:
nora 2022-09-14 22:49:10 +02:00
parent 594047c0a1
commit 443d5385df
3 changed files with 40 additions and 6 deletions

View file

@ -59,7 +59,7 @@ macro_rules! traits {
impl<T: $trait, O: FmtOpts> Arguments for $name<T, O> { impl<T: $trait, O: FmtOpts> Arguments for $name<T, O> {
fn fmt<W: Write, OldOpts: FmtOpts>(&self, f: &mut Formatter<W, OldOpts>) -> Result { fn fmt<W: Write, OldOpts: FmtOpts>(&self, f: &mut Formatter<W, OldOpts>) -> Result {
let mut f = f.with_opts(&self.1); let mut f = f.wrap_with(&self.1);
<T as $trait>::fmt(&self.0, &mut f) <T as $trait>::fmt(&self.0, &mut f)
} }

View file

@ -79,11 +79,11 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
} }
} }
impl<W, O> Formatter<W, O> { impl<W, O: FmtOpts> Formatter<W, O> {
fn with_opts<'opt, ONew>(&mut self, opts: &'opt ONew) -> Formatter<&mut W, &'opt ONew> { fn wrap_with<'opt, ONew: FmtOpts>(&mut self, opts: &ONew) -> Formatter<&mut W, ONew::ReplaceInnermost<O>> {
Formatter { Formatter {
buf: &mut self.buf, buf: &mut self.buf,
opts, opts: opts.override_other(self.opts),
} }
} }
} }
@ -155,6 +155,7 @@ mod tests {
} }
} }
// testing
fn fmt() { fn fmt() {
let a = ( let a = (
_private::Str("amount: "), _private::Str("amount: "),
@ -164,4 +165,6 @@ fn fmt() {
let mut str = String::new(); let mut str = String::new();
let mut f = Formatter::new(&mut str); let mut f = Formatter::new(&mut str);
Arguments::fmt(&a, &mut f).unwrap(); Arguments::fmt(&a, &mut f).unwrap();
println!("{str}");
} }

View file

@ -20,12 +20,24 @@ macro_rules! options {
} }
)* )*
) => { ) => {
pub trait FmtOpts { // FIXME: We can get rid of this Copy can't we
pub trait FmtOpts: Copy {
#[doc(hidden)] #[doc(hidden)]
type Inner: FmtOpts; type Inner: FmtOpts;
/// Replaces the innermost `()` with `I`
type ReplaceInnermost<I: FmtOpts>: FmtOpts;
fn inner(&self) -> &Self::Inner; fn inner(&self) -> &Self::Inner;
/// # Example
/// `Self` is `WithAlternate<WithFill<(), ' '>>`
/// `Other` is WithMinus<()>
///
/// This returns `WithAlternate<WithFille<WithMinus<()>, ' '>>`
///
fn override_other<Other: FmtOpts>(self, other: Other) -> Self::ReplaceInnermost<Other>;
$( $(
#[inline] #[inline]
fn $name(&self) -> $ret { fn $name(&self) -> $ret {
@ -37,10 +49,16 @@ macro_rules! options {
impl FmtOpts for () { impl FmtOpts for () {
type Inner = Self; type Inner = Self;
type ReplaceInnermost<I: FmtOpts> = I;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
self self
} }
fn override_other<Other: FmtOpts>(self, other: Other) -> Self::ReplaceInnermost::<Other> {
other
}
$( $(
#[inline] #[inline]
fn $name(&self) -> $ret { fn $name(&self) -> $ret {
@ -52,10 +70,16 @@ macro_rules! options {
impl<O: FmtOpts> FmtOpts for &'_ O { impl<O: FmtOpts> FmtOpts for &'_ O {
type Inner = O::Inner; type Inner = O::Inner;
fn inner(&self) -> &Self::Inner { type ReplaceInnermost<I: FmtOpts> = O::ReplaceInnermost<I>;
fn inner(&self) -> &Self::Inner {
O::inner(self) O::inner(self)
} }
fn override_other<Other: FmtOpts>(self, other: Other) -> Self::ReplaceInnermost<Other> {
O::override_other(*self, other)
}
$( $(
#[inline] #[inline]
fn $name(&self) -> $ret { fn $name(&self) -> $ret {
@ -75,15 +99,22 @@ macro_rules! options {
} }
$( $(
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $with_name<I, $($(const $const_gen_name: $with_ty),*)?>(#[doc(hidden)] pub I); pub struct $with_name<I, $($(const $const_gen_name: $with_ty),*)?>(#[doc(hidden)] pub I);
impl<I: FmtOpts, $($(const $const_gen_name: $with_ty),*)?> FmtOpts for $with_name<I, $($($const_gen_name),*)?> { impl<I: FmtOpts, $($(const $const_gen_name: $with_ty),*)?> FmtOpts for $with_name<I, $($($const_gen_name),*)?> {
type Inner = I; type Inner = I;
type ReplaceInnermost<Replacement: FmtOpts> = $with_name<I::ReplaceInnermost<Replacement>, $($($const_gen_name),*)?>;
fn inner(&self) -> &Self::Inner { fn inner(&self) -> &Self::Inner {
&self.0 &self.0
} }
fn override_other<Other: FmtOpts>(self, other: Other) -> Self::ReplaceInnermost<Other> {
$with_name(self.0.override_other(other))
}
fn $name(&self) -> $ret { fn $name(&self) -> $ret {
$($struct_body)* $($struct_body)*
} }