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 committed Nov 22, 2024
1 parent 345fbff commit f6a6c8c
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 @@ -804,6 +804,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 @@ -1174,6 +1179,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 f6a6c8c

Please sign in to comment.