Hi! I'm ANoobyBird, and I'm a newbie in rust, as well as programming as a whole, so I try my best to articulate clearly, and please pardon me and point out as you may if you find this post to be ambiguous or nonconstructive in any manner, or if there's already a way to achieve what is suggested in rust which I haven't noticed.
I'd like to suggest that, there should be a mechanism for a trait B
to be interrelated to another A
by stipulating that if one is to implement a type T
for B
, it surely must also implement A
, in a specific way. I must clarify that what I'm talking about is not a supertrait, a supertrait only puts the constraint but does not "know" how to satisfy the constraint.
For a really very rough and trivial and hopefully still clear enough illustration,
pub trait A {
fn method_a1(&self);
fn method_a2(&self) -> usize;
}
pub trait B {
fn method_b1(&self);
fn method_b2(&self) -> usize;
}
// Imaginary syntax
derive A for B {
fn method_a1(&self) {
self.method_b1();
}
fn method_a2(&self) -> usize {
self.method_b2()
}
}
// Now, any concrete type which has implemented B,
// will also automatically implement A.
// Something like that.
So, what I'm suggesting is essentially the ability for a trait B
to derive another, say A
, instead of having to implement A
as a supertrait for every concrete type T
.
Btw, of course such a derive may also be optional, depending on the specific design, say enabled only by writing #[derive(A)]
specifically above a type which has implemented B, but that's the specifics, all I'm suggesting here is the possibility for a trait itself, not just a type, to be able to automatically derive another trait.
Thank you for reading!
And thank steffahn, who pointed out a mistake in the original post which is now edited.