Skip to content

Commit 8fbdb1c

Browse files
jyn514hawkw
andcommitted
tracing: allow owned values and fat pointers in Span::record (#2212)
Previously, using `record("x", "y")` would give an error: ``` error[E0277]: the size for values of type `str` cannot be known at compilation time --> src/main.rs:3:22 | 243 | span.record("x", "y"); | ^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `str` note: required by a bound in `Span::record` --> /home/jnelson/.local/lib/cargo/registry/src/github.com-1ecc6299db9ec823/tracing-0.1.32/src/span.rs:1184:30 | 1184 | pub fn record<Q: ?Sized, V>(&self, field: &Q, value: &V) -> &Self | ^ required by this bound in `Span::record` ``` Now it works fine, as tested by the doc-example. This doesn't break any existing code, because there's a generic `impl<T: Value> Value for &T`: https://docs.rs/tracing/0.1.35/tracing/trait.Value.html#impl-Value-for-%26%27a%20T Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 21cdf08 commit 8fbdb1c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

tracing/src/span.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ impl Span {
11361136
///
11371137
/// // Now, record a value for parting as well.
11381138
/// // (note that the field name is passed as a string slice)
1139-
/// span.record("parting", &"goodbye world!");
1139+
/// span.record("parting", "goodbye world!");
11401140
/// ```
11411141
/// However, it may also be used to record a _new_ value for a field whose
11421142
/// value was already recorded:
@@ -1154,7 +1154,7 @@ impl Span {
11541154
/// }
11551155
/// Err(_) => {
11561156
/// // Things are no longer okay!
1157-
/// span.record("is_okay", &false);
1157+
/// span.record("is_okay", false);
11581158
/// }
11591159
/// }
11601160
/// ```
@@ -1181,17 +1181,17 @@ impl Span {
11811181
/// // Now, you try to record a value for a new field, `new_field`, which was not
11821182
/// // declared as `Empty` or populated when you created `span`.
11831183
/// // You won't get any error, but the assignment will have no effect!
1184-
/// span.record("new_field", &"interesting_value_you_really_need");
1184+
/// span.record("new_field", "interesting_value_you_really_need");
11851185
///
11861186
/// // Instead, all fields that may be recorded after span creation should be declared up front,
11871187
/// // using field::Empty when a value is not known, as we did for `parting`.
11881188
/// // This `record` call will indeed replace field::Empty with "you will be remembered".
1189-
/// span.record("parting", &"you will be remembered");
1189+
/// span.record("parting", "you will be remembered");
11901190
/// ```
11911191
///
11921192
/// [`field::Empty`]: super::field::Empty
11931193
/// [`Metadata`]: super::Metadata
1194-
pub fn record<Q: ?Sized, V>(&self, field: &Q, value: &V) -> &Self
1194+
pub fn record<Q: ?Sized, V>(&self, field: &Q, value: V) -> &Self
11951195
where
11961196
Q: field::AsField,
11971197
V: field::Value,
@@ -1201,7 +1201,7 @@ impl Span {
12011201
self.record_all(
12021202
&meta
12031203
.fields()
1204-
.value_set(&[(&field, Some(value as &dyn field::Value))]),
1204+
.value_set(&[(&field, Some(&value as &dyn field::Value))]),
12051205
);
12061206
}
12071207
}
@@ -1614,4 +1614,10 @@ mod test {
16141614
impl AssertSync for Span {}
16151615
impl AssertSync for Entered<'_> {}
16161616
impl AssertSync for EnteredSpan {}
1617+
1618+
#[test]
1619+
fn test_record_backwards_compat() {
1620+
Span::current().record("some-key", &"some text");
1621+
Span::current().record("some-key", &false);
1622+
}
16171623
}

0 commit comments

Comments
 (0)