作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的游戏规则引擎,我有一个称为Rule
的中心特征,可以处理游戏回调。 Rule
有两种类型:BaseRule
适用于游戏中的任何实体,而CreatureRule
仅适用于Creatures。目前,我的代码结构如下:
trait BaseRule<T> {
fn on_turn_start(&self, owner: &T) {}
}
#[typetag::serde(tag = "type")]
pub trait CreatureRule: BaseRule<Creature> {
fn on_death(&self, owner: &Creature) {}
}
Rule
和
CreatureRule
。我试图对
BaseRule
进行全面的实现:
impl<R: CreatureRule> BaseRule<Creature> for R {
}
BaseRule
特性的新实现,例如通过
impl BaseRule<Creature> for BaseMeleeDamageAttack {
fn on_turn_start(&self, owner: &Creature) {
// do stuff
}
}
BaseRule
的类型提供
CreatureRule
的全面默认实现,但仍然允许它们覆盖函数的默认实现?
CreatureRule
上使用泛型类型参数,因为Serde序列化不适用于具有泛型类型的特征。)
最佳答案
我认为您必须为每一个手动实现它,但是只有一行:
impl BaseRule<Creature> for ... {}
关于rust - 覆盖Rust中的一揽子特质实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62011235/
我是一名优秀的程序员,十分优秀!