Skip to main content

ReadCondition

Struct ReadCondition 

Source
pub struct ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: Topicable,
{ /* private fields */ }
Expand description

A filter on a Reader that restricts samples by their State.

A ReadCondition is created against a reader with a state mask and can be attached to a WaitSet to trigger when matching samples become available. Reading via the condition returns only samples whose combined sample, view, and instance state matches the mask.

§Examples

use cyclonedds::state;
use cyclonedds::{Duration, ReadCondition, WaitSet};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;

let condition = ReadCondition::new(
    &reader,
    state::sample::Fresh | state::instance::Any | state::view::Any,
)?;
let mut waitset = WaitSet::<()>::new(&participant)?;
waitset.attach(&condition, None)?;
waitset.wait(Duration::INFINITE)?;

let samples = condition.take()?;

Implementations§

Source§

impl<'d, 'p, 't, 'r, T> ReadCondition<'d, 'p, 't, 'r, T>
where T: Topicable,

Source

pub fn new(reader: &'r Reader<'d, 'p, 't, T>, mask: State) -> Result<Self>

Creates a new ReadCondition on reader that matches samples whose state satisfies mask.

§Errors

Returns an Error if the read condition fails to create.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let condition = ReadCondition::new(&reader, state::sample::Fresh)?;
Source

pub fn mask(&self) -> Result<State>

Returns the state mask this condition was created with.

§Errors

Returns an Error if the mask returned by the read condition is invalid.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let condition = ReadCondition::new(&reader, state::sample::Fresh)?;
assert_eq!(condition.mask()?, state::sample::Fresh);
Source

pub fn triggered(&self) -> Result<bool>

Returns true if this condition is currently triggered.

A condition is triggered when samples matching its mask are available in the reader cache.

§Errors

Returns an Error if the read condition fails to read the trigger state.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let writer = Writer::new(&topic)?;

let condition = ReadCondition::new(&reader, state::sample::Fresh)?;
writer.write(&Data::default())?;
assert!(condition.triggered()?);
Ok::<_, cyclonedds::Error>(())
Source

pub fn take(&self) -> Result<Vec<SampleOrKey<T>>>
where T: Clone,

Removes and returns all samples matching this condition’s mask from the reader cache.

§Errors

Returns an Error if the read condition fails to take samples.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let writer = Writer::new(&topic)?;

let condition = ReadCondition::new(
    &reader,
    state::sample::Stale | state::instance::Any | state::view::Any,
)?;
writer.write(&Data::default())?;

// No sample matches this state initially.
let samples = condition.take()?;
assert_eq!(samples.len(), 0);

// Attempt a normal read.
assert_eq!(reader.read()?.len(), 1);

// Sample should now match this state because they're stale.
let samples = condition.take()?;
assert_eq!(samples.len(), 1);

// Samples should be removed from the cache.
assert_eq!(condition.take()?.len(), 0);
Source

pub fn read(&self) -> Result<Vec<SampleOrKey<T>>>
where T: Clone,

Returns all samples matching this condition’s mask without removing them from the reader cache.

§Errors

Returns an Error if the read condition fails to read samples.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let writer = Writer::new(&topic)?;

let condition = ReadCondition::new(
    &reader,
    state::sample::Stale | state::instance::Any | state::view::Any,
)?;
writer.write(&Data::default())?;

// No sample matches this state initially.
let samples = condition.read()?;
assert_eq!(samples.len(), 0);

// Attempt a normal read.
assert_eq!(reader.read()?.len(), 1);

// Sample should now match this state because they're stale.
let samples = condition.read()?;
assert_eq!(samples.len(), 1);

// Samples remain in the cache.
assert_eq!(condition.read()?.len(), 1);
Source

pub fn peek(&self) -> Result<Vec<SampleOrKey<T>>>
where T: Clone,

Returns all samples matching this condition’s mask without marking them as read or removing them from the cache.

§Errors

Returns an Error if the read condition fails to peek samples.

§Examples
use cyclonedds::{ReadCondition, state};

let topic = Topic::<Data>::new(&participant, "MyTopic")?;
let reader = Reader::new(&topic)?;
let writer = Writer::new(&topic)?;

let condition = ReadCondition::new(
    &reader,
    state::sample::Stale | state::instance::Any | state::view::Any,
)?;
writer.write(&Data::default())?;

// No sample matches this state initially.
let samples = condition.peek()?;
assert_eq!(samples.len(), 0);

// Attempt a normal read.
assert_eq!(reader.read()?.len(), 1);

// Sample should now match this state because they're stale.
let samples = condition.peek()?;
assert_eq!(samples.len(), 1);

// Samples remain in the cache.
assert_eq!(condition.peek()?.len(), 1);

Trait Implementations§

Source§

impl<'domain, 'participant, 'topic, 'reader, T> Debug for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: Topicable + Debug,

Source§

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

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

impl<T> Drop for ReadCondition<'_, '_, '_, '_, T>
where T: Topicable,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Topicable> Entity for ReadCondition<'_, '_, '_, '_, T>

Source§

fn id(&self) -> EntityId

Returns the EntityId of this entity. Read more
Source§

fn instance_handle(&self) -> Result<InstanceHandle>

Returns the InstanceHandle of this entity. Read more
Source§

fn status_changes(&self) -> Result<Status>

Returns the set of status flags that have changed since they were last read or taken. Read more
Source§

fn take_status(&self, mask: Option<Status>) -> Result<Status>

Takes and clears the status flags matching mask, or all flags if mask is None. Read more
Source§

fn read_status(&self, mask: Option<Status>) -> Result<Status>

Reads the status flags matching mask without clearing them, or all flags if mask is None. Read more
Source§

fn status_mask(&self) -> Result<Status>

Returns the status mask enabled on the entity. Read more
Source§

fn set_status_mask(&self, mask: Status) -> Result<()>

Sets and enables a status mask on the entity. Read more

Auto Trait Implementations§

§

impl<'domain, 'participant, 'topic, 'reader, T> Freeze for ReadCondition<'domain, 'participant, 'topic, 'reader, T>

§

impl<'domain, 'participant, 'topic, 'reader, T> RefUnwindSafe for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: RefUnwindSafe,

§

impl<'domain, 'participant, 'topic, 'reader, T> Send for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: Sync,

§

impl<'domain, 'participant, 'topic, 'reader, T> Sync for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: Sync,

§

impl<'domain, 'participant, 'topic, 'reader, T> Unpin for ReadCondition<'domain, 'participant, 'topic, 'reader, T>

§

impl<'domain, 'participant, 'topic, 'reader, T> UnsafeUnpin for ReadCondition<'domain, 'participant, 'topic, 'reader, T>

§

impl<'domain, 'participant, 'topic, 'reader, T> UnwindSafe for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
where T: RefUnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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.