Skip to content

Commit

Permalink
tracing: add index API for Field (#2820)
Browse files Browse the repository at this point in the history
## Motivation

Expose the index of the field in order to support cases such as sending field information
by index instead of by the string name in Tokio Console. Details:
tokio-rs/console#462 (comment)

## Solution

Adds a new API which exposes the index of a field, which is how it is stored internally
(together with a reference to the `FieldSet` containing the ordered field names.

Signed-off-by: hi-rustin <[email protected]>
Co-authored-by: Hayden Stainsby <[email protected]>
  • Loading branch information
Rustin170506 and hds authored Nov 18, 2024
1 parent 91fedc1 commit 827cd14
Showing 1 changed file with 16 additions and 0 deletions.
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 {
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

0 comments on commit 827cd14

Please sign in to comment.