Hello, since Rust 1.4 (the release I got into Rust), I’m struggling with cases where I need to define generic From conversions for any T: MyTrait, like here:
trait Trait: {}
impl Trait for u32 {}
impl Trait for u64 {}
struct MyType<T: Trait> {
pub t: T,
}
impl<T: Trait, U: Trait + From<T>> From<MyType<T>> for MyType<U> {
fn from(rhs: MyType<T>) -> Self {
MyType { t: U::from(rhs.t) }
}
}
https://play.rust-lang.org/?gist=2deed0da944c5ea2d1e4a60c54ddf75b&version=nightly&mode=debug
If I understand what’s specialization in Rust is going to be, this problem can be solved through it by changing the From<T> for T impl (and the respective Into) in stdlib with use of a “default” impl.
// From (and thus Into) is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> From<T> for T {
fn from(t: T) -> T { t }
}
=>
// From (and thus Into) is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> From<T> for T {
default fn from(t: T) -> T { t }
}
Since specialization is currently nightly, we can feature-gate it inside stdlib until it lands to stable.
I’m writing this post to open a pre-RFC discussion and figure out possible pitfalls in my vision and proposal before writing an RFC.
Please be welcome to correct me if I’m wrong!
UPD: checked on the current nightly and it doesn’t seem to work, though I don’t really understand why.
Can someone more familiar with specialization feature status explain how this should be implemented and if that’s possible at all?
https://play.rust-lang.org/?gist=e0d1db8fbcbd7c646fc8731f0ab99a39&version=nightly&mode=debug