Skip to main content

Handle

Struct Handle 

Source
pub struct Handle<I, O> {
    pub n: usize,
    /* private fields */
}
Expand description

Channel to communicated with Actor instances

Fields§

§n: usize

Implementations§

Source§

impl<I, O> Handle<I, O>
where I: Send + 'static, O: Send + 'static,

Source

pub fn new(n: usize) -> Self

create a new Handle with n of channel depth

use tokiactor::*;

let handle = Handle::<i32, i32>::new(1);
Source

pub fn spawn<FN, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
where FN: FnMut(I) -> O + Send + 'static, IntoActor: Into<Actor<I, O, FN>>,

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);
Source

pub fn spawn_n<FN, IntoActor>(self, n: usize, actor: IntoActor) -> Handle<I, O>
where FN: FnMut(I) -> O + Send + 'static, IntoActor: Into<Actor<I, O, FN>>, Actor<I, O, FN>: Clone,

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);
Source

pub fn spawn_tokio<FN, FU, IntoActor>(self, actor: IntoActor) -> Handle<I, O>
where FN: FnMut(I) -> FU + Send + 'static, FU: Future<Output = O> + Send + 'static, IntoActor: Into<Actor<I, FU, FN>>,

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)
    }
);
Source

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;
    }
);
Source

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;
    }
);
Source

pub fn inspect<INS>(self, ins: INS) -> Handle<I, O>
where INS: Inspector<I, O> + Send + 'static + Clone,

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;
    }
);
Source

pub fn convert<F, P>(self, f: F) -> Handle<I, P>
where P: Send + 'static, F: Fn(O) -> P + Send + 'static + Clone,

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);
    }
)
Source

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);
    }
)
Source

pub fn join<U, V>(self, rhs: Handle<U, V>) -> Handle<(I, U), (O, V)>
where U: Send + 'static, V: Send + 'static,

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>>
where I: Send + 'static, O: Send + 'static,

Source

pub fn and<U, V>(self, rhs: OptionHandle<U, V>) -> OptionHandle<(I, U), (O, V)>
where U: Send + 'static, V: Send + 'static,

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)));
    }
)
Source

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));
    }
)
Source

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));
    }
)
Source

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>>
where I: Send + 'static, O: Send + 'static, E: Send + 'static,

Source

pub fn and<U, V, EN>( self, rhs: ResultHandle<U, V, EN>, ) -> ResultHandle<(I, U), (O, V), Error>
where E: Sync + Error, U: Send + 'static, V: Send + 'static, EN: Sync + Send + Error + 'static,

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));
    }
);
Source

pub fn and_then<P, EN>( self, rhs: ResultHandle<O, P, EN>, ) -> ResultHandle<I, P, Error>
where E: Sync + Error, P: Send + 'static, EN: Sync + Send + Error + 'static,

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);
    }
);
Source

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);
    }
);
Source

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);
    }
);

Trait Implementations§

Source§

impl<I, O> Clone for Handle<I, O>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<I, O> !Unpin for Handle<I, O>

§

impl<I, O> !UnsafeUnpin for Handle<I, O>

§

impl<I, O> Freeze for Handle<I, O>

§

impl<I, O> RefUnwindSafe for Handle<I, O>

§

impl<I, O> Send for Handle<I, O>
where I: Send, O: Send,

§

impl<I, O> Sync for Handle<I, O>
where I: Send, O: Send,

§

impl<I, O> UnwindSafe for Handle<I, O>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.