Skip to content

Commit 827cd14

Browse files
Rustin170506hds
andauthored
tracing: add index API for Field (#2820)
## 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]>
1 parent 91fedc1 commit 827cd14

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tracing-core/src/field.rs

+16
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ impl Field {
681681
pub fn name(&self) -> &'static str {
682682
self.fields.names[self.i]
683683
}
684+
685+
/// Returns the index of this field in its [`FieldSet`].
686+
pub fn index(&self) -> usize {
687+
self.i
688+
}
684689
}
685690

686691
impl fmt::Display for Field {
@@ -1053,6 +1058,17 @@ mod test {
10531058
assert!(valueset.is_empty());
10541059
}
10551060

1061+
#[test]
1062+
fn index_of_field_in_fieldset_is_correct() {
1063+
let fields = TEST_META_1.fields();
1064+
let foo = fields.field("foo").unwrap();
1065+
assert_eq!(foo.index(), 0);
1066+
let bar = fields.field("bar").unwrap();
1067+
assert_eq!(bar.index(), 1);
1068+
let baz = fields.field("baz").unwrap();
1069+
assert_eq!(baz.index(), 2);
1070+
}
1071+
10561072
#[test]
10571073
fn empty_value_set_is_empty() {
10581074
let fields = TEST_META_1.fields();

0 commit comments

Comments
 (0)