use super::*;
pub struct MsgWrap<T> {
t: T,
context: Option<Context>,
}
impl<T> MsgWrap<T> {
pub fn new(t: T, context: Context) -> Self {
Self {
t,
context: Some(context),
}
}
pub fn inner(self) -> T {
if let Some(context) = self.context {
tracing::Span::set_current_context(context);
}
self.t
}
pub fn without_context(self) -> T {
self.t
}
pub fn from_no_context(t: T) -> Self {
Self { t, context: None }
}
pub fn into_parts(self) -> (T, Context) {
(self.t, self.context.unwrap_or_default())
}
}
impl<T> From<T> for MsgWrap<T> {
fn from(t: T) -> Self {
let span = tracing::Span::current();
let context = if span.is_disabled() {
None
} else {
Some(span.get_context())
};
Self { t, context }
}
}
impl<T> std::fmt::Debug for MsgWrap<T>
where
T: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:?}", self.t))
}
}