amethyst / shred

Shared resource dispatcher

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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?

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?

My bad, there's a whole chapter on this in the book 🤦‍♂️