Why won't Write<R> initialize R if it's missing?
farnoy opened this issue · comments
I'm kind of missing the point of Write. It requires a Default instance to initialize the resource, but still panics if used without initializing it manually. So what is the difference from WriteExpect?
It should not panic, and I never heard of the initialization not working.
Do you have a small example?
Not on hand, I'll try to reproduce a small one today.
OK, I think this is it, on specs 0.12.1
:
extern crate specs;
use specs::prelude::*;
pub struct TestData {
test: Option<u64>,
}
impl Default for TestData {
fn default() -> TestData {
TestData {
test: None,
}
}
}
pub struct TestSystem;
impl<'a> System<'a> for TestSystem {
type SystemData = Write<'a, TestData>;
fn run(&mut self, mut test_data: Self::SystemData) {
test_data.test = Some(16);
}
}
fn main() {
let mut world = specs::World::new();
let mut dispatcher = specs::DispatcherBuilder::new()
.with(TestSystem, "test_system", &[])
.build();
dispatcher.dispatch(&world.res);
}
I get this when running:
thread '<unnamed>' panicked at 'Tried to fetch a resource, but the resource does not exist.
Try adding the resource by inserting it manually or using the `setup` method.
You can get the type name of the missing resource by enabling `shred`'s `nightly` feature', /home/kuba/.cargo/registry/src/github.com-1ecc6299db9ec823/shred-0.7.0/src/res/mod.rs:215:48
note: Run with `RUST_BACKTRACE=1` for a backtrace.
If I skip the implementation of Default
, it won't compile, but it doesn't seem to use it?
Very short answer: `Dispatcher::setup`
…On Tue, Aug 14, 2018, 16:19 Jakub Okoński ***@***.***> wrote:
OK, I think this is it, on specs 0.12.1:
extern crate specs;
use specs::prelude::*;
pub struct TestData {
test: Option<u64>,
}
impl Default for TestData {
fn default() -> TestData {
TestData {
test: None,
}
}
}
pub struct TestSystem;
impl<'a> System<'a> for TestSystem {
type SystemData = Write<'a, TestData>;
fn run(&mut self, mut test_data: Self::SystemData) {
test_data.test = Some(16);
}
}
fn main() {
let mut world = specs::World::new();
let mut dispatcher = specs::DispatcherBuilder::new()
.with(TestSystem, "test_system", &[])
.build();
dispatcher.dispatch(&world.res);
}
I get this when running:
thread '<unnamed>' panicked at 'Tried to fetch a resource, but the resource does not exist.
Try adding the resource by inserting it manually or using the `setup` method.
You can get the type name of the missing resource by enabling `shred`'s `nightly` feature', /home/kuba/.cargo/registry/src/github.com-1ecc6299db9ec823/shred-0.7.0/src/res/mod.rs:215:48
note: Run with `RUST_BACKTRACE=1` for a backtrace.
If I skip the implementation of Default, it won't compile, but it doesn't
seem to use it?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#96 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AWFFuX-B0NmhzZtFgqIGTeheolrwFn_gks5uQtyIgaJpZM4V7oEs>
.
My bad, there's a whole chapter on this in the book 🤦♂️