rustdds/dds/with_key/
datasample.rs1use crate::{
2 dds::{key::*, sampleinfo::*, with_key::datawriter::WriteOptions},
3 structure::{
4 cache_change::CacheChange, guid::GUID, sequence_number::SequenceNumber, time::Timestamp,
5 },
6};
7
8#[derive(Clone, PartialEq, Debug)]
18pub enum Sample<D, K> {
19 Value(D),
20 Dispose(K),
21}
22
23impl<D, K> Sample<D, K> {
24 pub fn value(self) -> Option<D> {
25 match self {
26 Sample::Value(d) => Some(d),
27 Sample::Dispose(_) => None,
28 }
29 }
30
31 pub fn map_value<D2, F: FnOnce(D) -> D2>(self, op: F) -> Sample<D2, K> {
32 match self {
33 Sample::Value(d) => Sample::Value(op(d)),
34 Sample::Dispose(k) => Sample::Dispose(k),
35 }
36 }
37
38 pub fn map_dispose<K2, F: FnOnce(K) -> K2>(self, op: F) -> Sample<D, K2> {
39 match self {
40 Sample::Value(d) => Sample::Value(d),
41 Sample::Dispose(k) => Sample::Dispose(op(k)),
42 }
43 }
44
45 pub fn unwrap(self) -> D {
46 match self {
47 Sample::Value(d) => d,
48 Sample::Dispose(_k) => panic!("Unwrap called on a Sample with no data"),
49 }
50 }
51
52 pub const fn as_ref(&self) -> Sample<&D, &K> {
53 match *self {
54 Sample::Value(ref d) => Sample::Value(d),
55 Sample::Dispose(ref k) => Sample::Dispose(k),
56 }
57 }
58}
59
60#[derive(PartialEq, Debug)]
78pub struct DataSample<D: Keyed> {
79 pub(crate) sample_info: SampleInfo, pub(crate) value: Sample<D, D::K>,
82}
83
84impl<D> DataSample<D>
85where
86 D: Keyed,
87{
88 pub(crate) fn new(sample_info: SampleInfo, value: Sample<D, D::K>) -> Self {
89 Self { sample_info, value }
90 }
91
92 pub fn key(&self) -> D::K {
95 match &self.value {
96 Sample::Value(d) => d.key(),
97 Sample::Dispose(k) => k.clone(),
98 }
99 } pub fn value(&self) -> &Sample<D, D::K> {
102 &self.value
103 }
104
105 pub fn into_value(self) -> Sample<D, D::K> {
106 self.value
107 }
108
109 pub fn sample_info(&self) -> &SampleInfo {
110 &self.sample_info
111 }
112
113 pub fn sample_info_mut(&mut self) -> &mut SampleInfo {
114 &mut self.sample_info
115 }
116} #[derive(Debug, Clone)]
121pub struct DeserializedCacheChange<D: Keyed> {
122 pub(crate) receive_instant: Timestamp, pub(crate) writer_guid: GUID, pub(crate) sequence_number: SequenceNumber, pub(crate) write_options: WriteOptions, pub(crate) sample: Sample<D, D::K>, }
132
133impl<D: Keyed> DeserializedCacheChange<D> {
134 pub fn new(receive_instant: Timestamp, cc: &CacheChange, deserialized: Sample<D, D::K>) -> Self {
135 DeserializedCacheChange {
136 receive_instant,
137 writer_guid: cc.writer_guid,
138 sequence_number: cc.sequence_number,
139 write_options: cc.write_options.clone(),
140 sample: deserialized,
141 }
142 }
143}