pub struct Context<'a> { /* private fields */ }Expand description
State and services available during one step call.
Implementations§
Source§impl<'a> Context<'a>
impl<'a> Context<'a>
Sourcepub fn location(&self) -> (SequenceRef, usize)
pub fn location(&self) -> (SequenceRef, usize)
Returns the sequence and step index.
Sourcepub fn note(&mut self, message: impl Into<String>)
pub fn note(&mut self, message: impl Into<String>)
Adds a location-tagged note to the runner event stream.
Examples found in repository?
examples/dialog.rs (line 27)
16fn main() {
17 let mut library = Library::new();
18
19 // Insert first so later steps can refer to the cycle.
20 let hub = library.insert(Sequence::new("hub"));
21
22 let accepted = library.insert(
23 Sequence::new("accepted")
24 .with_step(steps::run("Take the ring", |ctx| {
25 match ctx.services_mut().get_mut::<World>() {
26 Some(world) => world.items.retain(|item| item != "gold ring"),
27 None => ctx.note("no World service; the ring stayed put"),
28 }
29 }))
30 .with_step(say("Elder", "You have my thanks.")),
31 );
32
33 let refused = library.insert(
34 Sequence::new("refused")
35 .with_step(say("Elder", "Come back when you have."))
36 .with_step(
37 steps::run("Return to the hub", move |_ctx| Progress::Goto(Some(hub)))
38 .ends()
39 .delegating_to(hub),
40 ),
41 );
42
43 let answered = Completion::new();
44 let waiting_on = answered.clone();
45 let hub_body = library.get_mut(hub).unwrap();
46 hub_body.push(say("Elder", "Have you found my ring?"));
47 hub_body.push(steps::run("Wait for the player", move |_ctx| {
48 waiting_on.clone()
49 }));
50 hub_body.push(steps::run("Record the answer", |ctx| {
51 let said_yes = ctx.services().get::<World>().is_some_and(|w| w.said_yes);
52 ctx.set_flag("said_yes", said_yes);
53 }));
54 hub_body.push(steps::Branch {
55 condition: Some(Box::new(conditions::Flag::is_set("said_yes"))),
56 if_true: Some(accepted),
57 if_false: Some(refused),
58 });
59
60 let mut services = TypeMap::new();
61 services.insert(World {
62 items: vec!["gold ring".to_owned(), "lantern".to_owned()],
63 said_yes: false,
64 });
65
66 let mut runner = Runner::default();
67 runner.start(hub, None).unwrap();
68
69 loop {
70 let poll = runner.advance(&mut library, &mut services);
71 drain(&mut runner);
72
73 match poll {
74 Poll::Pending => {
75 println!("> Yes");
76 services.get_mut::<World>().unwrap().said_yes = true;
77 answered.signal();
78 }
79 Poll::Ready(outcome) => {
80 println!("--- {outcome:?} ---");
81 break;
82 }
83 }
84 }
85
86 println!("inventory: {:?}", services.get::<World>().unwrap().items);
87}Sourcepub fn services(&self) -> &TypeMap
pub fn services(&self) -> &TypeMap
Returns the host service registry.
Examples found in repository?
examples/dialog.rs (line 51)
16fn main() {
17 let mut library = Library::new();
18
19 // Insert first so later steps can refer to the cycle.
20 let hub = library.insert(Sequence::new("hub"));
21
22 let accepted = library.insert(
23 Sequence::new("accepted")
24 .with_step(steps::run("Take the ring", |ctx| {
25 match ctx.services_mut().get_mut::<World>() {
26 Some(world) => world.items.retain(|item| item != "gold ring"),
27 None => ctx.note("no World service; the ring stayed put"),
28 }
29 }))
30 .with_step(say("Elder", "You have my thanks.")),
31 );
32
33 let refused = library.insert(
34 Sequence::new("refused")
35 .with_step(say("Elder", "Come back when you have."))
36 .with_step(
37 steps::run("Return to the hub", move |_ctx| Progress::Goto(Some(hub)))
38 .ends()
39 .delegating_to(hub),
40 ),
41 );
42
43 let answered = Completion::new();
44 let waiting_on = answered.clone();
45 let hub_body = library.get_mut(hub).unwrap();
46 hub_body.push(say("Elder", "Have you found my ring?"));
47 hub_body.push(steps::run("Wait for the player", move |_ctx| {
48 waiting_on.clone()
49 }));
50 hub_body.push(steps::run("Record the answer", |ctx| {
51 let said_yes = ctx.services().get::<World>().is_some_and(|w| w.said_yes);
52 ctx.set_flag("said_yes", said_yes);
53 }));
54 hub_body.push(steps::Branch {
55 condition: Some(Box::new(conditions::Flag::is_set("said_yes"))),
56 if_true: Some(accepted),
57 if_false: Some(refused),
58 });
59
60 let mut services = TypeMap::new();
61 services.insert(World {
62 items: vec!["gold ring".to_owned(), "lantern".to_owned()],
63 said_yes: false,
64 });
65
66 let mut runner = Runner::default();
67 runner.start(hub, None).unwrap();
68
69 loop {
70 let poll = runner.advance(&mut library, &mut services);
71 drain(&mut runner);
72
73 match poll {
74 Poll::Pending => {
75 println!("> Yes");
76 services.get_mut::<World>().unwrap().said_yes = true;
77 answered.signal();
78 }
79 Poll::Ready(outcome) => {
80 println!("--- {outcome:?} ---");
81 break;
82 }
83 }
84 }
85
86 println!("inventory: {:?}", services.get::<World>().unwrap().items);
87}Sourcepub fn services_mut(&mut self) -> &mut TypeMap
pub fn services_mut(&mut self) -> &mut TypeMap
Returns mutable access to the service registry.
Examples found in repository?
examples/dialog.rs (line 25)
16fn main() {
17 let mut library = Library::new();
18
19 // Insert first so later steps can refer to the cycle.
20 let hub = library.insert(Sequence::new("hub"));
21
22 let accepted = library.insert(
23 Sequence::new("accepted")
24 .with_step(steps::run("Take the ring", |ctx| {
25 match ctx.services_mut().get_mut::<World>() {
26 Some(world) => world.items.retain(|item| item != "gold ring"),
27 None => ctx.note("no World service; the ring stayed put"),
28 }
29 }))
30 .with_step(say("Elder", "You have my thanks.")),
31 );
32
33 let refused = library.insert(
34 Sequence::new("refused")
35 .with_step(say("Elder", "Come back when you have."))
36 .with_step(
37 steps::run("Return to the hub", move |_ctx| Progress::Goto(Some(hub)))
38 .ends()
39 .delegating_to(hub),
40 ),
41 );
42
43 let answered = Completion::new();
44 let waiting_on = answered.clone();
45 let hub_body = library.get_mut(hub).unwrap();
46 hub_body.push(say("Elder", "Have you found my ring?"));
47 hub_body.push(steps::run("Wait for the player", move |_ctx| {
48 waiting_on.clone()
49 }));
50 hub_body.push(steps::run("Record the answer", |ctx| {
51 let said_yes = ctx.services().get::<World>().is_some_and(|w| w.said_yes);
52 ctx.set_flag("said_yes", said_yes);
53 }));
54 hub_body.push(steps::Branch {
55 condition: Some(Box::new(conditions::Flag::is_set("said_yes"))),
56 if_true: Some(accepted),
57 if_false: Some(refused),
58 });
59
60 let mut services = TypeMap::new();
61 services.insert(World {
62 items: vec!["gold ring".to_owned(), "lantern".to_owned()],
63 said_yes: false,
64 });
65
66 let mut runner = Runner::default();
67 runner.start(hub, None).unwrap();
68
69 loop {
70 let poll = runner.advance(&mut library, &mut services);
71 drain(&mut runner);
72
73 match poll {
74 Poll::Pending => {
75 println!("> Yes");
76 services.get_mut::<World>().unwrap().said_yes = true;
77 answered.signal();
78 }
79 Poll::Ready(outcome) => {
80 println!("--- {outcome:?} ---");
81 break;
82 }
83 }
84 }
85
86 println!("inventory: {:?}", services.get::<World>().unwrap().items);
87}Sourcepub fn service_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn service_mut<T: Any>(&mut self) -> Option<&mut T>
Returns mutable access to the host service of type T.
Sourcepub fn instigator(&self) -> Option<&dyn Any>
pub fn instigator(&self) -> Option<&dyn Any>
Returns the opaque chain instigator.
Sourcepub fn instigator_as<T: Any>(&self) -> Option<&T>
pub fn instigator_as<T: Any>(&self) -> Option<&T>
Returns the instigator as type T.
Sourcepub fn flags(&self) -> &ChainFlags
pub fn flags(&self) -> &ChainFlags
Returns the chain flags.
Sourcepub fn flags_mut(&mut self) -> &mut ChainFlags
pub fn flags_mut(&mut self) -> &mut ChainFlags
Returns mutable access to the chain flags.
Sourcepub fn set_flag(&mut self, name: impl Into<String>, value: bool)
pub fn set_flag(&mut self, name: impl Into<String>, value: bool)
Sets a chain flag.
Examples found in repository?
examples/dialog.rs (line 52)
16fn main() {
17 let mut library = Library::new();
18
19 // Insert first so later steps can refer to the cycle.
20 let hub = library.insert(Sequence::new("hub"));
21
22 let accepted = library.insert(
23 Sequence::new("accepted")
24 .with_step(steps::run("Take the ring", |ctx| {
25 match ctx.services_mut().get_mut::<World>() {
26 Some(world) => world.items.retain(|item| item != "gold ring"),
27 None => ctx.note("no World service; the ring stayed put"),
28 }
29 }))
30 .with_step(say("Elder", "You have my thanks.")),
31 );
32
33 let refused = library.insert(
34 Sequence::new("refused")
35 .with_step(say("Elder", "Come back when you have."))
36 .with_step(
37 steps::run("Return to the hub", move |_ctx| Progress::Goto(Some(hub)))
38 .ends()
39 .delegating_to(hub),
40 ),
41 );
42
43 let answered = Completion::new();
44 let waiting_on = answered.clone();
45 let hub_body = library.get_mut(hub).unwrap();
46 hub_body.push(say("Elder", "Have you found my ring?"));
47 hub_body.push(steps::run("Wait for the player", move |_ctx| {
48 waiting_on.clone()
49 }));
50 hub_body.push(steps::run("Record the answer", |ctx| {
51 let said_yes = ctx.services().get::<World>().is_some_and(|w| w.said_yes);
52 ctx.set_flag("said_yes", said_yes);
53 }));
54 hub_body.push(steps::Branch {
55 condition: Some(Box::new(conditions::Flag::is_set("said_yes"))),
56 if_true: Some(accepted),
57 if_false: Some(refused),
58 });
59
60 let mut services = TypeMap::new();
61 services.insert(World {
62 items: vec!["gold ring".to_owned(), "lantern".to_owned()],
63 said_yes: false,
64 });
65
66 let mut runner = Runner::default();
67 runner.start(hub, None).unwrap();
68
69 loop {
70 let poll = runner.advance(&mut library, &mut services);
71 drain(&mut runner);
72
73 match poll {
74 Poll::Pending => {
75 println!("> Yes");
76 services.get_mut::<World>().unwrap().said_yes = true;
77 answered.signal();
78 }
79 Poll::Ready(outcome) => {
80 println!("--- {outcome:?} ---");
81 break;
82 }
83 }
84 }
85
86 println!("inventory: {:?}", services.get::<World>().unwrap().items);
87}Sourcepub fn eval(&self, condition: &dyn Condition) -> bool
pub fn eval(&self, condition: &dyn Condition) -> bool
Evaluates a condition with the chain context.
Sourcepub fn enact<'e>(&mut self, effects: impl IntoIterator<Item = &'e dyn Effect>)
pub fn enact<'e>(&mut self, effects: impl IntoIterator<Item = &'e dyn Effect>)
Applies effects with the chain context.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Context<'a>
impl<'a> !Send for Context<'a>
impl<'a> !Sync for Context<'a>
impl<'a> !UnwindSafe for Context<'a>
impl<'a> Freeze for Context<'a>
impl<'a> Unpin for Context<'a>
impl<'a> UnsafeUnpin for Context<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more