Context

Struct Context 

Source
pub struct Context<ActorId = DefaultActorId> { /* private fields */ }

Implementations§

Source§

impl<ActorId> Context<ActorId>

Source

pub fn new() -> Self

Examples found in repository?
examples/simple.rs (line 33)
32async fn main() {
33    let ctx = Context::new();
34    MyActor::run(ctx.clone(), 42);
35
36    let sender = ctx.sender::<Value>();
37    sender.send(Value(11)).await.ok();
38    sender.send(Value(22)).await.ok();
39}
More examples
Hide additional examples
examples/lifecycle.rs (line 33)
32async fn main() {
33    let ctx = Context::new();
34    MyActor::run(ctx.clone(), 42);
35
36    let sender = ctx.sender::<Value>();
37    sender.send(Value(11)).await.ok();
38    sender.send(Value(22)).await.ok();
39
40    ctx.shutdown().await;
41}
examples/dynamic.rs (line 40)
39async fn main() {
40    let ctx = Context::new();
41
42    MyActor::run(ctx.clone(), "actor 1", 1);
43    let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45    let sender_1 = ctx.actor_sender::<Value>("actor 1");
46    sender_1.send(Value(11)).await.ok();
47    sender_1.send(Value(12)).await.ok();
48
49    let sender_2 = ctx.actor_sender::<Value>("actor 2");
50    sender_2.send(Value(21)).await.ok();
51    sender_2.send(Value(22)).await.ok();
52
53    sender_2.send(Value(0)).await.ok();
54    ctx.join(&actor_2_task).await.ok();
55
56    MyActor::run(ctx.clone(), "actor 2", 2);
57
58    let sender_2 = ctx.actor_sender::<Value>("actor 2");
59    sender_2.send(Value(23)).await.ok();
60
61    ctx.shutdown().await;
62}
Source

pub fn from_system(system: System<ActorId>) -> Self

Source

pub fn spawn<T>(&self, future: T) -> TaskId
where T: Future<Output = ()> + Send + 'static,

Examples found in repository?
examples/dynamic.rs (lines 19-29)
14    fn run(ctx: Context, actor_id: &'static str, value: u32) -> TaskId {
15        let mut value_in = ctx.actor_receiver::<Value>(actor_id);
16        let mut actor = MyActor { value };
17
18        println!("run the {actor_id}");
19        ctx.clone().spawn(async move {
20            truba::event_loop!(ctx, {
21                Some(msg) = value_in.recv() => {
22                    actor.handle_value(msg);
23                    if actor.value == 0 {
24                        break;
25                    }
26                },
27            });
28            println!("stop the {actor_id}");
29        })
30    }
Source

pub fn extract_channel<M: Message>(&self) -> Option<M::Channel>

Source

pub fn get_sender<M: Message>(&self) -> Option<<M::Channel as Channel>::Sender>

Source

