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

Use Cow<'static, str> for Metadata strings #1020

Closed
wants to merge 29 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5868a6d
[WIP] Metadata name is Cow<'static, str>
dvdplm Sep 28, 2020
f40de7b
Fetch patch from git
dvdplm Sep 29, 2020
ed04e30
Clean up MockSpan
dvdplm Sep 29, 2020
645c133
Don't clone the name in Metadata::name
dvdplm Sep 30, 2020
5a69f07
Use Cow in assert_last_closed
dvdplm Sep 30, 2020
f922cc1
If Metadata is always used with a 'static lifetime it might make sens…
dvdplm Sep 30, 2020
fa4a808
cleanup
dvdplm Sep 30, 2020
183f75f
Merge branch 'master' into dp-use-cow
dvdplm Oct 5, 2020
97e5b31
Metadata.target is Cow – breaks AsLog impl
dvdplm Oct 6, 2020
028ae65
Make it build (ty Ben)
dvdplm Oct 6, 2020
a3661f8
Make tests pass
dvdplm Oct 6, 2020
f0a6443
Undo changes to TestTracer and patched opentelemetry, not needed
dvdplm Oct 6, 2020
a9a0568
Remove patch
dvdplm Oct 6, 2020
2ee5da9
cleanup
dvdplm Oct 6, 2020
d8afcbe
cleanup
dvdplm Oct 6, 2020
381d574
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 7, 2020
8913759
Store file as Cow
dvdplm Oct 7, 2020
b250109
Store module_path as Cow
dvdplm Oct 7, 2020
e29d3ca
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 16, 2020
f7274dc
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 19, 2020
b3bec86
Feature gate usage of Cow in Metadata
dvdplm Oct 19, 2020
fda189b
Add constructor for dynamic data
dvdplm Oct 19, 2020
c46c352
No need for extra lifetime
dvdplm Oct 19, 2020
7b0dfe5
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 19, 2020
280f0bf
Use impl Into<Cow<'a, str>
dvdplm Oct 20, 2020
4225d96
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 22, 2020
f42a644
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 23, 2020
47e9cd5
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
Oct 28, 2020
de588f4
Merge remote-tracking branch 'origin/master' into dp-target-is-cow
dvdplm Dec 14, 2020
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
80 changes: 80 additions & 0 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,35 @@ impl<'a> Metadata<'a> {
}
}

/// Construct new metadata for a span or event, with a name, target, level, field
/// names, and optional source code location using dynamically allocated data.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
pub fn from_cow<S>(
name: S,
target: S,
level: Level,
file: Option<S>,
line: Option<u32>,
module_path: Option<S>,
fields: field::FieldSet,
kind: Kind,
) -> Self
where
S: Into<Cow<'a, str>>,
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
{
Metadata {
name: name.into(),
target: target.into(),
level,
module_path: module_path.map(Into::into),
file: file.map(Into::into),
line,
fields,
kind
}
}

/// Returns the names of the fields on the described span or event.
pub fn fields(&self) -> &field::FieldSet {
&self.fields
Expand Down Expand Up @@ -837,6 +866,13 @@ impl PartialOrd<Level> for LevelFilter {
mod tests {
use super::*;
use core::mem;
#[cfg(feature = "alloc")]
use crate::{
callsite::{Callsite, Identifier},
field::FieldSet,
Interest,
};


#[test]
fn level_from_str() {
Expand Down Expand Up @@ -900,4 +936,48 @@ mod tests {
assert_eq!(expected, repr, "repr changed for {:?}", filter)
}
}

#[cfg(feature = "alloc")]
#[test]
fn create_metadata_from_dynamic_data() {
struct TestCallsite;
static CS1: TestCallsite = TestCallsite;

impl Callsite for TestCallsite {
fn set_interest(&self, _interest: Interest) {}
fn metadata(&self) -> &Metadata<'_> {
unimplemented!("not needed for this test")
}
}
let callsite_id = Identifier(&CS1);
let field_set = FieldSet::new(&["one", "fine", "day"], callsite_id);

// Use `String`s
let _metadata = Metadata::from_cow(
"a name".to_string(),
"a target".to_string(),
Level::TRACE,
None,
None,
None,
field_set,
Kind::EVENT,
);

let callsite_id = Identifier(&CS1);
let field_set = FieldSet::new(&["one", "fine", "day"], callsite_id);
// Use `str`s
let _metadata = Metadata::from_cow(
"a name",
"a target",
Level::TRACE,
None,
None,
None,
field_set,
Kind::EVENT,
);

// TODO dp: come up with a test that makes sense.
}
}