diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index 52d1dcf8a..c09b86359 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -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 { @@ -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();