#Use type parameter when implementing trait

4 messages · Page 1 of 1 (latest)

pseudo mulch
#

Hi, why doesn't the following compile:

trait A {}

impl<T> Into<usize> for T
where
    T: A
{
    fn into(self) -> usize {
        todo!();
    }
}

It fails with the error:

   Compiling playground v0.0.1 (/playground)
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
 --> src/lib.rs:3:6
  |
3 | impl<T> Into<usize> for T
  |      ^ type parameter `T` must be used as the type parameter for some local type
  |
  = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
  = note: only traits defined in the current crate can be implemented for a type parameter

For more information about this error, try `rustc --explain E0210`.
error: could not compile `playground` due to previous error

But the trait A isn't a foreign trait?

grim kraken
#

with the where clause, you're only saying it should implement trait A, not that you're actively implementing trait A
you're actually implementing Into for a foreign type

pseudo mulch
#

right makes sense. so is there no way to do what i'm trying to do?

#

because currently i'm using macros but it would be nicer to use traits