Skip to content

Commit b6cb99e

Browse files
Rustin170506hds
andcommitted
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 9210501 commit b6cb99e

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
@@ -804,6 +804,11 @@ impl Field {
804804
pub fn name(&self) -> &'static str {
805805
self.fields.names[self.i]
806806
}
807+
808+
/// Returns the index of this field in its [`FieldSet`].
809+
pub fn index(&self) -> usize {
810+
self.i
811+
}
807812
}
808813

809814
impl fmt::Display for Field {
@@ -1174,6 +1179,17 @@ mod test {
11741179
assert!(valueset.is_empty());
11751180
}
11761181

1182+
#[test]
1183+
fn index_of_field_in_fieldset_is_correct() {
1184+
let fields = TEST_META_1.fields();
1185+
let foo = fields.field("foo").unwrap();
1186+
assert_eq!(foo.index(), 0);
1187+
let bar = fields.field("bar").unwrap();
1188+
assert_eq!(bar.index(), 1);
1189+
let baz = fields.field("baz").unwrap();
1190+
assert_eq!(baz.index(), 2);
1191+
}
1192+
11771193
#[test]
11781194
fn empty_value_set_is_empty() {
11791195
let fields = TEST_META_1.fields();

0 commit comments

Comments
 (0)