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 #[deprecated(note = "panics on Sample::Dispose; use value() or match on Sample::Value/Dispose")]
46 pub fn unwrap(self) -> D {
53 match self {
54 Sample::Value(d) => d,
55 Sample::Dispose(_k) => panic!(
56 "Unwrap called on a Sample with no data (Dispose notification). Use Sample::value() \
57 instead."
58 ),
59 }
60 }
61
62 pub const fn as_ref(&self) -> Sample<&D, &K> {
63 match *self {
64 Sample::Value(ref d) => Sample::Value(d),
65 Sample::Dispose(ref k) => Sample::Dispose(k),
66 }
67 }
68}
69
70#[derive(PartialEq, Debug)]
88pub struct DataSample<D: Keyed> {
89 pub(crate) sample_info: SampleInfo, pub(crate) value: Sample<D, D::K>,
92}
93
94impl<D> DataSample<D>
95where
96 D: Keyed,
97{
98 pub(crate) fn new(sample_info: SampleInfo, value: Sample<D, D::K>) -> Self {
99 Self { sample_info, value }
100 }
101
102 pub fn key(&self) -> D::K {
105 match &self.value {
106 Sample::Value(d) => d.key(),
107 Sample::Dispose(k) => k.clone(),
108 }
109 } pub fn value(&self) -> &Sample<D, D::K> {
112 &self.value
113 }
114
115 pub fn into_value(self) -> Sample<D, D::K> {
116 self.value
117 }
118
119 pub fn sample_info(&self) -> &SampleInfo {
120 &self.sample_info
121 }
122
123 pub fn sample_info_mut(&mut self) -> &mut SampleInfo {
124 &mut self.sample_info
125 }
126} #[derive(Debug, Clone)]
131pub struct DeserializedCacheChange<D: Keyed> {
132 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>, }
142
143impl<D: Keyed> DeserializedCacheChange<D> {
144 pub fn new(receive_instant: Timestamp, cc: &CacheChange, deserialized: Sample<D, D::K>) -> Self {
145 DeserializedCacheChange {
146 receive_instant,
147 writer_guid: cc.writer_guid,
148 sequence_number: cc.sequence_number,
149 write_options: cc.write_options.clone(),
150 sample: deserialized,
151 }
152 }
153}