Skip to content

Commit 3e1f23e

Browse files
eopbdavidbarsky
authored andcommitted
core: allow ValueSets of any length (#2508)
## Motivation Fixes: #1566 ## Solution This change removes the maximum of 32 fields limitation using const generics. Having this arbitrary restriction in place to prevent stack overflows feels a little misplaced to me since stack size varies between environments.
1 parent 02044dd commit 3e1f23e

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

tracing-core/src/field.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,6 @@ impl FieldSet {
879879
}
880880

881881
/// Returns a new `ValueSet` with entries for this `FieldSet`'s values.
882-
///
883-
/// Note that a `ValueSet` may not be constructed with arrays of over 32
884-
/// elements.
885882
#[doc(hidden)]
886883
pub fn value_set<'v, V>(&'v self, values: &'v V) -> ValueSet<'v>
887884
where
@@ -1080,28 +1077,10 @@ impl<'a> fmt::Display for ValueSet<'a> {
10801077
mod private {
10811078
use super::*;
10821079

1083-
/// Marker trait implemented by arrays which are of valid length to
1084-
/// construct a `ValueSet`.
1085-
///
1086-
/// `ValueSet`s may only be constructed from arrays containing 32 or fewer
1087-
/// elements, to ensure the array is small enough to always be allocated on the
1088-
/// stack. This trait is only implemented by arrays of an appropriate length,
1089-
/// ensuring that the correct size arrays are used at compile-time.
1080+
/// Restrictions on `ValueSet` lengths were removed in #2508 but this type remains for backwards compatibility.
10901081
pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (dyn Value + 'a)>)]> {}
1091-
}
1092-
1093-
macro_rules! impl_valid_len {
1094-
( $( $len:tt ),+ ) => {
1095-
$(
1096-
impl<'a> private::ValidLen<'a> for
1097-
[(&'a Field, ::core::option::Option<&'a (dyn Value + 'a)>); $len] {}
1098-
)+
1099-
}
1100-
}
11011082

1102-
impl_valid_len! {
1103-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1104-
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
1083+
impl<'a, const N: usize> ValidLen<'a> for [(&'a Field, Option<&'a (dyn Value + 'a)>); N] {}
11051084
}
11061085

11071086
#[cfg(test)]

tracing/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -386,22 +386,6 @@
386386
//! span.record("parting", &"goodbye world!");
387387
//! ```
388388
//!
389-
//! Note that a span may have up to 32 fields. The following will not compile:
390-
//!
391-
//! ```rust,compile_fail
392-
//! # use tracing::Level;
393-
//! # fn main() {
394-
//! let bad_span = span!(
395-
//! Level::TRACE,
396-
//! "too many fields!",
397-
//! a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9,
398-
//! j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16, q = 17,
399-
//! r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, x = 24, y = 25,
400-
//! z = 26, aa = 27, bb = 28, cc = 29, dd = 30, ee = 31, ff = 32, gg = 33
401-
//! );
402-
//! # }
403-
//! ```
404-
//!
405389
//! Finally, events may also include human-readable messages, in the form of a
406390
//! [format string][fmt] and (optional) arguments, **after** the event's
407391
//! key-value fields. If a format string and arguments are provided,

tracing/src/span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! override their default values.
2323
//! - The span's [verbosity level]
2424
//! - A string literal providing the span's name.
25-
//! - Finally, between zero and 32 arbitrary key/value fields.
25+
//! - Finally, zero or more arbitrary key/value fields.
2626
//!
2727
//! [`target`]: super::Metadata::target
2828
//!

tracing/tests/macros.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,48 @@ fn span_with_non_rust_symbol() {
308308
span!(Level::TRACE, "non-rust", "guid:x-request-id" = "abcdef");
309309
}
310310

311+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
312+
#[test]
313+
fn large_span() {
314+
span!(
315+
Level::TRACE,
316+
"spans with more than 32 fields have been supported since #2508",
317+
a = 1,
318+
b = 2,
319+
c = 3,
320+
d = 4,
321+
e = 5,
322+
f = 6,
323+
g = 7,
324+
h = 8,
325+
i = 9,
326+
j = 10,
327+
k = 11,
328+
l = 12,
329+
m = 13,
330+
n = 14,
331+
o = 15,
332+
p = 16,
333+
q = 17,
334+
r = 18,
335+
s = 19,
336+
t = 20,
337+
u = 21,
338+
v = 22,
339+
w = 23,
340+
x = 24,
341+
y = 25,
342+
z = 26,
343+
aa = 27,
344+
bb = 28,
345+
cc = 29,
346+
dd = 30,
347+
ee = 31,
348+
ff = 32,
349+
gg = 33
350+
);
351+
}
352+
311353
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
312354
#[test]
313355
fn event() {

0 commit comments

Comments
 (0)