I would like to achieve something like the following:
trait MyTrait:
usize: From<Self>
{
...
}
... to effectively require that for implementations of trait MyTrait, there exists an impl of impl<T: MyTrait> From<T> for usize { ... }. The end-goal is to be able to use the trait in the following circumstance:
fn a(b: impl MyTrait) {
let c = usize::from(b);
...
}
How can I introduce this bound, or implementation on the trait itself?
Thanks.
EDIT: I dislike (rightfully so, I think) the ergonomics of .into(), and prefer to explicitly use ::from(...) wherever possible.
Dang, you're right, I misunderstood that link completely! I knew I shouldn't post at 2 AM, but apparently I shouldn't post at 6 AM either. (Pretty much everything with AM is pretty suspect, honestly...)
Means: yes, it's a bug and should be needed, but all… it existed for 11 years expected time to fix is 11 years, too. Lindy's law sounds comical when you first hear it, but works eerily good.
// demo impl
impl MyTrait for u16 {}
// demo use in function signature
fn f<T: MyTrait>(x: T, y: T) {
let n: usize = x.into();
let m = usize::from(y);
}
I think this is what the edit/addition in the OP relates to:
though IMHO, this solution of “just use Into” is additionally very much specific to this case anyway, while the general question can stay relevant for all kinds of other settings