#Impl Into for multiple types in collections?

13 messages · Page 1 of 1 (latest)

narrow lily
#

I have a type called ConnectionBundle that can be constructed from different sets of data. The following impls work:

impl From<(usize, Sample, NodeOutput)> for ConnectionBundle {...}
impl From<(&'static str, Sample, NodeOutput)> for ConnectionBundle {...}
impl From<(&'static str, Sample)> for ConnectionBundle {...}

But as soon as I put these inside another type like an array or a Vec I get an error like "note: multiple impls satisfying ConnectionBundle: From<[_; 1]> found"

impl<const N: usize> From<[(&'static str, Sample); N]> for ConnectionBundle {...}
impl<const N: usize> From<[(usize, Sample); N]> for ConnectionBundle {...}
impl<const N: usize> From<[(&'static str, Sample, NodeOutput); N]> for ConnectionBundle {...}

Is there a way around this to allow collections of different types as a function parameter using impl Into<ConnectionBundle>?

proven mantle
#

it kinda looks like a bug in the compiler

narrow lily
#

Definitely, I thought this might be a well known limitation. Here we go:

error[E0283]: type annotations needed
   --> src/graph/connection.rs:626:74
    |
626 |         let _c: ConnectionBundle = [("freq", 440.0, node.out(0)).into()].into();
    |                                                                          ^^^^
    |
note: multiple `impl`s satisfying `ConnectionBundle: From<[_; 1]>` found
   --> src/graph/connection.rs:446:1
    |
446 | impl<const N: usize> From<[(&'static str, Sample); N]> for ConnectionBundle {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
455 | impl<const N: usize> From<[(usize, Sample); N]> for ConnectionBundle {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
464 | impl<const N: usize> From<[(&'static str, Sample, NodeOutput); N]> for ConnectionBundle {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: required for `[_; 1]` to implement `Into<ConnectionBundle>`
help: try using a fully qualified path to specify the expected types
    |
626 |         let _c: ConnectionBundle = <[_; 1] as Into<ConnectionBundle>>::into([("freq", 440.0, node.out(0)).into()]);
    |                                    +++++++++++++++++++++++++++++++++++++++++                                     ~
proven mantle
#

oh now it makes sense

#

it's like you wrote foo.into().into()

#

the compiler has no way to know what the intermediate type is

#

why is the inner into there anyway?

#

it shouldn't need it

#

unless you also have impl From<[ConnectionBundle; N]> for ConnectionBundle

#

actually if you only have the impl I wrote then your code should work

#

if not, remove the into on the tuple

narrow lily
#

Ahh yes that makes sense!