pub struct ActorRefStrong<M>where
M: MessageBounds,{ /* private fields */ }Expand description
A version of ActorRef that prevents the target from being deallocated
Holding this kind of reference increases performance slightly, compared to the normal ActorRef. However, one must be careful not to leave strong references lying around when not needed anymore, as they prevent the target component from being deallocated, potentially causing memory creep.
Should be created via hold.
It is recommended to upgrade a normal actor reference to this variant, if many messages will be sent in a loop to it. After the loop, the strong reference can be dropped again.
Implementations§
Source§impl<M> ActorRefStrong<M>where
M: MessageBounds,
impl<M> ActorRefStrong<M>where
M: MessageBounds,
Sourcepub fn tell<I>(&self, v: I)where
I: Into<M>,
pub fn tell<I>(&self, v: I)where
I: Into<M>,
Send message v to the actor instance referenced by this actor reference
Sourcepub fn ask_with<R, F>(&self, f: F) -> KFuture<R> ⓘ
pub fn ask_with<R, F>(&self, f: F) -> KFuture<R> ⓘ
Helper to create messages that expect a response via a future instead of a message
§Example
use kompact::prelude::*;
use std::time::Duration;
#[derive(ComponentDefinition)]
struct AskComponent {
ctx: ComponentContext<Self>
}
impl Actor for AskComponent {
type Message = Ask<u64, String>;
fn receive_local(&mut self, msg: Self::Message) -> HandlerResult {
msg.complete(|num| {
format!("{}", num)
})
.expect("completion");
Handled::OK
}
}
let system = KompactConfig::default().build().wait().expect("system");
let c = system.create(AskComponent::new);
let strong_ref = c.actor_ref().hold().expect("live ref");
let start_f = system.start_notify(&c);
start_f
.wait_timeout(Duration::from_millis(1000))
.expect("component start");
let response_f = strong_ref.ask_with(Ask::of(42u64));
let response = response_f
.wait_timeout(Duration::from_millis(1000))
.expect("response");
assert_eq!("42".to_string(), response);Source§impl<Request, Response> ActorRefStrong<Ask<Request, Response>>
impl<Request, Response> ActorRefStrong<Ask<Request, Response>>
Sourcepub fn ask(&self, request: Request) -> KFuture<Response> ⓘ
pub fn ask(&self, request: Request) -> KFuture<Response> ⓘ
Helper to create messages that expect a response via a future instead of a message
§Example
use kompact::prelude::*;
use std::time::Duration;
#[derive(ComponentDefinition)]
struct AskComponent {
ctx: ComponentContext<Self>
}
impl Actor for AskComponent {
type Message = Ask<u64, String>;
fn receive_local(&mut self, msg: Self::Message) -> HandlerResult {
msg.complete(|num| {
format!("{}", num)
})
.expect("completion");
Handled::OK
}
}
let system = KompactConfig::default().build().wait().expect("system");
let c = system.create(AskComponent::new);
let strong_ref = c.actor_ref().hold().expect("live ref");
let start_f = system.start_notify(&c);
start_f
.wait_timeout(Duration::from_millis(1000))
.expect("component start");
let response_f = strong_ref.ask(42u64);
let response = response_f
.wait_timeout(Duration::from_millis(1000))
.expect("response");
assert_eq!("42".to_string(), response);Trait Implementations§
Source§impl<M> ActorRefFactory for ActorRefStrong<M>where
M: MessageBounds,
impl<M> ActorRefFactory for ActorRefStrong<M>where
M: MessageBounds,
Source§impl<M> Clone for ActorRefStrong<M>where
M: MessageBounds,
impl<M> Clone for ActorRefStrong<M>where
M: MessageBounds,
Source§fn clone(&self) -> ActorRefStrong<M>
fn clone(&self) -> ActorRefStrong<M>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<M> Debug for ActorRefStrong<M>where
M: MessageBounds,
impl<M> Debug for ActorRefStrong<M>where
M: MessageBounds,
Source§impl<M> Display for ActorRefStrong<M>where
M: MessageBounds,
impl<M> Display for ActorRefStrong<M>where
M: MessageBounds,
Source§impl<M> DynActorRefFactory for ActorRefStrong<M>where
M: MessageBounds,
Available on crate feature distributed only.
impl<M> DynActorRefFactory for ActorRefStrong<M>where
M: MessageBounds,
distributed only.Source§fn dyn_ref(&self) -> DynActorRef
fn dyn_ref(&self) -> DynActorRef
Source§impl<M> PartialEq for ActorRefStrong<M>where
M: MessageBounds,
impl<M> PartialEq for ActorRefStrong<M>where
M: MessageBounds,
Source§fn eq(&self, other: &ActorRefStrong<M>) -> bool
fn eq(&self, other: &ActorRefStrong<M>) -> bool
self and other values to be equal, and is used by ==.