pub trait Actor<'a> {
type StoreRequest: StoreRequest<'a>;
type InitContext;
type Error: Error;
// Required methods
fn new(input: Self::StoreRequest, init_context: Self::InitContext) -> Self;
fn run(self) -> impl Future<Output = Result<Infallible, Self::Error>>;
}Expand description
Actor interface.
The Actor trait allows writing actors that communicate within a runtime.
It allows to define an initial context, which will be available for the whole life of the actor;
a constructor method, with all the StoreRequest types it needs to communicate with other actors;
and also the Actor::run method.
§Usage
Add the Actor implementing types to the actor list in veecle_os::runtime::execute! when
constructing a runtime instance.
The Actor::run method implements the actor’s event loop.
To yield back to the executor, every event loop must contain at least one await.
Otherwise, the endless loop of the actor will block the executor and other actors.
§Macros
The actor attribute macro can be used to implement actors.
The function the macro is applied to is converted into the event loop.
See its documentation for more details.
§Example
#[veecle_os_runtime::actor]
async fn my_actor(
reader: Reader<'_, Foo>,
writer: Writer<'_, Bar>,
#[init_context] ctx: Ctx,
) -> Infallible {
loop {
// Do something here.
}
}This will create a new struct called MyActor which implements Actor, letting you register it into a runtime.
§Manual
For cases where the macro is not sufficient, the Actor trait can also be implemented manually:
struct MyActor<'a> {
reader: Reader<'a, Foo>,
writer: Writer<'a, Bar>,
context: Ctx,
}
impl<'a> Actor<'a> for MyActor<'a> {
type StoreRequest = (Reader<'a, Foo>, Writer<'a, Bar>);
type InitContext = Ctx;
type Error = Infallible;
fn new((reader, writer): Self::StoreRequest, context: Self::InitContext) -> Self {
Self {
reader,
writer,
context,
}
}
async fn run(mut self) -> Result<Infallible, Self::Error> {
loop {
// Do something here.
}
}
}Required Associated Types§
Sourcetype StoreRequest: StoreRequest<'a>
type StoreRequest: StoreRequest<'a>
Sourcetype InitContext
type InitContext
Context that needs to be passed to the actor at initialisation.
Required Methods§
Sourcefn new(input: Self::StoreRequest, init_context: Self::InitContext) -> Self
fn new(input: Self::StoreRequest, init_context: Self::InitContext) -> Self
Creates a new instance of the struct implementing Actor.
See the crate documentation for examples.
Sourcefn run(self) -> impl Future<Output = Result<Infallible, Self::Error>>
fn run(self) -> impl Future<Output = Result<Infallible, Self::Error>>
Runs the Actor event loop.
See the crate documentation for examples.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.