In C++ I can write a template that takes a template parameter:
struct None;
template <class T>
struct FirstType {
using type = None;
};
template <template <class...> class T, class Head, class... Tail>
struct FirstType<T<Head, Tail...>> {
using type = Head;
};
This is impossible in Rust:
// doesn't work
trait FirstType {
type Output;
}
impl<T, Arg> FirstType for T<Arg> {
// ^^^ type argument not allowed
type Output = Arg;
}
But it's possible for the specific type:
struct Foo<T> {/* some stuff */}
impl<T> FirstType for Foo<T> {
type Output = T;
}
I don't understand why we can implement a trait for any type T
or for something like *mut T
, but not for T<U>
.
Is there any workaround?