pub struct Handle<I, O> {
pub n: usize,
/* private fields */
}Expand description
Channel to communicated with Actor instances
Fields§
§n: usizeImplementations§
Source§impl<I, O> Handle<I, O>
impl<I, O> Handle<I, O>
Sourcepub fn new(n: usize) -> Self
pub fn new(n: usize) -> Self
create a new Handle with n of channel depth
use tokiactor::*;
let handle = Handle::<i32, i32>::new(1);Sourcepub fn spawn<FN, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
pub fn spawn<FN, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
spawn Actor with type impl Into<Actor<I, O, FN>> in system thread
use tokiactor::*;
let handle = Handle::new(1).spawn(|i: i32| i + 1);Sourcepub fn spawn_n<FN, IntoActor>(self, n: usize, actor: IntoActor) -> Handle<I, O>
pub fn spawn_n<FN, IntoActor>(self, n: usize, actor: IntoActor) -> Handle<I, O>
spawn n Actor with type impl Into<Actor<I, O, FN>> in system threads when Actor is Clone
use tokiactor::*;
let handle = Handle::new(1).spawn_n(10, |i: i32| i + 1);Sourcepub fn spawn_tokio<FN, FU, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
pub fn spawn_tokio<FN, FU, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
spawn async Actor with type impl Into<Actor<I, O, FU, FN>> in tokio thread
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn_tokio(move |i: i32| async move {i + 41})
.handle(1)
.await;
assert_eq!(r, 42)
}
);Sourcepub async fn handle(&self, i: I) -> O
pub async fn handle(&self, i: I) -> O
Handle input
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn_tokio(move |i: i32| async move {i + 41})
.handle(1)
.await;
}
);Sourcepub async fn try_handle(&self, i: I) -> Result<O, HandleError<(I, Sender<O>)>>
pub async fn try_handle(&self, i: I) -> Result<O, HandleError<(I, Sender<O>)>>
Try handle input, return error when actor is dead…
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
Handle::new(1)
.spawn_tokio(move |i: i32| async move {i + 41})
.try_handle(1)
.await;
}
);Sourcepub fn inspect<INS>(self, ins: INS) -> Handle<I, O>
pub fn inspect<INS>(self, ins: INS) -> Handle<I, O>
Inspect Handle excution, check Inspector trait for more info
use tokiactor::*;
use tokio;
use std::time::Instant;
#[derive(Clone)]
struct Timer(Instant);
impl Inspector<i32, i32> for Timer
{
fn inspect_i(&mut self, i: i32) -> i32
{
self.0 = Instant::now();
i
}
fn inspect_o(&mut self, o: i32) -> i32
{
let delta = self.0.elapsed();
// get execution time, do logging...
dbg!(delta);
o
}
}
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
Handle::new(1)
.spawn_tokio(move |i: i32| async move {i + 41})
.inspect(Timer(Instant::now()))
.try_handle(1)
.await;
}
);
Sourcepub fn convert<F, P>(self, f: F) -> Handle<I, P>
pub fn convert<F, P>(self, f: F) -> Handle<I, P>
convert Handle<I, O> to Handle<I, P> with Fn(O)->P
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
Handle::new(1)
.spawn(move |i: i32| i + 1)
.convert(|v| v as f32);
}
)Sourcepub fn then<P>(self, rhs: Handle<O, P>) -> Handle<I, P>where
P: Send + 'static,
pub fn then<P>(self, rhs: Handle<O, P>) -> Handle<I, P>where
P: Send + 'static,
Handle<I, O> + Handle<O, P> => Handle<I, P>
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| i + 1)
.then(Handle::new(1).spawn(move |i: i32| i * 10)).handle(1).await;
assert_eq!(r, 20);
}
)Sourcepub fn join<U, V>(self, rhs: Handle<U, V>) -> Handle<(I, U), (O, V)>
pub fn join<U, V>(self, rhs: Handle<U, V>) -> Handle<(I, U), (O, V)>
Handle<I, O> | Handle<U, V> => Handle<(I,U),(O,V)>
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| i + 1)
.join(Handle::new(1).spawn(move |i: i32| i * 10)).handle((2, 1)).await;
assert_eq!(r, (3, 10));
}
)Source§impl<I, O> Handle<I, Option<O>>
impl<I, O> Handle<I, Option<O>>
Sourcepub fn and<U, V>(self, rhs: OptionHandle<U, V>) -> OptionHandle<(I, U), (O, V)>
pub fn and<U, V>(self, rhs: OptionHandle<U, V>) -> OptionHandle<(I, U), (O, V)>
OptionHandle<I, O> | OptionHandle<U, V> => OptionHandle<(I,U), (O,V)>
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| Some(i + 1))
.and(Handle::new(1).spawn(move |i: i32| Some(i * 10))).handle((1, 1)).await;
assert_eq!(r, Some((2, 10)));
}
)Sourcepub fn and_then<P>(self, rhs: OptionHandle<O, P>) -> OptionHandle<I, P>where
P: Send + 'static,
pub fn and_then<P>(self, rhs: OptionHandle<O, P>) -> OptionHandle<I, P>where
P: Send + 'static,
OptionHandle<I, O> + OptionHandle<O, P> => OptionHandle<I, P>
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| Some(i + 1))
.and_then(Handle::new(1).spawn(move |i: i32| Some(i * 10))).handle(1).await;
assert_eq!(r, Some(20));
}
)Sourcepub fn map<P>(self, rhs: Handle<O, P>) -> OptionHandle<I, P>where
P: Send + 'static,
pub fn map<P>(self, rhs: Handle<O, P>) -> OptionHandle<I, P>where
P: Send + 'static,
OptionHandle<I, O> + Handle<O, P> => OptionHandle<I, P>
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| Some(i + 1))
.map(Handle::new(1).spawn(move |i: i32| i * 10)).handle(1).await;
assert_eq!(r, Some(20));
}
)Sourcepub fn ok_or<C: ToString>(self, context: C) -> ResultHandle<I, O, Error>
pub fn ok_or<C: ToString>(self, context: C) -> ResultHandle<I, O, Error>
convert OptionHandle to ResultHandle
use tokiactor::*;
use tokio;
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| Some(i + 1))
.ok_or("null error").handle(1).await;
assert_eq!(r.unwrap(), 2);
}
)Source§impl<I, O, E> Handle<I, Result<O, E>>
impl<I, O, E> Handle<I, Result<O, E>>
Sourcepub fn and<U, V, EN>(
self,
rhs: ResultHandle<U, V, EN>,
) -> ResultHandle<(I, U), (O, V), Error>
pub fn and<U, V, EN>( self, rhs: ResultHandle<U, V, EN>, ) -> ResultHandle<(I, U), (O, V), Error>
ResultHandle<I, O, E> + ResultHandle<U, V, EN> => ResultHandle<(I, U), (O, V), anyhow::Error>
use tokiactor::*;
use tokio;
use thiserror;
#[derive(Debug, thiserror::Error)]
pub enum Error{}
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| {
Result::<i32, Error>::Ok(i+1)
})
.and(
Handle::new(1).spawn(move |i: i32| {
Result::<i32, Error>::Ok(i*1)
})
).handle((1, 1)).await;
assert_eq!(r.unwrap(), (2, 1));
}
);Sourcepub fn and_then<P, EN>(
self,
rhs: ResultHandle<O, P, EN>,
) -> ResultHandle<I, P, Error>
pub fn and_then<P, EN>( self, rhs: ResultHandle<O, P, EN>, ) -> ResultHandle<I, P, Error>
ResultHandle<I, O, E> | ResultHandle<O, P, EN> => ResultHandle<I, P, anyhow::Error>
use tokiactor::*;
use tokio;
use thiserror;
#[derive(Debug, thiserror::Error)]
pub enum Error{}
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| {
Result::<i32, Error>::Ok(i+1)
})
.and_then(
Handle::new(1).spawn(move |i: i32| {
Result::<i32, Error>::Ok(i*10)
})
).handle(1).await;
assert_eq!(r.unwrap(), 20);
}
);Sourcepub fn map<P>(self, rhs: Handle<O, P>) -> ResultHandle<I, P, E>where
P: Send + 'static,
pub fn map<P>(self, rhs: Handle<O, P>) -> ResultHandle<I, P, E>where
P: Send + 'static,
ResultHandle<I, O, E> | Handle<O, P> => ResultHandle<I, P, E>
use tokiactor::*;
use tokio;
use thiserror;
#[derive(Debug, thiserror::Error)]
pub enum Error{}
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| {
Result::<i32, Error>::Ok(i+1)
})
.map(
Handle::new(1).spawn(move |i: i32| {
i*10
})
).handle(1).await;
assert_eq!(r.unwrap(), 20);
}
);Sourcepub fn ok(self) -> OptionHandle<I, O>
pub fn ok(self) -> OptionHandle<I, O>
ResultHandle<I, O, E> => OptionHandle<I, O>
use tokiactor::*;
use tokio;
use thiserror;
#[derive(Debug, thiserror::Error)]
pub enum Error{}
tokio::runtime::Runtime::new().unwrap().block_on(
async move {
let r = Handle::new(1)
.spawn(move |i: i32| {
Result::<i32, Error>::Ok(i+1)
}).ok().handle(1).await;
assert_eq!(r.unwrap(), 2);
}
);