tirea_contract/transport/
transcoder.rs1use std::marker::PhantomData;
4
5pub trait Transcoder: Send {
10 type Input: Send + 'static;
12 type Output: Send + 'static;
14
15 fn prologue(&mut self) -> Vec<Self::Output> {
17 Vec::new()
18 }
19
20 fn transcode(&mut self, item: &Self::Input) -> Vec<Self::Output>;
22
23 fn epilogue(&mut self) -> Vec<Self::Output> {
25 Vec::new()
26 }
27}
28
29pub struct Identity<T>(PhantomData<T>);
31
32impl<T> Default for Identity<T> {
33 fn default() -> Self {
34 Self(PhantomData)
35 }
36}
37
38impl<T: Clone + Send + 'static> Transcoder for Identity<T> {
39 type Input = T;
40 type Output = T;
41
42 fn transcode(&mut self, item: &Self::Input) -> Vec<Self::Output> {
43 vec![item.clone()]
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn identity_passthrough() {
53 let mut t = Identity::<u32>::default();
54 assert_eq!(t.transcode(&42), vec![42]);
55 }
56}