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
use super::*;
use bytes::{Buf, BufMut, BytesMut};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CancelFrame {
stream_id: u32,
}
impl CancelFrame {
pub fn new(stream_id: u32) -> Self {
debug_assert_max_u31!(stream_id);
let stream_id = stream_id & MAX_U31;
CancelFrame { stream_id }
}
pub fn stream_id(&self) -> u32 {
self.stream_id
}
}
impl Encode for CancelFrame {
fn encode(&self, buf: &mut BytesMut) {
buf.put_u32(self.stream_id);
buf.put_u16(FrameType::CANCEL.bits());
}
fn len(&self) -> usize {
6
}
}
impl Decode for CancelFrame {
type Value = Self;
fn decode<B: Buf>(
_buf: &mut B,
stream_id: u32,
_flags: Flags,
) -> Result<Self::Value> {
Ok(CancelFrame { stream_id })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_codec() {
let lease = CancelFrame::new(1);
let mut buf = BytesMut::new();
lease.encode(&mut buf);
let mut buf = buf.freeze();
let buf_len = buf.len();
assert_eq!(buf_len, 4 + 2);
let stream_id = eat_stream_id(&mut buf).unwrap();
let (frame_type, flags) = eat_flags(&mut buf).unwrap();
assert_eq!(frame_type, FrameType::CANCEL);
assert_eq!(flags, Flags::empty());
let decoded = CancelFrame::decode(&mut buf, stream_id, flags).unwrap();
assert_eq!(decoded, lease);
assert_eq!(lease.len(), buf_len);
assert_eq!(decoded.len(), buf_len);
}
}