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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::frame::{util, Error, Frame, Head, Kind, StreamId};
use bytes::{Buf, BufMut, Bytes};
use std::fmt;
#[derive(Eq, PartialEq)]
pub struct Data<T = Bytes> {
stream_id: StreamId,
data: T,
flags: DataFlags,
pad_len: Option<u8>,
}
#[derive(Copy, Clone, Default, Eq, PartialEq)]
struct DataFlags(u8);
const END_STREAM: u8 = 0x1;
const PADDED: u8 = 0x8;
const ALL: u8 = END_STREAM | PADDED;
impl<T> Data<T> {
pub fn new(stream_id: StreamId, payload: T) -> Self {
assert!(!stream_id.is_zero());
Data {
stream_id,
data: payload,
flags: DataFlags::default(),
pad_len: None,
}
}
pub fn stream_id(&self) -> StreamId {
self.stream_id
}
pub fn is_end_stream(&self) -> bool {
self.flags.is_end_stream()
}
pub fn set_end_stream(&mut self, val: bool) {
if val {
self.flags.set_end_stream();
} else {
self.flags.unset_end_stream();
}
}
#[cfg(feature = "unstable")]
pub fn is_padded(&self) -> bool {
self.flags.is_padded()
}
#[cfg(feature = "unstable")]
pub fn set_padded(&mut self) {
self.flags.set_padded();
}
pub fn payload(&self) -> &T {
&self.data
}
pub fn payload_mut(&mut self) -> &mut T {
&mut self.data
}
pub fn into_payload(self) -> T {
self.data
}
pub(crate) fn head(&self) -> Head {
Head::new(Kind::Data, self.flags.into(), self.stream_id)
}
pub(crate) fn map<F, U>(self, f: F) -> Data<U>
where
F: FnOnce(T) -> U,
{
Data {
stream_id: self.stream_id,
data: f(self.data),
flags: self.flags,
pad_len: self.pad_len,
}
}
}
impl Data<Bytes> {
pub(crate) fn load(head: Head, mut payload: Bytes) -> Result<Self, Error> {
let flags = DataFlags::load(head.flag());
if head.stream_id().is_zero() {
return Err(Error::InvalidStreamId);
}
let pad_len = if flags.is_padded() {
let len = util::strip_padding(&mut payload)?;
Some(len)
} else {
None
};
Ok(Data {
stream_id: head.stream_id(),
data: payload,
flags,
pad_len,
})
}
}
impl<T: Buf> Data<T> {
pub(crate) fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) {
let len = self.data.remaining() as usize;
assert!(dst.remaining_mut() >= len);
self.head().encode(len, dst);
dst.put(&mut self.data);
}
}
impl<T> From<Data<T>> for Frame<T> {
fn from(src: Data<T>) -> Self {
Frame::Data(src)
}
}
impl<T> fmt::Debug for Data<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut f = fmt.debug_struct("Data");
f.field("stream_id", &self.stream_id);
if !self.flags.is_empty() {
f.field("flags", &self.flags);
}
if let Some(ref pad_len) = self.pad_len {
f.field("pad_len", pad_len);
}
f.finish()
}
}
impl DataFlags {
fn load(bits: u8) -> DataFlags {
DataFlags(bits & ALL)
}
fn is_empty(&self) -> bool {
self.0 == 0
}
fn is_end_stream(&self) -> bool {
self.0 & END_STREAM == END_STREAM
}
fn set_end_stream(&mut self) {
self.0 |= END_STREAM
}
fn unset_end_stream(&mut self) {
self.0 &= !END_STREAM
}
fn is_padded(&self) -> bool {
self.0 & PADDED == PADDED
}
#[cfg(feature = "unstable")]
fn set_padded(&mut self) {
self.0 |= PADDED
}
}
impl From<DataFlags> for u8 {
fn from(src: DataFlags) -> u8 {
src.0
}
}
impl fmt::Debug for DataFlags {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
util::debug_flags(fmt, self.0)
.flag_if(self.is_end_stream(), "END_STREAM")
.flag_if(self.is_padded(), "PADDED")
.finish()
}
}