Description
Given the following code: (Playground link not provided because this involves a crate not on playground)
use rayon::iter::IntoParallelIterator;
use dashmap::DashSet;
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Foo;
let test = DashSet::<Foo>::new();
test.par_iter();
The current output is:
error[E0599]: the method `par_iter` exists for struct `DashSet<Foo>`, but its trait bounds were not satisfied
--> src/lib.rs:74:14
|
74 | test.par_iter();
| ^^^^^^^^ method cannot be called on `DashSet<Foo>` due to unsatisfied trait bounds
|
::: /home/edward/.cargo/registry/src/git.colasdn.top-1ecc6299db9ec823/dashmap-4.0.2/src/set.rs:19:1
|
19 | pub struct DashSet<K, S = RandomState> {
| -------------------------------------- doesn't satisfy `DashSet<Foo>: IntoParallelRefIterator`
|
= note: the following trait bounds were not satisfied:
`&DashSet<Foo>: IntoParallelIterator`
which is required by `DashSet<Foo>: IntoParallelRefIterator`
Ideally the output should look like:
error[E0599]: the method `par_iter` exists for struct `DashSet<Foo>`, but its trait bounds were not satisfied
--> src/lib.rs:74:14
|
74 | test.par_iter();
| ^^^^^^^^ method cannot be called on `DashSet<Foo>` due to unsatisfied trait bounds
|
::: /home/edward/.cargo/registry/src/git.colasdn.top-1ecc6299db9ec823/dashmap-4.0.2/src/set.rs:19:1
|
19 | pub struct DashSet<K, S = RandomState> {
| -------------------------------------- doesn't satisfy `DashSet<Foo>: IntoParallelRefIterator`
|
= note: the following trait bounds were not satisfied:
`&DashSet<Foo>: IntoParallelIterator`
which is required by `DashSet<Foo>: IntoParallelRefIterator`
= note: `dashmap` has an optional feature `rayon` that is currently not enabled.
I was trying to call par_iter()
on the following example, but was very confused with regards to the error message. Consider the following image, snapshotted from the dashmap::DashSet
docs:
It looks like Foo
satisfies all the trait bounds needed for DashSet<Foo>
to fufill InterParallelIterator
, yet the trait still isn't being satisfied! What's going on? The solution is a case of either confusing docs.rs or a perhaps unintuitive diagnostic: a missing feature flag (in this case, rayon
).
As a quick concept, I would suggest a note indicating that the trait implementation does exist for a struct but isn't enabled because the feature isn't enabled. As a layman on the rustc internals, I'm not sure how feasible this would be, but something like the following might be viable:
- On a failed trait bound check
Trait
, - Check if struct
Foo
from cratecrate
implementsTrait
regardless of enabled features, - If so, check if the
Trait
implementation or its parent mods is enabled through a feature, and that feature isn't enabled, - If so, add a note saying that
Trait
is not satisfied because the trait implementation is gated by a feature flag.
I think this is worthwhile to implement, as I believe this is incredibly confusing to new Rust users. The compiler and docs make no indication that a trait implementation is gated, which means that a new user would be absolutely lost on next steps.
Heck, I would consider myself an intermediate or even an advanced user, and had to ask the community for help.