Struct HandleCommon

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

Contains common functionality that is shared between Handle and BlockingHandle.

Implementations§

Source§

impl<T> HandleCommon<T>

Source

pub fn is_stopped(&self) -> bool

Checks if the agent has been stopped.

use tokio_agent::{Agent, Result};

#[tokio::main]
async fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 1).unwrap();
    let agent_2 = agent.clone();

    agent.stop().await?;

    assert!(agent_2.is_stopped());

    Ok(())
}
Source

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

Performs a cast (fire and forget) operation on the agent state.

The function func is sent to the agent, which then invokes the function, passing a mutable reference to the agent’s state.

This function will return true if the agent was not stopped at the time of invoking cast, however, a true result does not guarantee that your function will actually exit, as the agent may be stopped prior to evaluating your function.

use tokio_agent::Agent;

let agent = Agent::spawn_thread(|| 42).unwrap().blocking();

agent.cast(|x| {
    *x += 1;
    Agent::Continue(())
});
assert_eq!(agent.get(|x| *x).unwrap(), 43);
Source

pub fn is_same_agent(&self, other: &HandleCommon<T>) -> bool

Returns true if the Agent refered to by this handle is the same as other’s Agent.

use tokio_agent::Agent;

let agent = Agent::spawn_thread(|| "the agent").unwrap();
let agent_2 = agent.clone();

assert!(agent_2.is_same_agent(&agent));

// Additionally, a BlockingHandle can be compared with an Handle as well.

let agent_2 = agent_2.blocking();
assert!(agent_2.is_same_agent(&agent));
assert!(agent.is_same_agent(&agent_2));

let imposter = Agent::spawn_thread(|| "sus").unwrap();
assert!(!agent.is_same_agent(&imposter));

Trait Implementations§

Source§

impl<T> Clone for HandleCommon<T>

Source§

fn clone(&self) -> Self

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 HandleCommon<T>

§

impl<T> RefUnwindSafe for HandleCommon<T>

§

impl<T> Send for HandleCommon<T>

§

impl<T> Sync for HandleCommon<T>

§

impl<T> Unpin for HandleCommon<T>

§

impl<T> UnwindSafe for HandleCommon<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.