1use std::fmt::{Display, Formatter};
2
3use tracing::Value;
4
5pub 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#[expect(clippy::ref_option, reason = "public API; changing the signature is a breaking change")]
19pub fn opt<T: Display>(val: &Option<T>) -> impl Value + '_ {
20 tracing::field::display(DisplayOption(val))
21}