From 443d5385df4dac73cbd5adc4e022ce6bebb4418e Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:49:10 +0200 Subject: [PATCH] types. --- src/args.rs | 2 +- src/lib.rs | 9 ++++++--- src/opts.rs | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/args.rs b/src/args.rs index 3f4cfcc..15d835a 100644 --- a/src/args.rs +++ b/src/args.rs @@ -59,7 +59,7 @@ macro_rules! traits { impl Arguments for $name { fn fmt(&self, f: &mut Formatter) -> Result { - let mut f = f.with_opts(&self.1); + let mut f = f.wrap_with(&self.1); ::fmt(&self.0, &mut f) } diff --git a/src/lib.rs b/src/lib.rs index 4336de3..d91e145 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,11 +79,11 @@ impl Formatter { } } -impl Formatter { - fn with_opts<'opt, ONew>(&mut self, opts: &'opt ONew) -> Formatter<&mut W, &'opt ONew> { +impl Formatter { + fn wrap_with<'opt, ONew: FmtOpts>(&mut self, opts: &ONew) -> Formatter<&mut W, ONew::ReplaceInnermost> { Formatter { buf: &mut self.buf, - opts, + opts: opts.override_other(self.opts), } } } @@ -155,6 +155,7 @@ mod tests { } } +// testing fn fmt() { let a = ( _private::Str("amount: "), @@ -164,4 +165,6 @@ fn fmt() { let mut str = String::new(); let mut f = Formatter::new(&mut str); Arguments::fmt(&a, &mut f).unwrap(); + + println!("{str}"); } diff --git a/src/opts.rs b/src/opts.rs index bd3c74b..1a6b5b0 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -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)] type Inner: FmtOpts; + /// Replaces the innermost `()` with `I` + type ReplaceInnermost: FmtOpts; + fn inner(&self) -> &Self::Inner; + /// # Example + /// `Self` is `WithAlternate>` + /// `Other` is WithMinus<()> + /// + /// This returns `WithAlternate, ' '>>` + /// + fn override_other(self, other: Other) -> Self::ReplaceInnermost; + $( #[inline] fn $name(&self) -> $ret { @@ -37,10 +49,16 @@ macro_rules! options { impl FmtOpts for () { type Inner = Self; + type ReplaceInnermost = I; + fn inner(&self) -> &Self::Inner { self } + fn override_other(self, other: Other) -> Self::ReplaceInnermost:: { + other + } + $( #[inline] fn $name(&self) -> $ret { @@ -52,10 +70,16 @@ macro_rules! options { impl FmtOpts for &'_ O { type Inner = O::Inner; - fn inner(&self) -> &Self::Inner { + type ReplaceInnermost = O::ReplaceInnermost; + + fn inner(&self) -> &Self::Inner { O::inner(self) } + fn override_other(self, other: Other) -> Self::ReplaceInnermost { + O::override_other(*self, other) + } + $( #[inline] fn $name(&self) -> $ret { @@ -75,15 +99,22 @@ macro_rules! options { } $( + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct $with_name(#[doc(hidden)] pub I); impl FmtOpts for $with_name { type Inner = I; + type ReplaceInnermost = $with_name, $($($const_gen_name),*)?>; + fn inner(&self) -> &Self::Inner { &self.0 } + fn override_other(self, other: Other) -> Self::ReplaceInnermost { + $with_name(self.0.override_other(other)) + } + fn $name(&self) -> $ret { $($struct_body)* }