tycho_common/
display.rs

1use std::fmt::{Display, Formatter};
2
3use tracing::Value;
4
5/// Wrapper that makes `Option<T>` implement `Display`.
6pub struct DisplayOption<'a, T>(&'a Option<T>);
7
8impl<'a, T: Display> Display for DisplayOption<'a, T> {
9    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
10        match self.0 {
11            Some(inner) => write!(f, "{}", inner),
12            None => write!(f, "None"),
13        }
14    }
15}
16
17/// Convenience function so you can write `%opt(value)` in `tracing` logs.
18pub fn opt<T: Display>(val: &Option<T>) -> impl Value + '_ {
19    tracing::field::display(DisplayOption(val))
20}