pub struct AcTokio(/* private fields */);
Available on crate feature
tokio
only.Expand description
Owned handle to an AcTokioRuntime
.
Dropping this handle will abort all actors spawned by this runtime.
Implementations§
source§impl AcTokio
impl AcTokio
sourcepub fn new(name: impl Into<String>, num_threads: usize) -> Result<Self>
pub fn new(name: impl Into<String>, num_threads: usize) -> Result<Self>
Create a new AcTokio
runtime with the given name and number of threads.
let rt = AcTokio::new("test", 1).unwrap();
let actor = rt.spawn_actor("test", |mut ctx| async move {
let ActoInput::<String, ()>::Message(name) = ctx.recv().await else { return };
println!("Hello, {}!", name);
});
// Send a message to the actor, after which it will terminate.
actor.me.send("world".to_owned());
// Wait for the actor to terminate.
rt.with_rt(|rt| rt.block_on(actor.handle.join())).unwrap();
In the above example the last step is crucial, without it nothing may be printed. The following demonstrates that this object owns the tokio runtime:
use acto::{AcTokio, ActoRuntime, ActoCell};
let flag = Arc::new(AtomicBool::new(false));
// This serves as a drop detector so we know the actor was killed.
struct X(Arc<AtomicBool>);
impl Drop for X {
fn drop(&mut self) {
self.0.store(true, Ordering::Relaxed);
}
}
let x = X(flag.clone());
let tokio = AcTokio::new("test", 1).unwrap();
tokio.spawn_actor("test", move |mut ctx: ActoCell<(), _>| async move {
// make sure to move the drop detector inside this scope
let _y = x;
// wait forever
loop { ctx.recv().await; }
});
// this will synchronously await termination of all actors and of the threads that run them
drop(tokio);
// verify that indeed the `_y` was dropped inside the actor
assert!(flag.load(Ordering::Relaxed));
sourcepub fn from_handle(name: impl Into<String>, handle: Handle) -> Self
pub fn from_handle(name: impl Into<String>, handle: Handle) -> Self
Create a new AcTokio
runtime from an existing tokio::runtime::Handle
.
This is useful if you want to use an existing tokio runtime, for example if you want to use [acto
] in a library.
Dropping this value will not abort the actors spawned by this runtime, but it will prevent new actors from
being spawned, so make sure to keep it around long enough!
Methods from Deref<Target = AcTokioRuntime>§
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for AcTokio
impl Send for AcTokio
impl Sync for AcTokio
impl Unpin for AcTokio
impl !UnwindSafe for AcTokio
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
Mutably borrows from an owned value. Read more