Skip to main content

Context

Struct Context 

Source
pub struct Context<'a> { /* private fields */ }
Expand description

State and services available during one step call.

Implementations§

Source§

impl<'a> Context<'a>

Source

pub fn location(&self) -> (SequenceRef, usize)

Returns the sequence and step index.

Source

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}
Source

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}
Source

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}
Source

pub fn service<T: Any>(&self) -> Option<&T>

Returns the host service of type T.

Source

pub fn service_mut<T: Any>(&mut self) -> Option<&mut T>

Returns mutable access to the host service of type T.

Source

pub fn instigator(&self) -> Option<&dyn Any>

Returns the opaque chain instigator.

Source

pub fn instigator_as<T: Any>(&self) -> Option<&T>

Returns the instigator as type T.

Source

pub fn flags(&self) -> &ChainFlags

Returns the chain flags.

Source

pub fn flags_mut(&mut self) -> &mut ChainFlags

Returns mutable access to the chain flags.

Source

pub fn flag(&self, name: &str) -> bool

Returns a chain flag.

Source

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}
Source

pub fn eval(&self, condition: &dyn Condition) -> bool

Evaluates a condition with the chain context.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.