1use crate::Contextual;
2use std::fmt;
3
4pub trait DisplayWithContext<C: ?Sized> {
5 fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result;
6}
7
8impl<T: DisplayWithContext<C::Target>, C: std::ops::Deref> fmt::Display for Contextual<T, C> {
9 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10 self.0.fmt_with(self.1.deref(), f)
11 }
12}
13
14impl<'a, T: DisplayWithContext<C> + ?Sized, C> DisplayWithContext<C> for &'a T {
15 fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
16 T::fmt_with(*self, context, f)
17 }
18}
19
20impl<'a, T: DisplayWithContext<C> + std::borrow::ToOwned + ?Sized, C> DisplayWithContext<C>
21 for std::borrow::Cow<'a, T>
22{
23 fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
24 T::fmt_with(self, context, f)
25 }
26}