0

This struct holds a field that needs to have a lot of different traits.

struct ManyRequirements<T> 
    where
        T:Ord,
        T:Copy,
        T:Clone,
        T:Iterator,
        T:Display
{
    something: T
}

I want to write methods for it that work regardless of T, like a "new" method for example. I just want to be able to say ManyRequirements::new() and have it work properly. But if I tried,

impl ManyRequirements {
    fn new() {

    }
}

or

impl<T> ManyRequirements<T> {
    fn new() {

    }
}

I get

the trait bound `T: Ord` is not satisfied
the trait `Ord` is not implemented for `T`

repeated for each trait T failed to be implemented for. The only thing that works is

impl <T> ManyRequirements<T> 
    where
        T:Ord,
        T:Copy,
        T:Clone,
        T:Iterator,
        T:Display
{

}

Is there a shorter way to say "T can be whatever"? Something like

impl <_> ManyRequirements<_> 
{

}
Jay Mehta
  • 21
  • 4
  • 6
    But `T` _can't_ be whatever, since `ManyRequirements` requires it to implement those traits. If it actually doesn't, you code omit the where clause on `ManyRequirements`'s definition and only include the traits constraints on the impl block that need them – Brian61354270 Mar 23 '23 at 16:28
  • 3
    Also that is one of the reasons trait constraints are usually on `impl` blocks rather than on structures. And you can use `+` to constraint types on multiple traits rather than repeating `T:` every time e.g. `impl ManyRequirements where T: Ord + Copy + Clone + Iterator + Display`. Or just put the relevant traits on the impls where they are needed e.g. none of these traits is useful to create a `ManyRequirements` (possibly aside from `Copy`), however `Default` might be if the ctor is parameter-less. – Masklinn Mar 23 '23 at 16:56
  • @Brian61354270 but why does Rust require me to specifically say in the impl that the struct's generic has to implement all of those traits, when it already knows this since I declared it in the struct declaration? Is there a way to have multiple impl blocks that all require T to implement those traits without a lot of repeated code? – Jay Mehta Mar 23 '23 at 22:53
  • should clarify: I'm trying to use the type state pattern like in [this video](https://youtu.be/_ccDqRTx-JU) but I want my struct to also have a variable that implements some specific traits. For this pattern, you have to make multiple impl blocks, but I want each block to have the same requirements for what that one generic type should implement. Do I have to repeat that the type needs to implement those traits in each block or is there a way to do it without repitition? – Jay Mehta Mar 23 '23 at 23:06
  • This feature is stalled here: https://github.com/rust-lang/rust/issues/44491. [dhardy suggests](https://github.com/rust-lang/rust/issues/44491#issuecomment-1088375079) using [`impl_tools::impl_scope!`](https://crates.io/crates/impl-tools#impl-scope). – Solomon Ucko Mar 24 '23 at 01:07
  • Duplicate of: https://stackoverflow.com/q/57262540/5445670, https://stackoverflow.com/q/49434716/5445670, https://stackoverflow.com/q/49229332/5445670, https://stackoverflow.com/q/72753654/5445670 – Solomon Ucko Mar 24 '23 at 01:10
  • 1
    Does this answer your question? [Should trait bounds be duplicated in struct and impl?](https://stackoverflow.com/questions/49229332/should-trait-bounds-be-duplicated-in-struct-and-impl) – Solomon Ucko Mar 24 '23 at 01:10
  • @SolomonUcko those answer my question, thank you! is there a way to mark the question as answered by you or something like that – Jay Mehta Mar 27 '23 at 18:59
  • @JayMehta You're welcome, thanks! I've now added an answer, so you can click the checkmark and possibly the upvote button. – Solomon Ucko Mar 27 '23 at 19:09