microrti_encoding/
element.rs1use std::any::{Any, TypeId};
4use std::fmt;
5
6use crate::buffer::{ByteReader, ByteWriter};
7use crate::error::{DecoderError, EncoderError};
8use crate::typed::{Decode, Encode};
9
10pub type DynElement = dyn DataElement;
12
13pub trait DataElement: Any + fmt::Debug + Send + Sync {
22 fn octet_boundary(&self) -> usize;
24
25 fn encode_into(&self, writer: &mut ByteWriter<'_>) -> Result<(), EncoderError>;
27
28 fn decode_from(&mut self, reader: &mut ByteReader<'_>) -> Result<(), DecoderError>;
32
33 fn clone_element(&self) -> Box<DynElement>;
35
36 fn encoded_length(&self) -> Result<usize, EncoderError> {
38 let mut counter = ByteWriter::counter();
39 self.encode_into(&mut counter)?;
40 counter.status()?;
41 Ok(counter.position())
42 }
43
44 fn is_same_type_as(&self, other: &DynElement) -> bool {
47 self.element_type_id() == other.element_type_id()
48 }
49
50 #[doc(hidden)]
52 fn element_type_id(&self) -> TypeId {
53 TypeId::of::<Self>()
54 }
55
56 fn to_bytes(&self) -> Result<Vec<u8>, EncoderError> {
58 let mut buffer = Vec::new();
59 self.append_to(&mut buffer)?;
60 Ok(buffer)
61 }
62
63 fn append_to(&self, buffer: &mut Vec<u8>) -> Result<(), EncoderError> {
66 let start = buffer.len();
67 let mut writer = ByteWriter::new(buffer);
68 let result = self.encode_into(&mut writer).and_then(|()| writer.status());
69 if result.is_err() {
70 buffer.truncate(start);
71 }
72 result
73 }
74
75 fn decode_slice(&mut self, bytes: &[u8]) -> Result<(), DecoderError> {
77 self.decode_from(&mut ByteReader::new(bytes))
78 }
79
80 fn decode_slice_strict(&mut self, bytes: &[u8]) -> Result<(), DecoderError> {
82 let mut reader = ByteReader::new(bytes);
83 self.decode_from(&mut reader)?;
84 reader.expect_end()
85 }
86}
87
88impl<T> DataElement for T
89where
90 T: Decode + Clone + fmt::Debug + Send + Sync + 'static,
91{
92 fn octet_boundary(&self) -> usize {
93 <T as Encode>::OCTET_BOUNDARY
94 }
95
96 fn encode_into(&self, writer: &mut ByteWriter<'_>) -> Result<(), EncoderError> {
97 Encode::encode(self, writer);
98 writer.status()
99 }
100
101 fn decode_from(&mut self, reader: &mut ByteReader<'_>) -> Result<(), DecoderError> {
102 *self = T::decode(reader)?;
103 Ok(())
104 }
105
106 fn clone_element(&self) -> Box<DynElement> {
107 Box::new(self.clone())
108 }
109}
110
111impl dyn DataElement {
112 pub fn is<T: DataElement>(&self) -> bool {
114 let any: &dyn Any = self;
115 any.is::<T>()
116 }
117
118 pub fn downcast_ref<T: DataElement>(&self) -> Option<&T> {
120 let any: &dyn Any = self;
121 any.downcast_ref::<T>()
122 }
123
124 pub fn downcast_mut<T: DataElement>(&mut self) -> Option<&mut T> {
126 let any: &mut dyn Any = self;
127 any.downcast_mut::<T>()
128 }
129}
130
131impl Clone for Box<DynElement> {
132 fn clone(&self) -> Self {
133 (**self).clone_element()
134 }
135}
136
137impl PartialEq for dyn DataElement {
140 fn eq(&self, other: &Self) -> bool {
141 self.is_same_type_as(other)
142 && matches!((self.to_bytes(), other.to_bytes()), (Ok(left), Ok(right)) if left == right)
143 }
144}