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

tracing: add index API for Field #2820

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Changes from all 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
16 changes: 16 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ impl Field {
pub fn name(&self) -> &'static str {
self.fields.names[self.i]
}

/// Returns the index of this field in its [`FieldSet`].
pub fn index(&self) -> usize {
hds marked this conversation as resolved.
Show resolved Hide resolved
self.i
}
}

impl fmt::Display for Field {
Expand Down Expand Up @@ -1053,6 +1058,17 @@ mod test {
assert!(valueset.is_empty());
}

#[test]
fn index_of_field_in_fieldset_is_correct() {
let fields = TEST_META_1.fields();
let foo = fields.field("foo").unwrap();
assert_eq!(foo.index(), 0);
let bar = fields.field("bar").unwrap();
assert_eq!(bar.index(), 1);
let baz = fields.field("baz").unwrap();
assert_eq!(baz.index(), 2);
}

#[test]
fn empty_value_set_is_empty() {
let fields = TEST_META_1.fields();
Expand Down
Loading