pub struct HandleCommon<T> { /* private fields */ }
Expand description
Contains common functionality that is shared between Handle
and BlockingHandle
.
Implementations§
Source§impl<T> HandleCommon<T>
impl<T> 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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more