Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a trait have a supertrait that is parameterized by a generic?

Can you do something like this in Rust?

trait A : forall<T> B<T> { ... }

That is, if we want:

impl A for D { ... }

We must first implement:

impl<T> B<T> for D { ... }
like image 787
Clinton Avatar asked Sep 16 '25 16:09

Clinton


2 Answers

No. Rust's type system doesn't currently support any features involving higher kinded types. It does, however, support a similar construction to what you described, but limited to lifetime parameters. For example:

trait B<'a> {}

trait A: for<'a> B<'a> {}

struct D;

impl A for D { }

This is an error:

error[E0277]: the trait bound `for<'a> D: B<'a>` is not satisfied
 --> src/lib.rs:7:6
  |
7 | impl A for D { }
  |      ^ the trait `for<'a> B<'a>` is not implemented for `D`

Until you add the blanket implementation:

impl<'a> B<'a> for D { }

It's not impossible that Rust will eventually add similar functionality for types too, but I wouldn't expect it any time soon.

like image 79
Peter Hall Avatar answered Sep 19 '25 04:09

Peter Hall


While the language is unable to specify such a higher kinded constraint, it is often possible to rethink the trait B so that its implementation is universal over this parameter type T.

Considering this example of B:

trait B<T> {
    fn foo(bar: T) -> u32;
}

A type that implements B for any T would be practically equivalent to the type implementing the trait below:

trait UniversalB {
    fn foo<T>(bar: T) -> u32;
}

This one declares the type parameters at method call level. While this trait is no longer object safe, it can still be used as a supertrait:

trait A: UniversalB {}

The implementations may need to be adjusted accordingly.

See also:

  • The Rust Programming Language, Chapter 10, Section 1 on Generic Data Types In Method Definitions
like image 39
E_net4 stands with Ukraine Avatar answered Sep 19 '25 05:09

E_net4 stands with Ukraine