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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::fmt::{self, Debug, Formatter};
use serde::Serialize;
use rmps;
use hpack::RawHeader;
#[derive(Clone)]
pub struct Request {
ty: u64,
buf: Vec<u8>,
headers: Vec<RawHeader>,
}
impl Request {
#[inline]
pub fn new<S: Serialize>(ty: u64, args: &S) -> Result<Self, rmps::encode::Error> {
let buf = rmps::to_vec(args)?;
let request = Request::from_buf(ty, buf);
Ok(request)
}
#[inline]
pub fn ty(&self) -> u64 {
self.ty
}
#[inline]
pub fn data(&self) -> &[u8] {
&self.buf
}
#[inline]
pub fn headers(&self) -> &[RawHeader] {
&self.headers
}
pub fn add_header<H: Into<RawHeader>>(mut self, header: H) -> Self {
self.headers.push(header.into());
self
}
pub fn add_headers<H: IntoIterator<Item = RawHeader>>(mut self, headers: H) -> Self {
self.headers.extend(headers);
self
}
pub(crate) fn into_components(self) -> (Vec<u8>, Vec<RawHeader>) {
(self.buf, self.headers)
}
#[inline]
pub(crate) fn from_buf(ty: u64, buf: Vec<u8>) -> Self {
Self {
ty: ty,
buf: buf,
headers: Vec::new(),
}
}
}
impl Debug for Request {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
fmt.debug_struct("Request")
.field("ty", &self.ty)
.field("len", &self.buf.len())
.field("headers", &self.headers)
.finish()
}
}