Skip to content

Commit ae5280d

Browse files
committed
docs(examples): add examples to Store documentation
To keep the code readable, the examples are not inlined with the code, but kept in a separate location, using the (nightly) feature [`external_doc`]. To build and open the documentation, run: cargo +nightly doc --no-deps --open --features doc To test the examples in the documentation, run: cargo +nightly test --doc --features doc [`external_doc`]: https://doc.rust-lang.org/beta/unstable-book/language-features/external-doc.html
1 parent a38ce8c commit ae5280d

File tree

8 files changed

+484
-0
lines changed

8 files changed

+484
-0
lines changed

Diff for: Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ authors = ["Jean Mertz <[email protected]>"]
55
license = "MIT OR Apache-2.0"
66
description = "Entity–Component–System library currently in development"
77
edition = "2018"
8+
9+
[features]
10+
doc = []
11+
12+
[dev-dependencies]
13+
anymap = "0.12"

Diff for: doc/examples/store_accepts.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Examples
2+
3+
```rust
4+
#[derive(Debug)]
5+
struct ComponentStore(anymap::Map);
6+
7+
impl Store for ComponentStore {
8+
fn accepts<C: Component>(&self) -> bool {
9+
self.0.contains::<Vec<C>>()
10+
}
11+
12+
// other implementations hidden...
13+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
14+
# fn register<C: Component>(&mut self) {}
15+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> { Ok(()) }
16+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
17+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
18+
}
19+
20+
# use things::{Component, Store};
21+
#
22+
# impl Default for ComponentStore {
23+
# fn default() -> Self { Self::new() }
24+
# }
25+
```
26+
27+
Using `accepts` without first [`register`]ing a component type will return
28+
false (as opposed to the [`Err`] we got when trying to [`push`] an instance of
29+
this type into the store):
30+
31+
```rust
32+
# fn main() {
33+
# let mut store = ComponentStore::new();
34+
assert_eq!(store.accepts::<String>(), false)
35+
# }
36+
37+
# use things::{Component, Store};
38+
#
39+
# #[derive(Debug)]
40+
# struct ComponentStore(anymap::Map);
41+
#
42+
# impl Store for ComponentStore {
43+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
44+
# fn register<C: Component>(&mut self) {}
45+
# fn accepts<C: Component>(&self) -> bool { self.0.contains::<Vec<C>>() }
46+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> { Ok(()) }
47+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
48+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
49+
# }
50+
#
51+
# impl Default for ComponentStore {
52+
# fn default() -> Self { Self::new() }
53+
# }
54+
```
55+
56+
After [`register`]ing, `true` is returned:
57+
58+
```rust
59+
# fn main() {
60+
# let mut store = ComponentStore::new();
61+
store.register::<String>();
62+
assert_eq!(store.accepts::<String>(), true)
63+
# }
64+
65+
# use things::{Component, Store};
66+
#
67+
# #[derive(Debug)]
68+
# struct ComponentStore(anymap::Map);
69+
#
70+
# impl Store for ComponentStore {
71+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
72+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
73+
# fn accepts<C: Component>(&self) -> bool { self.0.contains::<Vec<C>>() }
74+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> { Ok(()) }
75+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
76+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
77+
# }
78+
#
79+
# impl Default for ComponentStore {
80+
# fn default() -> Self { Self::new() }
81+
# }
82+
```
83+
84+
[`register`]: Store::register
85+
[`push`]: Store::push

Diff for: doc/examples/store_get.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Examples
2+
3+
```rust
4+
#[derive(Debug)]
5+
struct ComponentStore(anymap::Map);
6+
7+
impl Store for ComponentStore {
8+
fn get<C: Component>(&self) -> Option<&[C]> {
9+
self.0.get::<Vec<C>>().map(|vec| vec.as_slice())
10+
}
11+
12+
// other implementations hidden...
13+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
14+
# fn register<C: Component>(&mut self) {}
15+
# fn accepts<C: Component>(&self) -> bool { true }
16+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> { Ok(()) }
17+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
18+
}
19+
20+
# use things::{Component, Store};
21+
#
22+
# impl Default for ComponentStore {
23+
# fn default() -> Self { Self::new() }
24+
# }
25+
```
26+
27+
After implementing `get`, we [`register`] two component types [`u32`] and
28+
[`u64`], and [`push`] three components into the store:
29+
30+
```rust
31+
# fn main() {
32+
let mut store = ComponentStore::new();
33+
34+
store.register::<u32>();
35+
store.register::<u64>();
36+
37+
store.push(1_u32);
38+
store.push(2_u64); // note: u64
39+
store.push(3_u32);
40+
# }
41+
42+
# use things::{Component, Store};
43+
#
44+
# #[derive(Debug)]
45+
# struct ComponentStore(anymap::Map);
46+
#
47+
# impl Store for ComponentStore {
48+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
49+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
50+
# fn accepts<C: Component>(&self) -> bool { true }
51+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> {
52+
# self.0.get_mut::<Vec<C>>().unwrap().push(component); Ok(())
53+
# }
54+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
55+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
56+
# }
57+
#
58+
# impl Default for ComponentStore {
59+
# fn default() -> Self { Self::new() }
60+
# }
61+
```
62+
63+
We can now fetch those components using `get`:
64+
65+
```rust
66+
# fn main() {
67+
# let mut store = ComponentStore::new();
68+
#
69+
# store.register::<u32>();
70+
# store.register::<u64>();
71+
#
72+
# store.push(1_u32);
73+
# store.push(2_u64);
74+
# store.push(3_u32);
75+
76+
let components = store.get::<u32>().unwrap();
77+
assert_eq!(components[0], 1);
78+
assert_eq!(components[1], 3);
79+
80+
let components = store.get::<u64>().unwrap();
81+
assert_eq!(components[0], 2);
82+
# }
83+
84+
# use things::{Component, Store};
85+
#
86+
# #[derive(Debug)]
87+
# struct ComponentStore(anymap::Map);
88+
#
89+
# impl Store for ComponentStore {
90+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
91+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
92+
# fn accepts<C: Component>(&self) -> bool { true }
93+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> {
94+
# self.0.get_mut::<Vec<C>>().unwrap().push(component); Ok(())
95+
# }
96+
# fn get<C: Component>(&self) -> Option<&[C]> {
97+
# self.0.get::<Vec<C>>().map(|vec| vec.as_slice())
98+
# }
99+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
100+
# }
101+
#
102+
# impl Default for ComponentStore {
103+
# fn default() -> Self { Self::new() }
104+
# }
105+
```
106+
107+
[`register`]: Store::register
108+
[`push`]: Store::push

