(Sorry if this has been discussed before, this combination of keywords obviously returns a lot of results about existing features)
I’ve sometimes needed a function to be generic over a trait, rather than a type. One use case is transmuting a trait object reference to a std::raw::TraitObject, in a generic fashion.
fn foo<T: ?Sized>(x: &T) -> TraitObject {
transmute(x)
}
This doesn’t work as T could be sized, or another DST (slice or str).
The compiler complains that x might have the wrong size (one word, rather than 2 for TraitObject).
transmute_copy can be used as a workaround, but is very much UB if T is not a trait object.
My suggestion is to allow T to be restricted to a trait :
fn foo<trait T>(x: &T);
I think the syntax fits nicely with the various const x: u32 generics proposals.
The keyword before the variable refers to the style (avoiding the words type and kind on purpose here) of variable, and can be either const, trait, type (the default) or an apostrophe for lifetimes.
fn foo<'a, trait T: Debug, type U: T, const x: u32>(data: [U; x]) {
println!("{:?}", data);
}
(Just to be clear, this proposal is orthogonal to value generics proposed by RFC 1657 and others, the syntax just fits well)