pub fn sender_of_custom_channel<M: Message>( &self, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Sender

Source

pub fn receiver_of_custom_channel<M: Message>( &self, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Receiver

Source

pub fn sender<M: Message>(&self) -> <M::Channel as Channel>::Sender

Examples found in repository?
examples/simple.rs (line 36)
32async fn main() {
33    let ctx = Context::new();
34    MyActor::run(ctx.clone(), 42);
35
36    let sender = ctx.sender::<Value>();
37    sender.send(Value(11)).await.ok();
38    sender.send(Value(22)).await.ok();
39}
More examples
Hide additional examples
examples/lifecycle.rs (line 36)
32async fn main() {
33    let ctx = Context::new();
34    MyActor::run(ctx.clone(), 42);
35
36    let sender = ctx.sender::<Value>();
37    sender.send(Value(11)).await.ok();
38    sender.send(Value(22)).await.ok();
39
40    ctx.shutdown().await;
41}
Source

pub fn receiver<M: Message>(&self) -> <M::Channel as Channel>::Receiver

Examples found in repository?
examples/lifecycle.rs (line 15)
14    fn run(ctx: Context, value: u32) {
15        let mut value_in = ctx.receiver::<Value>();
16        let mut actor = MyActor { value };
17
18        truba::spawn_event_loop!(ctx, {
19            Some(msg) = value_in.recv() => {
20                actor.handle_value(msg);
21            },
22        });
23    }
More examples
Hide additional examples
examples/simple.rs (line 15)
14    fn run(ctx: Context, value: u32) {
15        let mut value_in = ctx.receiver::<Value>();
16        let mut actor = MyActor { value };
17
18        truba::spawn_min_event_loop!(ctx, {
19            Some(msg) = value_in.recv() => {
20                actor.handle_value(msg);
21            },
22        });
23    }
Source

pub fn is_channel_closed<M: Message>(&self) -> Option<bool>

Source

pub fn system(&self) -> MutexGuard<'_, System<ActorId>>

Source

pub async fn shutdown(&self)

Examples found in repository?
examples/lifecycle.rs (line 40)
32async fn main() {
33    let ctx = Context::new();
34    MyActor::run(ctx.clone(), 42);
35
36    let sender = ctx.sender::<Value>();
37    sender.send(Value(11)).await.ok();
38    sender.send(Value(22)).await.ok();
39
40    ctx.shutdown().await;
41}
More examples
Hide additional examples
examples/dynamic.rs (line 61)
39async fn main() {
40    let ctx = Context::new();
41
42    MyActor::run(ctx.clone(), "actor 1", 1);
43    let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45    let sender_1 = ctx.actor_sender::<Value>("actor 1");
46    sender_1.send(Value(11)).await.ok();
47    sender_1.send(Value(12)).await.ok();
48
49    let sender_2 = ctx.actor_sender::<Value>("actor 2");
50    sender_2.send(Value(21)).await.ok();
51    sender_2.send(Value(22)).await.ok();
52
53    sender_2.send(Value(0)).await.ok();
54    ctx.join(&actor_2_task).await.ok();
55
56    MyActor::run(ctx.clone(), "actor 2", 2);
57
58    let sender_2 = ctx.actor_sender::<Value>("actor 2");
59    sender_2.send(Value(23)).await.ok();
60
61    ctx.shutdown().await;
62}
Source

pub async fn join_all(&self)

Source

pub async fn join(&self, id: &TaskId) -> Result<(), JoinError>

Examples found in repository?
examples/dynamic.rs (line 54)
39async fn main() {
40    let ctx = Context::new();
41
42    MyActor::run(ctx.clone(), "actor 1", 1);
43    let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45    let sender_1 = ctx.actor_sender::<Value>("actor 1");
46    sender_1.send(Value(11)).await.ok();
47    sender_1.send(Value(12)).await.ok();
48
49    let sender_2 = ctx.actor_sender::<Value>("actor 2");
50    sender_2.send(Value(21)).await.ok();
51    sender_2.send(Value(22)).await.ok();
52
53    sender_2.send(Value(0)).await.ok();
54    ctx.join(&actor_2_task).await.ok();
55
56    MyActor::run(ctx.clone(), "actor 2", 2);
57
58    let sender_2 = ctx.actor_sender::<Value>("actor 2");
59    sender_2.send(Value(23)).await.ok();
60
61    ctx.shutdown().await;
62}
Source

pub fn handles(&self) -> &TaskHandles

Source§

impl<ActorId: Eq + Hash> Context<ActorId>

Source

pub fn extract_actor_channel<M: Message>( &self, actor_id: &ActorId, ) -> Option<M::Channel>

Source

pub fn get_actor_sender<M: Message>( &self, actor_id: &ActorId, ) -> Option<<M::Channel as Channel>::Sender>

Source

pub fn actor_sender_of_custom_channel<M: Message>( &self, actor_id: ActorId, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Sender

Source

pub fn actor_receiver_of_custom_channel<M: Message>( &self, actor_id: ActorId, constructor: impl FnOnce() -> M::Channel, ) -> <M::Channel as Channel>::Receiver

Source§

impl<ActorId: Eq + Hash + Display> Context<ActorId>

Source

pub fn actor_sender<M: Message>( &self, actor_id: impl Into<ActorId>, ) -> <M::Channel as Channel>::Sender

Examples found in repository?
examples/dynamic.rs (line 45)
39async fn main() {
40    let ctx = Context::new();
41
42    MyActor::run(ctx.clone(), "actor 1", 1);
43    let actor_2_task = MyActor::run(ctx.clone(), "actor 2", 2);
44
45    let sender_1 = ctx.actor_sender::<Value>("actor 1");
46    sender_1.send(Value(11)).await.ok();
47    sender_1.send(Value(12)).await.ok();
48
49    let sender_2 = ctx.actor_sender::<Value>("actor 2");
50    sender_2.send(Value(21)).await.ok();
51    sender_2.send(Value(22)).await.ok();
52
53    sender_2.send(Value(0)).await.ok();
54    ctx.join(&actor_2_task).await.ok();
55
56    MyActor::run(ctx.clone(), "actor 2", 2);
57
58    let sender_2 = ctx.actor_sender::<Value>("actor 2");
59    sender_2.send(Value(23)).await.ok();
60
61    ctx.shutdown().await;
62}
Source

pub fn actor_receiver<M: Message>( &self, actor_id: impl Into<ActorId>, ) -> <M::Channel as Channel>::Receiver

Examples found in repository?
examples/dynamic.rs (line 15)
14    fn run(ctx: Context, actor_id: &'static str, value: u32) -> TaskId {
15        let mut value_in = ctx.actor_receiver::<Value>(actor_id);
16        let mut actor = MyActor { value };
17
18        println!("run the {actor_id}");
19        ctx.clone().spawn(async move {
20            truba::event_loop!(ctx, {
21                Some(msg) = value_in.recv() => {
22                    actor.handle_value(msg);
23                    if actor.value == 0 {
24                        break;
25                    }
26                },
27            });
28            println!("stop the {actor_id}");
29        })
30    }
Source

pub fn is_actor_channel_closed<M: Message>( &self, actor_id: impl Borrow<ActorId>, ) -> Option<bool>

Trait Implementations§

Source§

impl<ActorId: Clone> Clone for Context<ActorId>

Source§

fn clone(&self) -> Context<ActorId>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<ActorId> Default for Context<ActorId>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<ActorId> From<System<ActorId>> for Context<ActorId>

Source§

fn from(system: System<ActorId>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<ActorId> Freeze for Context<ActorId>

§

impl<ActorId = String> !RefUnwindSafe for Context<ActorId>

§

impl<ActorId> Send for Context<ActorId>
where ActorId: Send,

§

impl<ActorId> Sync for Context<ActorId>
where ActorId: Send,

§

impl<ActorId> Unpin for Context<ActorId>

§

impl<ActorId = String> !UnwindSafe for Context<ActorId>

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> CloneAny for T
where T: Any + Clone + Send + Sync,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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.
Source§

impl<T> UnsafeAny for T
where T: Any,