Struct BlockingHandle

Source
pub struct BlockingHandle<T>(/* private fields */);
Expand description

The “Blocking” variant of Handle which can be used to interact with an Agent outside of a tokio runtime context.

Implementations§

Source§

impl<T> BlockingHandle<T>

Source

pub fn nonblocking(self) -> Handle<T>

Converts this BlockingHandle back into an Handle which allows you to interact with the the Agent inside of a tokio runtime context.

Source

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

The blocking variant of Handle::call.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 0).unwrap().blocking();
    let result = agent.call(|x| {
        *x += 1;
        Agent::Continue(*x)
    })?;

    assert_eq!(result, 1);
    Ok(())
}
Source

pub fn clone_state(&self) -> Result<T>
where T: Send + Clone + 'static,

The blocking variant of Handle::clone_state.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let result = agent.clone_state()?;
    assert_eq!(result, 42);
    Ok(())
}
Source

pub fn get<F, R>(&self, func: F) -> Result<R>
where F: FnOnce(&T) -> R + Send + 'static, R: Send + 'static,

The blocking variant of Handle::get.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let result = agent.get(|x| *x)?;
    assert_eq!(result, 42);
    Ok(())
}
Source

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

The blocking variant of Handle::get_and_update.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let result = agent.get_and_update(|x| {
        *x += 1;
        *x
    })?;
    assert_eq!(result, 43);

    Ok(())
}
Source

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

The blocking variant of Handle::update.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    agent.update(|x| { *x += 1; })?;
    let result = agent.get(|x| *x)?;
    assert_eq!(result, 43);
    Ok(())
}
Source

pub fn replace(&self, value: T) -> Result<T>
where T: Send + 'static,

The blocking variant of Handle::replace.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let result = agent.replace(43)?;
    assert_eq!(result, 42);
    let result = agent.get(|x| *x)?;
    assert_eq!(result, 43);
    Ok(())
}
Source

pub fn take(&self) -> Result<T>
where T: Default + Send + 'static,

The blocking variant of Handle::take.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let result = agent.take()?;
    assert_eq!(result, 42);
    let result = agent.get(|x| *x)?;
    assert_eq!(result, 0);

    Ok(())
}
Source

pub fn stop(self) -> Result<()>

The blocking variant of Handle::stop.

use tokio_agent::{Agent, Result};

fn main() -> Result<()> {
    let agent = Agent::spawn_thread(|| 42).unwrap().blocking();
    let agent_2 = agent.clone();

    agent.stop()?;

    assert!(agent_2.stop().is_err());

    Ok(())
}

Methods from Deref<Target = 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> Clone for BlockingHandle<T>

Source§

fn clone(&self) -> BlockingHandle<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
Source§

impl<T> Debug for BlockingHandle<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for BlockingHandle<T>

Source§

type Target = HandleCommon<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for BlockingHandle<T>

§

impl<T> RefUnwindSafe for BlockingHandle<T>

§

impl<T> Send for BlockingHandle<T>

§

impl<T> Sync for BlockingHandle<T>

§

impl<T> Unpin for BlockingHandle<T>

§

impl<T> UnwindSafe for BlockingHandle<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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.