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,
impl<'d, 'p, 't, 'r, T> ReadCondition<'d, 'p, 't, 'r, T>where
T: Topicable,
Sourcepub fn new(reader: &'r Reader<'d, 'p, 't, T>, mask: State) -> Result<Self>
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)?;Sourcepub fn mask(&self) -> Result<State>
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);Sourcepub fn triggered(&self) -> Result<bool>
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>(())Sourcepub fn take(&self) -> Result<Vec<SampleOrKey<T>>>where
T: Clone,
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);Sourcepub fn read(&self) -> Result<Vec<SampleOrKey<T>>>where
T: Clone,
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);Sourcepub fn peek(&self) -> Result<Vec<SampleOrKey<T>>>where
T: Clone,
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>
impl<'domain, 'participant, 'topic, 'reader, T> Debug for ReadCondition<'domain, 'participant, 'topic, 'reader, T>
Source§impl<T> Drop for ReadCondition<'_, '_, '_, '_, T>where
T: Topicable,
impl<T> Drop for ReadCondition<'_, '_, '_, '_, T>where
T: Topicable,
Source§impl<T: Topicable> Entity for ReadCondition<'_, '_, '_, '_, T>
impl<T: Topicable> Entity for ReadCondition<'_, '_, '_, '_, T>
Source§fn instance_handle(&self) -> Result<InstanceHandle>
fn instance_handle(&self) -> Result<InstanceHandle>
InstanceHandle of this entity. Read more