Struct ThreadedActor

Source
pub struct ThreadedActor<T> { /* private fields */ }
Expand description

An actor that uses an OS thread-per-instance.

This may be useful if the application only needs a few actors and doesn’ otherwise use an async runtime. Or it can be used in an async context if an actor needs to block or is compute intensive and requires its own thread.

Implementations§

Source§

impl<T> ThreadedActor<T>
where T: Default + Send + 'static,

Source

pub fn new() -> Self

Creates an actor with a default initial state.

This requires the state type to implement the Default trait.

Examples found in repository?
examples/threaded_keyval.rs (line 29)
28    pub fn new() -> Self {
29        Self { actor: ThreadedActor::new() }
30    }
Source§

impl<T> ThreadedActor<T>
where T: Send + 'static,

Source

pub fn from_state(state: T) -> Self

Creates a threaded actor with the specified initial state.

Source

pub fn cast<F>(&self, f: F)
where F: FnOnce(&mut T) + Send + 'static,

Send an asynchronous request to the actor.

This queues the request and returns immediately.

Examples found in repository?
examples/threaded_keyval.rs (lines 41-43)
33    pub fn insert<K,V>(&self, key: K, val: V)
34    where
35        K: Into<String>,
36        V: Into<String>,
37    {
38        let key = key.into();
39        let val = val.into();
40
41        self.actor.cast(move |state| {
42            state.insert(key, val);
43        });
44    }
Source

pub fn call<F, R>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R + Send + 'static, R: Send + 'static,

Examples found in repository?
examples/threaded_keyval.rs (lines 51-53)
49    pub fn get<K: Into<String>>(&self, key: K) -> Option<String> {
50        let key = key.into();
51        self.actor.call(move |state| {
52            state.get(&key).map(|v| v.to_string())
53        })
54    }

Trait Implementations§

Source§

impl<T: Clone> Clone for ThreadedActor<T>

Source§

fn clone(&self) -> ThreadedActor<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ThreadedActor<T>

§

impl<T> RefUnwindSafe for ThreadedActor<T>

§

impl<T> Send for ThreadedActor<T>

§

impl<T> Sync for ThreadedActor<T>

§

impl<T> Unpin for ThreadedActor<T>

§

impl<T> UnwindSafe for ThreadedActor<T>

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.