Rust conflicting implementation but not same traits

The compiler don't let me implement a trait for two similar type,

error[E0119]: conflicting implementations of trait `std::Synth::Spawner<_, _>`:
  --> src\std\mod.rs:86:5
   |
52 |     impl<U: Send + 'static, T: Parameter + Iterator<Item = U> + Send + 'static> Spawner<U, T> for T {
   |     ----------------------------------------------------------------------------------------------- first implementation here
...
86 |     impl<U: Send + 'static, T: Iterator<Item = U> + Send + 'static> Spawner<U, T> for T {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

I understand that it could be tricky, so a larger question would be, how to deal gracefully with some situation like :

trait F for X -> trait bounds...
{
code code....
code code...
}

and

trait F for X -> trait bounds... + Another trait
{
same code code....
TheAnotherTrait();
same code code...
}

thank you !

This requires specialization, which is available on nightly. (note: specialization is unsound because lifetime specialization is unsound, this can bite in subtle, easy to miss ways, and the sound subset min_specialization does not handle specializing with an extra trait bound. specialization is likely to change in significant ways because of this).

Woaw thank you.

I do nightly rust 8)

Hmm, but now I'm hitting again this wall ... Sorry about that

I wanna do a classic Input data struct to be like that.
It's a classic input that can be a scalar or a signal.
I was thinking doing it with specialization, and even if I added the attribute, it does not work...
It's always the same error E0119

pub struct Input<T> {
    input: T,
}

impl<T: Iterator<Item = f32>> Iterator for Input<T> {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        self.input.next()
    }
}

impl Iterator for Input<f32> {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        Some(self.input)
    }
}
error[E0119]: conflicting implementations of trait `std::iter::Iterator` for type `std::Input<f32>`:
  --> src\std\mod.rs:68:1
   |
60 | impl<T: Iterator<Item = f32>> Iterator for Input<T> {
   | --------------------------------------------------- first implementation here
...
68 | impl Iterator for Input<f32> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::Input<f32>`
   |
   = note: upstream crates may add a new impl of trait `std::iter::Iterator` for type `f32` in future versions

You can't do that. Rust doesn't know that Iterator will never be implemented for f32, so you can't make potentially conflicting impls. You can modify your code a bit to make it work

pub struct Input<T> {
    input: T,
}

impl<T: Iterator<Item = f32>> Iterator for Input<T> {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        self.input.next()
    }
}

// use Input<std::iter::Repeat<f32>> instead
/* impl Iterator for Input<f32> {
    type Item = f32;

    fn next(&mut self) -> Option<Self::Item> {
        Some(self.input)
    }
} */

okay. Thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.