Skip to content

Commit b0f4489

Browse files
committed
Merge #49
49: Add optional fetching of resources r=torkleyy a=torkleyy cc @Rhuagh
2 parents db43c75 + a4ae92c commit b0f4489

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "shred"
3-
version = "0.4.3"
4-
authors = ["torkleyy"]
3+
version = "0.4.4"
4+
authors = ["torkleyy <torkleyy@gmail.com>"]
55
description = """
66
Dispatches systems in parallel which need read access to some resources,
77
and write access to others.
@@ -27,4 +27,4 @@ shred-derive = { path = "shred-derive", version = "0.3" }
2727
smallvec = "0.4"
2828

2929
[dev-dependencies]
30-
cgmath = "0.14"
30+
cgmath = "0.15"

examples/fetch_opt.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate shred;
2+
3+
use shred::{DispatcherBuilder, Fetch, FetchMut, Resources, System};
4+
5+
#[derive(Debug)]
6+
struct ResA;
7+
8+
#[derive(Debug)]
9+
struct ResB;
10+
11+
struct PrintSystem;
12+
13+
impl<'a> System<'a> for PrintSystem {
14+
// We can simply use `Option<Fetch>` or `Option<FetchMut>` if a resource
15+
// isn't strictly required.
16+
type SystemData = (Fetch<'a, ResA>, Option<FetchMut<'a, ResB>>);
17+
18+
fn run(&mut self, data: Self::SystemData) {
19+
let (a, mut b) = data;
20+
21+
println!("{:?}", &*a);
22+
23+
if let Some(ref mut x) = b {
24+
println!("{:?}", &**x);
25+
26+
**x = ResB;
27+
}
28+
}
29+
}
30+
31+
fn main() {
32+
let mut resources = Resources::new();
33+
let mut dispatcher = DispatcherBuilder::new()
34+
.add(PrintSystem, "print", &[]) // Adds a system "print" without dependencies
35+
.build();
36+
resources.add(ResA);
37+
38+
// `ResB` is not in resources, but `PrintSystem` still works.
39+
dispatcher.dispatch(&mut resources);
40+
41+
resources.add(ResB);
42+
43+
// Now `ResB` can be printed, too.
44+
dispatcher.dispatch(&mut resources);
45+
}

src/res.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,38 @@ impl<'a, T> SystemData<'a> for FetchMut<'a, T>
124124
}
125125
}
126126

127+
impl<'a, T> SystemData<'a> for Option<Fetch<'a, T>>
128+
where T: Resource
129+
{
130+
fn fetch(res: &'a Resources, id: usize) -> Self {
131+
res.try_fetch(id)
132+
}
133+
134+
fn reads(id: usize) -> Vec<ResourceId> {
135+
vec![ResourceId::new_with_id::<T>(id)]
136+
}
137+
138+
fn writes(_: usize) -> Vec<ResourceId> {
139+
vec![]
140+
}
141+
}
142+
143+
impl<'a, T> SystemData<'a> for Option<FetchMut<'a, T>>
144+
where T: Resource
145+
{
146+
fn fetch(res: &'a Resources, id: usize) -> Self {
147+
res.try_fetch_mut(id)
148+
}
149+
150+
fn reads(_: usize) -> Vec<ResourceId> {
151+
vec![]
152+
}
153+
154+
fn writes(id: usize) -> Vec<ResourceId> {
155+
vec![ResourceId::new_with_id::<T>(id)]
156+
}
157+
}
158+
127159
/// A resource defines a set of data
128160
/// which can only be accessed according
129161
/// to Rust's typical borrowing model (one writer xor multiple readers).

0 commit comments

Comments
 (0)