Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ValueSets to be constructed with slices in addition to arrays #2787

Open
wants to merge 2 commits into
base: v0.1.x
Choose a base branch
from

Conversation

maddiemort
Copy link

@maddiemort maddiemort commented Nov 3, 2023

Motivation

I'm working on an implementation of Subscriber that needs to be able to store (in a form as close as possible to the original) the inputs to its methods, and reconstruct them later to be passed to another Subscriber. For the most part, this works really well, but the biggest sticking point by far came from trying to reconstruct a ValueSet from a stored list of (Field, Value) pairs.

The core of the problem is the ValidLen trait and its usage by FieldSet::value_set(). In order to call that function, you have to be able to provide a statically-sized array, because that's the only type that implements ValidLen.

While, in theory, constructing an array for my use case should be just about possible (although the tricks necessary to do so likely involve a macro and the reimposition of an arbitrary length limit), lifetime issues (that appear to result from a call to <[_; N]>::try_from()) make it impossible. I'm going to construct a minimal example of this and investigate further at some point, but in the meantime:

Solution

Since #2508 removed the limitation on array length here, there doesn't seem to be a drawback to simply adding an implementation of ValidLen for slices. They already meet the Borrow supertrait requirement, so no additional machinery is needed.

This makes this API trivially easy to call for my use case, and I can't think of any associated drawbacks.

There used to be a length restriction on the arrays usable to construct
`ValueSet`s, but this was due to Rust compiler limitations that no
longer exist. After that restriction was removed, the `ValidLen` trait
was kept around but implemented for statically-sized arrays of all
lengths.

While in theory this allows `ValueSet`s to be constructed from arrays of
arbitrary length, the size of the arrays must still be known at compile
time. Now that there is no longer a restriction on the length of arrays,
slices could in theory be used as well (since they meet the supertrait
requirement of `ValidLen`), but no implementation of the trait exists.

Adding an implementation of `ValidLen` for slices is straightforward.

Refs: tokio-rs#2508
@maddiemort maddiemort requested review from hawkw and a team as code owners November 3, 2023 13:45
@maddiemort
Copy link
Author

I'm not entirely sure why CI is failing, but I don't think it's related to my change.

@hawkw
Copy link
Member

hawkw commented Nov 3, 2023

This change makes sense to me. The CI failure is due to a yanked dependency breaking our build with -Z minimal-versions, that's not related to this change.

tracing-core/src/field.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants