我试图实现一个使用通用组件的系统,但当我试图改变变量时,我总是遇到join方法的问题。下面是我的代码的简化版本:
use specs::{Component, prelude::*};
#[derive(Component, Debug, Default)]
#[storage(NullStorage)]
pub struct FooMarker;
#[derive(Component, Debug, Default)]
#[storage(VecStorage)]
pub struct Foo(pub f64);
pub trait Variable: Component + std::fmt::Debug + Default + Send + Sync + 'static {
fn get_value(&self) -> f64;
fn set_value(&mut self, value: f64);
}
impl Variable for Foo {
fn get_value(&self) -> f64 {
self.0
}
fn set_value(&mut self, value: f64) {
self.0 = value;
}
}
#[derive(Default)]
pub struct ChangeVariableSystem<T: Default=Foo> {
_phantom_variable: std::marker::PhantomData<T>,
}
impl <'a, T> System<'a> for ChangeVariableSystem<T>
where
T: Component + Variable + Default + Send + Sync + 'static,
{
type SystemData = (
ReadStorage<'a, FooMarker>,
WriteStorage<'a, T>,
);
fn run(&mut self, data: Self::SystemData) {
let (
markers,
mut variables,
) = data;
(&variables, &markers).join()
.for_each(|(variable, _)| {
variable.get_value();
println!("{:?}", variable);
});
(&mut variables, &markers).join()
.for_each(|(variable, _)| {
variable.set_value(1.0);
println!("{:?}", variable);
});
}
}
fn main() {
let mut world = World::new();
world.register::<Foo>();
world.register::<FooMarker>();
for _ in 0..10 {
world.create_entity()
.with(Foo(0.0))
.with(FooMarker)
.build();
}
let mut dispatcher = DispatcherBuilder::new()
.with(
ChangeVariableSystem::<Foo>::default(),
"change_variable_system",
&[],
).build();
}
字符串
在前面的代码中,我在第一个join中没有问题,因为变量没有变化,但在第二个中,我遇到了以下问题:
error[E0599]: the method `join` exists for tuple `(&mut Storage<'_, T, FetchMut<'_, MaskedStorage<T>>>, &Storage<'_, FooMarker, Fetch<'_,
MaskedStorage<FooMarker>>>)`, but its trait bounds were not satisfied
--> project\examples\generics.rs:60:36
|
60 | (&mut variables, &markers).join()
| ^^^^ method cannot be called due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`&mut Storage<'_, T, FetchMut<'_, MaskedStorage<T>>>: specs::Join`
which is required by `(&mut Storage<'_, T, FetchMut<'_, MaskedStorage<T>>>, &Storage<'_, FooMarker, Fetch<'_, MaskedStorage<FooMarker>>>): specs::Join`
`<T as specs::Component>::Storage: SharedGetMutStorage<T>`
which is required by `(&mut Storage<'_, T, FetchMut<'_, MaskedStorage<T>>>, &Storage<'_, FooMarker, Fetch<'_, MaskedStorage<FooMarker>>>): specs::Join`
For more information about this error, try `rustc --explain E0599`.
型
我不知道该怎么解决这个问题。组件特性绑定不应该就能解决这个问题吗?你能帮我吗?
我刚用0.19版本的规格试了同样的东西,它工作了。这是0.20版本的错误吗?
谢谢你,谢谢
1条答案
按热度按时间mnowg1ta1#
v0.20.0确实改变了这一点(添加了一个trait绑定)。现在需要在绑定中添加
where T::Storage: SharedGetMutStorage<T>
。您还需要在第二个闭包的开头添加
let variable = variable.access_mut();
,以访问该值。