Skip to content

Commit

Permalink
Implement ValidLen for all sizes of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
eopb committed Sep 5, 2023
1 parent 683cc8a commit 971732e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
25 changes: 2 additions & 23 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,6 @@ impl FieldSet {
}

/// Returns a new `ValueSet` with entries for this `FieldSet`'s values.
///
/// Note that a `ValueSet` may not be constructed with arrays of over 32
/// elements.
#[doc(hidden)]
pub fn value_set<'v, V>(&'v self, values: &'v V) -> ValueSet<'v>
where
Expand Down Expand Up @@ -957,28 +954,10 @@ impl<'a> fmt::Display for ValueSet<'a> {
mod private {
use super::*;

/// Marker trait implemented by arrays which are of valid length to
/// construct a `ValueSet`.
///
/// `ValueSet`s may only be constructed from arrays containing 32 or fewer
/// elements, to ensure the array is small enough to always be allocated on the
/// stack. This trait is only implemented by arrays of an appropriate length,
/// ensuring that the correct size arrays are used at compile-time.
/// Restrictions on `ValueSet` lengths were removed in #2508 but this type remains for backwards compatibility.
pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (dyn Value + 'a)>)]> {}
}

macro_rules! impl_valid_len {
( $( $len:tt ),+ ) => {
$(
impl<'a> private::ValidLen<'a> for
[(&'a Field, ::core::option::Option<&'a (dyn Value + 'a)>); $len] {}
)+
}
}

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

#[cfg(test)]
Expand Down
16 changes: 0 additions & 16 deletions tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,22 +386,6 @@
//! span.record("parting", &"goodbye world!");
//! ```
//!
//! Note that a span may have up to 32 fields. The following will not compile:
//!
//! ```rust,compile_fail
//! # use tracing::Level;
//! # fn main() {
//! let bad_span = span!(
//! Level::TRACE,
//! "too many fields!",
//! a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9,
//! j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16, q = 17,
//! r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, x = 24, y = 25,
//! z = 26, aa = 27, bb = 28, cc = 29, dd = 30, ee = 31, ff = 32, gg = 33
//! );
//! # }
//! ```
//!
//! Finally, events may also include human-readable messages, in the form of a
//! [format string][fmt] and (optional) arguments, **after** the event's
//! key-value fields. If a format string and arguments are provided,
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! override their default values.
//! - The span's [verbosity level]
//! - A string literal providing the span's name.
//! - Finally, between zero and 32 arbitrary key/value fields.
//! - Finally, zero or more arbitrary key/value fields.
//!
//! [`target`]: super::Metadata::target()
//!
Expand Down
42 changes: 42 additions & 0 deletions tracing/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,48 @@ fn span_with_non_rust_symbol() {
span!(Level::TRACE, "non-rust", "guid:x-request-id" = "abcdef");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn large_span() {
span!(
Level::TRACE,
"spans with more than 32 fields have been supported since #2508",
a = 1,
b = 2,
c = 3,
d = 4,
e = 5,
f = 6,
g = 7,
h = 8,
i = 9,
j = 10,
k = 11,
l = 12,
m = 13,
n = 14,
o = 15,
p = 16,
q = 17,
r = 18,
s = 19,
t = 20,
u = 21,
v = 22,
w = 23,
x = 24,
y = 25,
z = 26,
aa = 27,
bb = 28,
cc = 29,
dd = 30,
ee = 31,
ff = 32,
gg = 33
);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn event() {
Expand Down

0 comments on commit 971732e

Please sign in to comment.