-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49: Add optional fetching of resources r=torkleyy a=torkleyy cc @Rhuagh
- Loading branch information
Showing
3 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "shred" | ||
version = "0.4.3" | ||
authors = ["torkleyy"] | ||
version = "0.4.4" | ||
authors = ["torkleyy <[email protected]>"] | ||
description = """ | ||
Dispatches systems in parallel which need read access to some resources, | ||
and write access to others. | ||
|
@@ -27,4 +27,4 @@ shred-derive = { path = "shred-derive", version = "0.3" } | |
smallvec = "0.4" | ||
|
||
[dev-dependencies] | ||
cgmath = "0.14" | ||
cgmath = "0.15" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
extern crate shred; | ||
|
||
use shred::{DispatcherBuilder, Fetch, FetchMut, Resources, System}; | ||
|
||
#[derive(Debug)] | ||
struct ResA; | ||
|
||
#[derive(Debug)] | ||
struct ResB; | ||
|
||
struct PrintSystem; | ||
|
||
impl<'a> System<'a> for PrintSystem { | ||
// We can simply use `Option<Fetch>` or `Option<FetchMut>` if a resource | ||
// isn't strictly required. | ||
type SystemData = (Fetch<'a, ResA>, Option<FetchMut<'a, ResB>>); | ||
|
||
fn run(&mut self, data: Self::SystemData) { | ||
let (a, mut b) = data; | ||
|
||
println!("{:?}", &*a); | ||
|
||
if let Some(ref mut x) = b { | ||
println!("{:?}", &**x); | ||
|
||
**x = ResB; | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut resources = Resources::new(); | ||
let mut dispatcher = DispatcherBuilder::new() | ||
.add(PrintSystem, "print", &[]) // Adds a system "print" without dependencies | ||
.build(); | ||
resources.add(ResA); | ||
|
||
// `ResB` is not in resources, but `PrintSystem` still works. | ||
dispatcher.dispatch(&mut resources); | ||
|
||
resources.add(ResB); | ||
|
||
// Now `ResB` can be printed, too. | ||
dispatcher.dispatch(&mut resources); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters