Skip to main content

SampleOrKey

Struct SampleOrKey 

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

Source

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);
Source

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:?}");
}
Source

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:?}");
}
Source

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());
Source

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));
Source

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:?}");
}
Source

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:?}");
}
Source

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());
Source

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));
Source

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:?}"),
    }
}

Trait Implementations§

Source§

impl<T> Clone for SampleOrKey<T>
where T: Topicable + Clone, T::Key: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for SampleOrKey<T>
where T: Topicable + Debug, T::Key: Debug,

Source§

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

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

impl<T> Deref for SampleOrKey<T>
where T: Topicable,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> !Freeze for SampleOrKey<T>

§

impl<T> !RefUnwindSafe for SampleOrKey<T>

§

impl<T> !Sync for SampleOrKey<T>

§

impl<T> Send for SampleOrKey<T>
where T: Send, <T as Topicable>::Key: Send,

§

impl<T> Unpin for SampleOrKey<T>

§

impl<T> UnsafeUnpin for SampleOrKey<T>

§

impl<T> UnwindSafe for SampleOrKey<T>
where T: UnwindSafe, <T as Topicable>::Key: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.