Suppose I have:
pub struct Foo<X, Y, Z> {
// Here I can use X, Y, Z as types:
x: X, y: Y, z: Z
}
Then I can:
impl<X, Y, Z> Foo<X, Y, Z> {
// Here I can also use X Y Z as types...
fn set_x(&mut self, x: X) { self.x = x; }
}
Then suppose I have:
type MyFoo = Foo<u8, u16, u32>;
fn do_my_foo(my_foo: MyFoo) {
// Here I can't use X, Y, Z as types.
// What I want to write:
// x: MyFoo::X = 0;
// What I have to write:
x: u8 = 0;
my_foo.set_x(x);
}
Is this possible in today's Rust? If not, has this been suggested/discussed?
Considering a more general case, in C++ templates it is possible to manually add type aliases inside a struct (almost anywhere, really). This allows not only capturing the type parameters but also derived ("dependent") types. Rust-ish equivalent might look like:
pub struct Bar<Y, Z> { ... }
pub struct Foo<X, Y, Z> {
type X = X;
type Bar = Bar<Y, Z>;
x: X, y: Y, z: Z
}
type MyFoo = Foo<u8, u16, u32>;
let x: MyFoo::X = 0;
let bar: MyFoo::Bar = ...;
This is very useful when creating non-trivial generic data types in libraries. Is something along these lines possible in today's Rust, or is being considered?