1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*! Format forwarding

This module provides wrapper types for each formatting trait other than `Debug`
which, when `Debug`-formatted, forward to the original trait instead of `Debug`.

Each wrapper type is a tuple struct so that it can be used as a named
constructor, such as in `.map(FmtDisplay)`. In addition, a blanket trait adds
extension methods `.fmt_<trait_name>>()` to provide the corresponding wrap.

Any modifiers in the format template string or struct modifier are passed
through to the desired trait implementation unchanged. The only effect of the
forwarding types in this module is to change the `?` template character to one
of the other trait signifiers.
!*/

use core::{
	fmt::{
		self,
		Binary,
		Debug,
		Display,
		Formatter,
		LowerExp,
		LowerHex,
		Octal,
		Pointer,
		UpperExp,
		UpperHex,
	},
	ops::{
		Deref,
		DerefMut,
	},
};

/// Wraps any value with a format-forward to `Debug`.
pub trait FmtForward: Sized {
	/// Causes `self` to use its `Binary` implementation when `Debug`-formatted.
	fn fmt_binary(self) -> FmtBinary<Self>
	where Self: Binary {
		FmtBinary(self)
	}

	/// Causes `self` to use its `Display` implementation when
	/// `Debug`-formatted.
	fn fmt_display(self) -> FmtDisplay<Self>
	where Self: Display {
		FmtDisplay(self)
	}

	/// Causes `self` to use its `LowerExp` implementation when
	/// `Debug`-formatted.
	fn fmt_lower_exp(self) -> FmtLowerExp<Self>
	where Self: LowerExp {
		FmtLowerExp(self)
	}

	/// Causes `self` to use its `LowerHex` implementation when
	/// `Debug`-formatted.
	fn fmt_lower_hex(self) -> FmtLowerHex<Self>
	where Self: LowerHex {
		FmtLowerHex(self)
	}

	/// Causes `self` to use its `Octal` implementation when `Debug`-formatted.
	fn fmt_octal(self) -> FmtOctal<Self>
	where Self: Octal {
		FmtOctal(self)
	}

	/// Causes `self` to use its `Pointer` implementation when
	/// `Debug`-formatted.
	fn fmt_pointer(self) -> FmtPointer<Self>
	where Self: Pointer {
		FmtPointer(self)
	}

	/// Causes `self` to use its `UpperExp` implementation when
	/// `Debug`-formatted.
	fn fmt_upper_exp(self) -> FmtUpperExp<Self>
	where Self: UpperExp {
		FmtUpperExp(self)
	}

	/// Causes `self` to use its `UpperHex` implementation when
	/// `Debug`-formatted.
	fn fmt_upper_hex(self) -> FmtUpperHex<Self>
	where Self: UpperHex {
		FmtUpperHex(self)
	}
}

impl<T: Sized> FmtForward for T {
}

/// Forwards a type’s `Binary` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtBinary<T: Binary>(pub T);

/// Forwards a type’s `Display` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtDisplay<T: Display>(pub T);

/// Forwards a type’s `LowerExp` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtLowerExp<T: LowerExp>(pub T);

/// Forwards a type’s `LowerHex` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtLowerHex<T: LowerHex>(pub T);

/// Forwards a type’s `Octal` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtOctal<T: Octal>(pub T);

/// Forwards a type’s `Pointer` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtPointer<T: Pointer>(pub T);

/// Forwards a type’s `UpperExp` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtUpperExp<T: UpperExp>(pub T);

/// Forwards a type’s `UpperHex` formatting implementation to `Debug`.
#[repr(transparent)]
pub struct FmtUpperHex<T: UpperHex>(pub T);

macro_rules! fmt {
	($($w:ty => $t:ident),* $(,)?) => { $(
		impl<T: $t + Binary> Binary for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				Binary::fmt(&self.0, fmt)
			}
		}

		impl<T: $t> Debug for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				<T as $t>::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + Display> Display for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				Display::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + LowerExp> LowerExp for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				LowerExp::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + LowerHex> LowerHex for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				LowerHex::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + Octal> Octal for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				Octal::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + Pointer> Pointer for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				Pointer::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + UpperExp> UpperExp for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				UpperExp::fmt(&self.0, fmt)
			}
		}

		impl<T: $t + UpperHex> UpperHex for $w {
			fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
				UpperHex::fmt(&self.0, fmt)
			}
		}

		impl<T: $t> Deref for $w {
			type Target = T;

			fn deref(&self) -> &Self::Target {
				&self.0
			}
		}

		impl<T: $t> DerefMut for $w {
			fn deref_mut(&mut self) -> &mut Self::Target {
				&mut self.0
			}
		}

		impl<T: $t> AsRef<T> for $w {
			fn as_ref(&self) -> &T {
				&self.0
			}
		}

		impl<T: $t> AsMut<T> for $w {
			fn as_mut(&mut self) -> &mut T {
				&mut self.0
			}
		}
	)* };
}

fmt!(
	FmtBinary<T> => Binary,
	FmtDisplay<T> => Display,
	FmtLowerExp<T> => LowerExp,
	FmtLowerHex<T> => LowerHex,
	FmtOctal<T> => Octal,
	FmtPointer<T> => Pointer,
	FmtUpperExp<T> => UpperExp,
	FmtUpperHex<T> => UpperHex,
);