forked from amethyst/specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
81 lines (60 loc) · 1.63 KB
/
common.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
extern crate futures;
extern crate specs;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use futures::{Future, Poll};
use futures::future::Lazy;
use specs::*;
use specs::common::{BoxedFuture, Errors, Merge};
use specs::error::BoxedErr;
struct MyFloat(f32);
struct MyFuture(BoxedFuture<MyFloat>);
impl Future for MyFuture {
type Item = MyFloat;
type Error = BoxedErr;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl Component for MyFloat {
type Storage = VecStorage<Self>;
}
impl Component for MyFuture {
type Storage = DenseVecStorage<Self>;
}
fn main() {
let mut world = World::new();
world.register::<MyFloat>();
world.register::<MyFuture>();
world.add_resource(Errors::new());
world.create_entity().with(future_sqrt(25.0)).build();
let mut dispatcher = DispatcherBuilder::new()
.add(Merge::<MyFuture>::new(), "merge_my_float", &[])
.build();
dispatcher.dispatch(&world.res);
world.write_resource::<Errors>().print_and_exit();
}
#[derive(Debug)]
enum MyErr {
_What,
_Ever,
}
impl Display for MyErr {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", self.description())
}
}
impl Error for MyErr {
fn description(&self) -> &str {
match *self {
MyErr::_What => "What",
MyErr::_Ever => "Ever",
}
}
}
fn future_sqrt(num: f32) -> MyFuture {
use futures::lazy;
let lazy: Lazy<_, Result<_, MyErr>> = lazy(move || Ok(num.sqrt()));
let future = lazy.map(MyFloat).map_err(BoxedErr::new);
MyFuture(Box::new(future))
}