admin管理员组文章数量:1406462
I have a specific problem I'm facing currently,
I'm designing a component system for an engine that I'm writing myself and I'd like to do something like this:
trait BaseComponent {
fn dimensions(&self) -> Dimensions;
fn topleftmost_point(&self) -> Point;
// other methods etc...
}
I want all components to implement this trait, but also one or more of these subtraits:
trait Component: BaseComponent {
type State;
fn draw(&mut self, ctx: &mut RenderContext<Self::State>) -> crate::Result<()>;
}
trait DynamicComponent: Component {
fn update(&mut self, ctx: &mut RenderContext<<Self as Component>::State>) -> crate::Result<()>;
}
trait AsyncComponent: BaseComponent {
type State;
fn poll_draw(self: Pin<&mut Self>, ctx: &mut AsyncRenderContext<'_, Self::State>) -> Poll<crate::Result<()>;
}
I also have planned for a async and blocking version of a stateless component, which is similar without the extra ctx
parameter.
My question is, would there be a way to automatically require any object that implements the BaseComponent
trait also implement one or more of these subtraits? It's not entirely necessary, but I feel like it could for sure eliminate some dumb errors I could potentially make.
UPDATE:
after about a day of trying different solutions, I found a system that at least partially works (it works for marker traits)
#![feature(marker_trait_attr)]
use core::marker::{PhantomData};
trait A
where
Comp<Self>: Coerce<dyn A>
{}
trait B: A {}
trait C: A {}
#[marker] trait Coerce<T: ?Sized> {}
impl<T: ?Sized + B> Coerce<dyn A> for Comp<T> {}
impl<T: ?Sized + C> Coerce<dyn A> for Comp<T> {}
struct Comp<T: ?Sized>(PhantomData<T>);
struct D;
struct E;
impl A for D {}
impl B for D {}
impl A for E {}
impl C for E {}
fn main() {}
the main issue of this, however, is that it is based off of unstable features, due to the marker trait attribute.
本文标签:
版权声明:本文标题:rust - Is there a way to require any struct that implements a base trait to also implement one of its subtraits? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745015428a2637810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论