Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value implementation for `Option<T> where T: Value #878

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ pub trait Visit {

/// Visit a value implementing `fmt::Debug`.
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug);

/// Visit an Option
fn record_option(&mut self, field: &Field, value: Option<&(dyn Value + 'static)>) {
if let Some(inner_value) = value {
self.record_debug(field, &inner_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this implements the recording of the interior type using the appropriate type-specific record_* method as described in the linked task.

You probably want to call the record method on the inner_value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right inner_value.record(field, self)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right inner_value.record(field, self)?

Yeah, that's right.

} else {
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this else { } be removed if we aren't going to do anything with it? If a serializer cares about the lack of a value they can always override this method and do a match themselves.

}
}

/// A field value of an erased type.
Expand Down Expand Up @@ -441,6 +449,15 @@ impl<'a> Value for fmt::Arguments<'a> {
}
}

impl<T> Value for Option<T>
where
T: Value + 'static,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
visitor.record_option(key, self.as_ref().map(|v| v as &dyn Value))
}
}

impl fmt::Debug for dyn Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// We are only going to be recording the field value, so we don't
Expand Down Expand Up @@ -476,6 +493,8 @@ impl fmt::Display for dyn Value {
}
}

impl<T> crate::sealed::Sealed for Option<T> {}

// ===== impl DisplayValue =====

impl<T: fmt::Display> crate::sealed::Sealed for DisplayValue<T> {}
Expand Down