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
use crate::base::status::Status;

pub trait RequestMessage: Send + Sync {
    fn compute_size(&self) -> u64;

    fn compute_size_with_tag_and_len(&self) -> u64 {
        let size = self.compute_size();
        let new_size = protobuf::rt::uint32_size(1, size as u32);
        new_size + size
    }

    fn serial_with_tag_and_len(&self, os: &mut protobuf::CodedOutputStream<'_>) {
        os.write_uint32(1, self.compute_size() as u32).unwrap();
        self.serial_with_output_stream(os).unwrap();
    }

    fn serial(&self) -> std::result::Result<Vec<u8>, String> {
        let size = self.compute_size() as usize;
        let mut v = Vec::with_capacity(size);
        let mut os: protobuf::CodedOutputStream<'_> = protobuf::CodedOutputStream::vec(&mut v);
        self.serial_with_output_stream(&mut os).unwrap();
        os.flush().unwrap();
        drop(os);
        Ok(v)
    }

    fn serial_with_output_stream(
        &self,
        os: &mut protobuf::CodedOutputStream<'_>,
    ) -> std::result::Result<(), Status>;

    //
    fn parse_from_bytes(&mut self, bytes: &[u8]) -> std::result::Result<(), Status> {
        let mut is = protobuf::CodedInputStream::from_bytes(bytes);
        self.parse_from_input_stream(&mut is)?;
        is.check_eof().unwrap();
        Ok(())
    }

    fn parse_from_input_stream_with_tag_and_len(
        &mut self,
        is: &mut protobuf::CodedInputStream<'_>,
    ) {
        let _tag = is.read_raw_tag_or_eof().unwrap();
        let size = is.read_uint32().unwrap();

        let _old_limit = is.push_limit(size as u64).unwrap();

        self.parse_from_input_stream(is).unwrap();

        is.pop_limit(_old_limit);
    }

    fn parse_from_bytes_return_num(&mut self, bytes: &[u8]) -> std::result::Result<u64, Status> {
        let mut is = protobuf::CodedInputStream::from_bytes(bytes);

        let tag = is.read_raw_tag_or_eof().unwrap();
        let size = is.read_uint32().unwrap();

        let _old_limit = is.push_limit(size as u64).unwrap();

        self.parse_from_input_stream(&mut is)?;

        Ok(is.pos())
    }

    fn parse_from_input_stream(
        &mut self,
        is: &mut protobuf::CodedInputStream<'_>,
    ) -> std::result::Result<(), Status>;
}