pub struct Task<Fut>(/* private fields */);Expand description
An actor that runs a future and is associated bidirectionally with its lifecycle: the actor dying stops the task, and the task dying kills the actor.
To support background tasks, Task holds its own ActorRef – it doesn’t die if you don’t
hold a reference to it.
Kameo doesn’t natively provide functionality for this – ActorRef::attach_stream is the
closest thing, but it doesn’t fit the mental model of a background task that doesn’t necessarily
produce messages. So instead this is a thin wrapper around tokio::spawn, and the inner
future kills the actor if the future completes.
Task implements Message<Infallible> so it can be addressed as a type-erased
Recipient even though it doesn’t accept any messages.
If you need to be generic over the task future or would need to name the type of an anonymous
future, it’s recommended to box the future and use ErasedTask as the task type.
§Examples
Spawning a task that shares lifecycle with a parent:
struct Parent;
impl Actor for Parent {
type Error = ();
type Args = ();
async fn on_start(_: (), slf: ActorRef<Self>) -> Result<Self, ()> {
Task::spawn_link(&slf, async move {
for i in 0.. {
println!("{i}");
tokio::time::sleep(Duration::from_millis(25)).await;
}
});
// ... other self-init ...
Ok(Self)
}
}
let parent = Parent::spawn(());
tokio::time::sleep(Duration::from_millis(99)).await;
parent.kill();
// prints 0, 1, 2, 3, then the parent and task are killedTrait Implementations§
Source§impl<Fut> Actor for Task<Fut>
impl<Fut> Actor for Task<Fut>
Source§type Error = Infallible
type Error = Infallible
Source§async fn on_start(
fut: Self::Args,
slf: ActorRef<Self>,
) -> Result<Self, Self::Error>
async fn on_start( fut: Self::Args, slf: ActorRef<Self>, ) -> Result<Self, Self::Error>
Source§async fn on_stop(
&mut self,
slf: WeakActorRef<Self>,
reason: ActorStopReason,
) -> Result<(), Self::Error>
async fn on_stop( &mut self, slf: WeakActorRef<Self>, reason: ActorStopReason, ) -> Result<(), Self::Error>
Source§fn name() -> &'static str
fn name() -> &'static str
Source§fn supervision_strategy() -> SupervisionStrategy
fn supervision_strategy() -> SupervisionStrategy
Source§fn on_message(
&mut self,
msg: Box<dyn DynMessage<Self>>,
actor_ref: ActorRef<Self>,
tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>,
stop: &mut bool,
) -> impl Future<Output = Result<(), Box<dyn ReplyError>>> + Send
fn on_message( &mut self, msg: Box<dyn DynMessage<Self>>, actor_ref: ActorRef<Self>, tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>, stop: &mut bool, ) -> impl Future<Output = Result<(), Box<dyn ReplyError>>> + Send
Source§fn on_panic(
&mut self,
actor_ref: WeakActorRef<Self>,
err: PanicError,
) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
fn on_panic( &mut self, actor_ref: WeakActorRef<Self>, err: PanicError, ) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
Source§fn on_link_died(
&mut self,
actor_ref: WeakActorRef<Self>,
id: ActorId,
reason: ActorStopReason,
) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
fn on_link_died( &mut self, actor_ref: WeakActorRef<Self>, id: ActorId, reason: ActorStopReason, ) -> impl Future<Output = Result<ControlFlow<ActorStopReason>, Self::Error>> + Send
Source§impl<Fut> Message<Infallible> for Task<Fut>
impl<Fut> Message<Infallible> for Task<Fut>
Source§type Reply = Infallible
type Reply = Infallible
Source§async fn handle(
&mut self,
_: Infallible,
_: &mut Context<Self, Self::Reply>,
) -> Infallible
async fn handle( &mut self, _: Infallible, _: &mut Context<Self, Self::Reply>, ) -> Infallible
Auto Trait Implementations§
impl<Fut> Freeze for Task<Fut>
impl<Fut> RefUnwindSafe for Task<Fut>where
Fut: RefUnwindSafe,
impl<Fut> Send for Task<Fut>where
Fut: Send,
impl<Fut> Sync for Task<Fut>where
Fut: Sync,
impl<Fut> Unpin for Task<Fut>where
Fut: Unpin,
impl<Fut> UnsafeUnpin for Task<Fut>
impl<Fut> UnwindSafe for Task<Fut>where
Fut: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<A, T> DynMessage<A> for T
impl<A, T> DynMessage<A> for T
Source§fn handle_dyn<'a>(
self: Box<T>,
state: &'a mut A,
actor_ref: ActorRef<A>,
tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>,
stop: &'a mut bool,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn ReplyError>>> + Send + 'a>>
fn handle_dyn<'a>( self: Box<T>, state: &'a mut A, actor_ref: ActorRef<A>, tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>, stop: &'a mut bool, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn ReplyError>>> + Send + 'a>>
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more