1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/// DeserializerAdapter is used to fit serde Deserializer implementations and
/// DataReader together.
///
/// DataReader cannot assume a specific serialization
/// format, so it needs to be given as a parameter.
///
/// for WITH_KEY topics, we need to be able to (de)serialize the key in addition
/// to data.
pub mod no_key {
use bytes::Bytes;
use crate::RepresentationIdentifier;
/// trait for connecting a Deserializer implementation and DataReader
/// together - no_key version.
pub trait DeserializerAdapter<D> {
type Error: std::error::Error; // Error type
/// Which data representations can the DeserializerAdapter read?
/// See RTPS specification Section 10 and Table 10.3
fn supported_encodings() -> &'static [RepresentationIdentifier];
/// Deserialize data from bytes to an object.
/// `encoding` must be something given by `supported_encodings()`, or
/// implementation may fail with Err or `panic!()`.
fn from_bytes(input_bytes: &[u8], encoding: RepresentationIdentifier)
-> Result<D, Self::Error>;
/// This method has a default implementation, but the default will make a
/// copy of all the input data in memory and then call from_bytes() .
// In order to avoid the copy, implement also this method.
fn from_vec_bytes(
input_vec_bytes: &[Bytes],
encoding: RepresentationIdentifier,
) -> Result<D, Self::Error> {
let total_len = input_vec_bytes.iter().map(Bytes::len).sum();
let mut total_payload = Vec::with_capacity(total_len);
for iv in input_vec_bytes {
total_payload.extend(iv);
}
Self::from_bytes(&total_payload, encoding)
}
}
/// trait for connecting a Serializer implementation and DataWriter
/// together - no_key version.
pub trait SerializerAdapter<D> {
type Error: std::error::Error; // Error type
// what encoding do we produce?
fn output_encoding() -> RepresentationIdentifier;
fn to_bytes(value: &D) -> Result<Bytes, Self::Error>;
}
}
pub mod with_key {
use bytes::Bytes;
use crate::{Keyed, RepresentationIdentifier};
use super::no_key;
/// trait for connecting a Deserializer implementation and DataReader
/// together - with_key version.
pub trait DeserializerAdapter<D>: no_key::DeserializerAdapter<D>
where
D: Keyed,
{
/// Deserialize a key `D::K` from bytes.
fn key_from_bytes(
input_bytes: &[u8],
encoding: RepresentationIdentifier,
) -> Result<D::K, Self::Error>;
}
/// trait for connecting a Serializer implementation and DataWriter
/// together - with_key version.
pub trait SerializerAdapter<D>: no_key::SerializerAdapter<D>
where
D: Keyed,
{
/// serialize a key `D::K` to Bytes.
fn key_to_bytes(value: &D::K) -> Result<Bytes, Self::Error>;
}
}