How to achieve `trait T: usize: From { ... }`?

⚓ Rust    📅 2026-03-15    👤 surdeus    👁️ 2      

surdeus

Hello,

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.

13 posts - 8 participants

Read full topic

🏷️ Rust_feed