pub struct BlockingHandle<T>(/* private fields */);
Expand description
Implementations§
Source§impl<T> BlockingHandle<T>
impl<T> BlockingHandle<T>
Sourcepub fn nonblocking(self) -> Handle<T>
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.
Sourcepub fn call<F, R>(&self, func: F) -> Result<R>
pub fn call<F, R>(&self, func: F) -> Result<R>
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(())
}
Sourcepub fn clone_state(&self) -> Result<T>
pub fn clone_state(&self) -> Result<T>
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(())
}
Sourcepub fn get<F, R>(&self, func: F) -> Result<R>
pub fn get<F, R>(&self, func: F) -> Result<R>
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(())
}
Sourcepub fn get_and_update<F, R>(&self, func: F) -> Result<R>
pub fn get_and_update<F, R>(&self, func: F) -> Result<R>
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(())
}
Sourcepub fn update<F>(&self, func: F) -> Result<()>
pub fn update<F>(&self, func: F) -> Result<()>
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(())
}
Sourcepub fn replace(&self, value: T) -> Result<T>where
T: Send + 'static,
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(())
}
Sourcepub fn take(&self) -> Result<T>
pub fn take(&self) -> Result<T>
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(())
}
Sourcepub fn stop(self) -> Result<()>
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>>§
Sourcepub fn is_stopped(&self) -> bool
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(())
}
Sourcepub fn cast<F>(&self, func: F) -> bool
pub fn cast<F>(&self, func: F) -> bool
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);
Sourcepub fn is_same_agent(&self, other: &HandleCommon<T>) -> bool
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>
impl<T: Clone> Clone for BlockingHandle<T>
Source§fn clone(&self) -> BlockingHandle<T>
fn clone(&self) -> BlockingHandle<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more