Diff for: doc/examples/store_get_mut.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Examples
2+
3+
```rust
4+
#[derive(Debug)]
5+
struct ComponentStore(anymap::Map);
6+
7+
impl Store for ComponentStore {
8+
fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> {
9+
self.0.get_mut::<Vec<C>>().map(|vec| vec.as_mut_slice())
10+
}
11+
12+
// other implementations hidden...
13+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
14+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
15+
# fn accepts<C: Component>(&self) -> bool { true }
16+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
17+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> { Ok(()) }
18+
}
19+
20+
# use things::{Component, Store};
21+
#
22+
# impl Default for ComponentStore {
23+
# fn default() -> Self { Self::new() }
24+
# }
25+
```
26+
27+
After implementing `get_mut`, we [`register`] the component type [`u32`] and
28+
[`push`] a component into the store:
29+
30+
```rust
31+
# fn main() {
32+
let mut store = ComponentStore::new();
33+
34+
store.register::<u32>();
35+
store.push(1_u32);
36+
# }
37+
38+
# use things::{Component, Store};
39+
#
40+
# #[derive(Debug)]
41+
# struct ComponentStore(anymap::Map);
42+
#
43+
# impl Default for ComponentStore {
44+
# fn default() -> Self { Self::new() }
45+
# }
46+
#
47+
# impl Store for ComponentStore {
48+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
49+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
50+
# fn accepts<C: Component>(&self) -> bool { true }
51+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> {
52+
# self.0.get_mut::<Vec<C>>().unwrap().push(component); Ok(())
53+
# }
54+
# fn get<C: Component>(&self) -> Option<&[C]> { None }
55+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> { None }
56+
# }
57+
```
58+
59+
After we've done so, we can manipulate the component using `get_mut`:
60+
61+
```rust
62+
# fn main() {
63+
# let mut store = ComponentStore::new();
64+
# store.register::<u32>();
65+
# store.push(1_u32);
66+
67+
store.get_mut::<u32>().unwrap().iter_mut().for_each(|i| *i += 1);
68+
69+
assert_eq!(store.get::<u32>().unwrap()[0], 2);
70+
# }
71+
72+
# use things::{Component, Store};
73+
#
74+
# #[derive(Debug)]
75+
# struct ComponentStore(anymap::Map);
76+
#
77+
# impl Store for ComponentStore {
78+
# fn new() -> Self { ComponentStore(anymap::Map::new()) }
79+
# fn register<C: Component>(&mut self) { self.0.insert(Vec::<C>::new()); }
80+
# fn accepts<C: Component>(&self) -> bool { true }
81+
# fn push<C: Component>(&mut self, component: C) -> Result<(), String> {
82+
# self.0.get_mut::<Vec<C>>().unwrap().push(component); Ok(())
83+
# }
84+
# fn get<C: Component>(&self) -> Option<&[C]> {
85+
# self.0.get::<Vec<C>>().map(|vec| vec.as_slice())
86+
# }
87+
# fn get_mut<C: Component>(&mut self) -> Option<&mut [C]> {
88+
# self.0.get_mut::<Vec<C>>().map(|vec| vec.as_mut_slice())
89+
# }
90+
# }
91+
#
92+
# impl Default for ComponentStore {
93+
# fn default() -> Self { Self::new() }
94+
# }
95+
```
96+
97+
[`register`]: Store::register
98+
[`push`]: Store::push

0 commit comments

Comments
 (0)