xCommonLib/serial/
request_message.rs

1use crate::base::status::Status;
2
3pub trait RequestMessage: Send + Sync {
4    fn compute_size(&self) -> u64;
5
6    fn compute_size_with_tag_and_len(&self) -> u64 {
7        let size = self.compute_size();
8        let new_size = protobuf::rt::uint32_size(1, size as u32);
9        new_size + size
10    }
11
12    fn serial_with_tag_and_len(&self, os: &mut protobuf::CodedOutputStream<'_>) {
13        os.write_uint32(1, self.compute_size() as u32).unwrap();
14        self.serial_with_output_stream(os).unwrap();
15    }
16
17    fn serial(&self) -> std::result::Result<Vec<u8>, String> {
18        let size = self.compute_size() as usize;
19        let mut v = Vec::with_capacity(size);
20        let mut os: protobuf::CodedOutputStream<'_> = protobuf::CodedOutputStream::vec(&mut v);
21        self.serial_with_output_stream(&mut os).unwrap();
22        os.flush().unwrap();
23        drop(os);
24        Ok(v)
25    }
26
27    fn serial_with_output_stream(
28        &self,
29        os: &mut protobuf::CodedOutputStream<'_>,
30    ) -> std::result::Result<(), Status>;
31
32    //
33    fn parse_from_bytes(&mut self, bytes: &[u8]) -> std::result::Result<(), Status> {
34        let mut is = protobuf::CodedInputStream::from_bytes(bytes);
35        self.parse_from_input_stream(&mut is)?;
36        is.check_eof().unwrap();
37        Ok(())
38    }
39
40    fn parse_from_input_stream_with_tag_and_len(
41        &mut self,
42        is: &mut protobuf::CodedInputStream<'_>,
43    ) {
44        let _tag = is.read_raw_tag_or_eof().unwrap();
45        let size = is.read_uint32().unwrap();
46
47        let _old_limit = is.push_limit(size as u64).unwrap();
48
49        self.parse_from_input_stream(is).unwrap();
50
51        is.pop_limit(_old_limit);
52    }
53
54    fn parse_from_bytes_return_num(&mut self, bytes: &[u8]) -> std::result::Result<u64, Status> {
55        let mut is = protobuf::CodedInputStream::from_bytes(bytes);
56
57        let tag = is.read_raw_tag_or_eof().unwrap();
58        let size = is.read_uint32().unwrap();
59
60        let _old_limit = is.push_limit(size as u64).unwrap();
61
62        self.parse_from_input_stream(&mut is)?;
63
64        Ok(is.pos())
65    }
66
67    fn parse_from_input_stream(
68        &mut self,
69        is: &mut protobuf::CodedInputStream<'_>,
70    ) -> std::result::Result<(), Status>;
71}