/src/h2/src/frame/stream_id.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /// A stream identifier, as described in [Section 5.1.1] of RFC 7540. |
2 | | /// |
3 | | /// Streams are identified with an unsigned 31-bit integer. Streams |
4 | | /// initiated by a client MUST use odd-numbered stream identifiers; those |
5 | | /// initiated by the server MUST use even-numbered stream identifiers. A |
6 | | /// stream identifier of zero (0x0) is used for connection control |
7 | | /// messages; the stream identifier of zero cannot be used to establish a |
8 | | /// new stream. |
9 | | /// |
10 | | /// [Section 5.1.1]: https://tools.ietf.org/html/rfc7540#section-5.1.1 |
11 | | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
12 | | pub struct StreamId(u32); |
13 | | |
14 | | #[derive(Debug, Copy, Clone)] |
15 | | pub struct StreamIdOverflow; |
16 | | |
17 | | const STREAM_ID_MASK: u32 = 1 << 31; |
18 | | |
19 | | impl StreamId { |
20 | | /// Stream ID 0. |
21 | | pub const ZERO: StreamId = StreamId(0); |
22 | | |
23 | | /// The maximum allowed stream ID. |
24 | | pub const MAX: StreamId = StreamId(u32::MAX >> 1); |
25 | | |
26 | | /// Parse the stream ID |
27 | | #[inline] |
28 | 137k | pub fn parse(buf: &[u8]) -> (StreamId, bool) { |
29 | 137k | let mut ubuf = [0; 4]; |
30 | 137k | ubuf.copy_from_slice(&buf[0..4]); |
31 | 137k | let unpacked = u32::from_be_bytes(ubuf); |
32 | 137k | let flag = unpacked & STREAM_ID_MASK == STREAM_ID_MASK; |
33 | 137k | |
34 | 137k | // Now clear the most significant bit, as that is reserved and MUST be |
35 | 137k | // ignored when received. |
36 | 137k | (StreamId(unpacked & !STREAM_ID_MASK), flag) |
37 | 137k | } |
38 | | |
39 | | /// Returns true if this stream ID corresponds to a stream that |
40 | | /// was initiated by the client. |
41 | 0 | pub fn is_client_initiated(&self) -> bool { |
42 | 0 | let id = self.0; |
43 | 0 | id != 0 && id % 2 == 1 |
44 | 0 | } |
45 | | |
46 | | /// Returns true if this stream ID corresponds to a stream that |
47 | | /// was initiated by the server. |
48 | 655k | pub fn is_server_initiated(&self) -> bool { |
49 | 655k | let id = self.0; |
50 | 655k | id != 0 && id % 2 == 0 |
51 | 655k | } |
52 | | |
53 | | /// Return a new `StreamId` for stream 0. |
54 | | #[inline] |
55 | 27.4k | pub fn zero() -> StreamId { |
56 | 27.4k | StreamId::ZERO |
57 | 27.4k | } <h2::frame::stream_id::StreamId>::zero Line | Count | Source | 55 | 20.0k | pub fn zero() -> StreamId { | 56 | 20.0k | StreamId::ZERO | 57 | 20.0k | } |
Unexecuted instantiation: <h2::frame::stream_id::StreamId>::zero <h2::frame::stream_id::StreamId>::zero Line | Count | Source | 55 | 7.35k | pub fn zero() -> StreamId { | 56 | 7.35k | StreamId::ZERO | 57 | 7.35k | } |
|
58 | | |
59 | | /// Returns true if this stream ID is zero. |
60 | 1.19M | pub fn is_zero(&self) -> bool { |
61 | 1.19M | self.0 == 0 |
62 | 1.19M | } |
63 | | |
64 | | /// Returns the next stream ID initiated by the same peer as this stream |
65 | | /// ID, or an error if incrementing this stream ID would overflow the |
66 | | /// maximum. |
67 | 431k | pub fn next_id(&self) -> Result<StreamId, StreamIdOverflow> { |
68 | 431k | let next = self.0 + 2; |
69 | 431k | if next > StreamId::MAX.0 { |
70 | 251 | Err(StreamIdOverflow) |
71 | | } else { |
72 | 431k | Ok(StreamId(next)) |
73 | | } |
74 | 431k | } |
75 | | } |
76 | | |
77 | | impl From<u32> for StreamId { |
78 | 25.4k | fn from(src: u32) -> Self { |
79 | 25.4k | assert_eq!(src & STREAM_ID_MASK, 0, "invalid stream ID -- MSB is set"); |
80 | 25.4k | StreamId(src) |
81 | 25.4k | } |
82 | | } |
83 | | |
84 | | impl From<StreamId> for u32 { |
85 | 240k | fn from(src: StreamId) -> Self { |
86 | 240k | src.0 |
87 | 240k | } |
88 | | } |
89 | | |
90 | | impl PartialEq<u32> for StreamId { |
91 | 214 | fn eq(&self, other: &u32) -> bool { |
92 | 214 | self.0 == *other |
93 | 214 | } |
94 | | } |