grpc/
for_test.rs

1//! Code useful in tests.
2
3use bytes::Bytes;
4
5use crate::error::Error;
6use crate::marshall::Marshaller;
7use crate::result::Result;
8
9pub struct MarshallerString;
10
11impl Marshaller<String> for MarshallerString {
12    fn write(&self, m: &String, _estimated_size: u32, out: &mut Vec<u8>) -> Result<()> {
13        out.extend_from_slice(m.as_bytes());
14        Ok(())
15    }
16
17    fn read(&self, bytes: Bytes) -> Result<String> {
18        String::from_utf8(bytes.as_ref().to_vec())
19            .map_err(|_| Error::Other("failed to parse utf-8"))
20    }
21}
22
23pub struct MarshallerBytes;
24
25impl Marshaller<Vec<u8>> for MarshallerBytes {
26    fn write(&self, m: &Vec<u8>, _estimated_size: u32, out: &mut Vec<u8>) -> Result<()> {
27        out.extend_from_slice(&m);
28        Ok(())
29    }
30
31    fn read(&self, bytes: Bytes) -> Result<Vec<u8>> {
32        Ok(bytes.as_ref().to_vec())
33    }
34}