amethyst / shred

Shared resource dispatcher
Apache License 2.0
237 stars 66 forks source link

Why won't Write<R> initialize R if it's missing? #96

Closed farnoy closed 6 years ago

farnoy commented 6 years ago

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?

torkleyy commented 6 years ago

It should not panic, and I never heard of the initialization not working. Do you have a small example?

farnoy commented 6 years ago

Not on hand, I'll try to reproduce a small one today.

farnoy commented 6 years ago

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?

torkleyy commented 6 years ago

Very short answer: Dispatcher::setup

On Tue, Aug 14, 2018, 16:19 Jakub Okoński notifications@github.com wrote:

OK, I think this is it, on specs 0.12.1:

extern crate specs; use specs::prelude::*; pub struct TestData { test: Option, } 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 '' 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 https://github.com/slide-rs/shred/issues/96#issuecomment-412888501, or mute the thread https://github.com/notifications/unsubscribe-auth/AWFFuX-B0NmhzZtFgqIGTeheolrwFn_gks5uQtyIgaJpZM4V7oEs .

farnoy commented 6 years ago

My bad, there's a whole chapter on this in the book :man_facepalming: