use crate::{
dds::{key::*, sampleinfo::*, with_key::datawriter::WriteOptions},
structure::{
cache_change::CacheChange, guid::GUID, sequence_number::SequenceNumber, time::Timestamp,
},
};
#[derive(Clone, PartialEq, Debug)]
pub enum Sample<D, K> {
Value(D),
Dispose(K),
}
impl<D, K> Sample<D, K> {
pub fn value(self) -> Option<D> {
match self {
Sample::Value(d) => Some(d),
Sample::Dispose(_) => None,
}
}
pub fn map_value<D2, F: FnOnce(D) -> D2>(self, op: F) -> Sample<D2, K> {
match self {
Sample::Value(d) => Sample::Value(op(d)),
Sample::Dispose(k) => Sample::Dispose(k),
}
}
pub fn map_dispose<K2, F: FnOnce(K) -> K2>(self, op: F) -> Sample<D, K2> {
match self {
Sample::Value(d) => Sample::Value(d),
Sample::Dispose(k) => Sample::Dispose(op(k)),
}
}
pub fn unwrap(self) -> D {
match self {
Sample::Value(d) => d,
Sample::Dispose(_k) => panic!("Unwrap called on a Sample with no data"),
}
}
pub const fn as_ref(&self) -> Sample<&D, &K> {
match *self {
Sample::Value(ref d) => Sample::Value(d),
Sample::Dispose(ref k) => Sample::Dispose(k),
}
}
}
#[derive(PartialEq, Debug)]
pub struct DataSample<D: Keyed> {
pub(crate) sample_info: SampleInfo,
pub(crate) value: Sample<D, D::K>,
}
impl<D> DataSample<D>
where
D: Keyed,
{
pub(crate) fn new(sample_info: SampleInfo, value: Sample<D, D::K>) -> Self {
Self { sample_info, value }
}
pub fn key(&self) -> D::K {
match &self.value {
Sample::Value(d) => d.key(),
Sample::Dispose(k) => k.clone(),
}
}
pub fn value(&self) -> &Sample<D, D::K> {
&self.value
}
pub fn into_value(self) -> Sample<D, D::K> {
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: Keyed> {
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>,
}
impl<D: Keyed> DeserializedCacheChange<D> {
pub fn new(receive_instant: Timestamp, cc: &CacheChange, deserialized: Sample<D, D::K>) -> Self {
DeserializedCacheChange {
receive_instant,
writer_guid: cc.writer_guid,
sequence_number: cc.sequence_number,
write_options: cc.write_options.clone(),
sample: deserialized,
}
}
}