use crate::{
dds::{
no_key::wrappers::NoKeyWrapper,
sampleinfo::SampleInfo,
with_key,
with_key::{
datasample::{DataSample as WithKeyDataSample, Sample},
datawriter::WriteOptions,
},
},
rpc::SampleIdentity,
structure::{
cache_change::CacheChange, guid::GUID, sequence_number::SequenceNumber, time::Timestamp,
},
};
#[derive(PartialEq, Eq, Debug)]
pub struct DataSample<D> {
pub(crate) sample_info: SampleInfo,
pub(crate) value: D,
}
impl<D> DataSample<D> {
pub(crate) fn from_with_key(keyed: WithKeyDataSample<NoKeyWrapper<D>>) -> Option<Self> {
match keyed.value {
Sample::Value(kv) => Some(Self {
sample_info: keyed.sample_info,
value: kv.d,
}),
Sample::Dispose(_) => None,
}
}
pub(crate) fn from_with_key_ref(
keyed: WithKeyDataSample<&NoKeyWrapper<D>>,
) -> Option<DataSample<&D>> {
match keyed.value {
Sample::Value(kv) => Some(DataSample::<&D> {
sample_info: keyed.sample_info,
value: &kv.d,
}),
Sample::Dispose(_) => None,
}
}
pub fn value(&self) -> &D {
&self.value
}
pub fn into_value(self) -> D {
self.value
}
pub fn sample_info(&self) -> &SampleInfo {
&self.sample_info
}
pub fn sample_info_mut(&mut self) -> &mut SampleInfo {
&mut self.sample_info
}
}
#[derive(Debug, Clone)]
pub struct DeserializedCacheChange<D> {
pub receive_instant: Timestamp,
pub writer_guid: GUID, pub sequence_number: SequenceNumber, pub write_options: WriteOptions,
pub sample: D,
}
impl<D> DeserializedCacheChange<D> {
pub fn new(receive_instant: Timestamp, cc: &CacheChange, deserialized: D) -> Self {
DeserializedCacheChange {
receive_instant,
writer_guid: cc.writer_guid,
sequence_number: cc.sequence_number,
write_options: cc.write_options.clone(),
sample: deserialized,
}
}
pub(crate) fn from_keyed(
kdcc: with_key::datasample::DeserializedCacheChange<NoKeyWrapper<D>>,
) -> Option<Self> {
match kdcc.sample {
Sample::Value(sample) => Some(DeserializedCacheChange {
receive_instant: kdcc.receive_instant,
writer_guid: kdcc.writer_guid,
sequence_number: kdcc.sequence_number,
write_options: kdcc.write_options,
sample: sample.d,
}),
Sample::Dispose(_key) => None,
}
}
pub fn into_value(self) -> D {
self.sample
}
pub fn source_timestamp(&self) -> Option<Timestamp> {
self.write_options.source_timestamp()
}
pub fn writer_guid(&self) -> GUID {
self.writer_guid
}
pub fn related_sample_identity(&self) -> Option<SampleIdentity> {
self.write_options.related_sample_identity()
}
}