pub struct SampleOrKey<T>where
T: Topicable,{ /* private fields */ }Expand description
A received sample, which is either a full payload of type
T or a key-only payload carrying
T::Key.
Key-only samples are produced when an instance is disposed or unregistered
by a writer. SampleOrKey derefs to T in both cases: for key-only
samples this materializes a default T from the key via
Topicable::from_key.
Use view to distinguish between the two cases without
triggering materialisation.
Implementations§
Source§impl<T> SampleOrKey<T>where
T: Topicable,
impl<T> SampleOrKey<T>where
T: Topicable,
Sourcepub const fn info(&self) -> &Info
pub const fn info(&self) -> &Info
Returns the metadata associated with this sample.
§Examples
let sample = &reader.take()?[0];
let info = sample.info();
println!("source timestamp: {:?}", info.source_timestamp);Sourcepub fn sample(&self) -> Option<&T>
pub fn sample(&self) -> Option<&T>
Returns a reference to the full sample payload, or None if this is a
key-only sample.
§Examples
let sample = &reader.take()?[0];
if let Some(data) = sample.sample() {
println!("payload: {data:?}");
}Sourcepub fn into_sample(self) -> Option<T>
pub fn into_sample(self) -> Option<T>
Consumes self and returns the full sample payload, or None if this
is a key-only sample.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
if let Some(data) = sample.into_sample() {
println!("payload: {data:?}");
}Sourcepub const fn is_sample(&self) -> bool
pub const fn is_sample(&self) -> bool
Returns true if this is a full sample.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
assert!(sample.is_sample());Sourcepub fn is_sample_and(&self, f: impl FnOnce(&T) -> bool) -> bool
pub fn is_sample_and(&self, f: impl FnOnce(&T) -> bool) -> bool
Returns true if this is a full sample and f returns true for its
payload.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
assert!(sample.is_sample_and(|data| data.x == 0));Sourcepub fn key(&self) -> Option<&T::Key>
pub fn key(&self) -> Option<&T::Key>
Returns a reference to the instance key, or None if this is a full
sample.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
if let Some(key) = sample.key() {
println!("key-only notification: {key:?}");
}Sourcepub fn into_key(self) -> Option<T::Key>
pub fn into_key(self) -> Option<T::Key>
Consumes self and returns the instance key, or None if this is a
full sample.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
if let Some(key) = sample.into_key() {
println!("key-only notification: {key:?}");
}Sourcepub const fn is_key(&self) -> bool
pub const fn is_key(&self) -> bool
Returns true if this is a key-only sample.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
assert!(!sample.is_key());Sourcepub fn is_key_and(&self, f: impl FnOnce(&T::Key) -> bool) -> bool
pub fn is_key_and(&self, f: impl FnOnce(&T::Key) -> bool) -> bool
Returns true if this is a key-only sample and f returns true for
its key.
§Examples
let sample = reader.take()?.into_iter().next().unwrap();
assert!(!sample.is_key_and(|key| key.x == 1));Sourcepub fn view(&self) -> View<'_, T>
pub fn view(&self) -> View<'_, T>
Returns a borrowed View of this sample for pattern matching without
triggering key or sample materialisation.
§Examples
use cyclonedds::sample::View;
for sample in reader.take()? {
match sample.view() {
View::Sample(data) => println!("sample: {data:?}"),
View::Key(key) => println!("key-only: {key:?}"),
}
}