rclrs

Struct GuardCondition

Source
pub struct GuardCondition { /* private fields */ }
Expand description

A waitable entity used for waking up a wait set manually.

If a wait set that is currently waiting on events should be interrupted from a separate thread, this can be done by adding an Arc<GuardCondition> to the wait set, and calling trigger() on the same GuardCondition while the wait set is waiting.

The guard condition may be reused multiple times, but like other waitable entities, can not be used in multiple wait sets concurrently.

§Example


let context = Context::default();

let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
let atomic_bool_for_closure = Arc::clone(&atomic_bool);

let gc = Arc::new(GuardCondition::new_with_callback(
    &context,
    move || {
        atomic_bool_for_closure.store(true, Ordering::Relaxed);
    },
));

let mut ws = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
ws.add_guard_condition(Arc::clone(&gc))?;

// Trigger the guard condition, firing the callback and waking the wait set being waited on, if any.
gc.trigger()?;

// The provided callback has now been called.
assert_eq!(atomic_bool.load(Ordering::Relaxed), true);

// The wait call will now immediately return.
ws.wait(Some(std::time::Duration::from_millis(10)))?;

Implementations§

Source§

impl GuardCondition

Source

pub fn new(context: &Context) -> Self

Creates a new guard condition with no callback.

Source

pub fn new_with_callback<F>(context: &Context, callback: F) -> Self
where F: Fn() + Send + Sync + 'static,

Creates a new guard condition with a callback.

Source

pub fn trigger(&self) -> Result<(), RclrsError>

Triggers this guard condition, activating the wait set, and calling the optionally assigned callback.

Trait Implementations§

Source§

impl Drop for GuardCondition

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl PartialEq for GuardCondition

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for GuardCondition

Auto Trait Implementations§

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> 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, 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.