rwebtransport 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,64 +1,64 @@
1
- // Copyright (C) 2018-2019, Cloudflare, Inc.
2
- // All rights reserved.
3
- //
4
- // Redistribution and use in source and binary forms, with or without
5
- // modification, are permitted provided that the following conditions are
6
- // met:
7
- //
8
- // * Redistributions of source code must retain the above copyright notice,
9
- // this list of conditions and the following disclaimer.
10
- //
11
- // * Redistributions in binary form must reproduce the above copyright
12
- // notice, this list of conditions and the following disclaimer in the
13
- // documentation and/or other materials provided with the distribution.
14
- //
15
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
- // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
- // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
- // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
- // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
- // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
- // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
- // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
- // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
-
27
- pub fn rand_bytes(buf: &mut [u8]) {
28
- unsafe {
29
- RAND_bytes(buf.as_mut_ptr(), buf.len());
30
- }
31
- }
32
-
33
- pub fn rand_u8() -> u8 {
34
- let mut buf = [0; 1];
35
-
36
- rand_bytes(&mut buf);
37
-
38
- buf[0]
39
- }
40
-
41
- pub fn rand_u64() -> u64 {
42
- let mut buf = [0; 8];
43
-
44
- rand_bytes(&mut buf);
45
-
46
- u64::from_ne_bytes(buf)
47
- }
48
-
49
- pub fn rand_u64_uniform(max: u64) -> u64 {
50
- let chunk_size = u64::MAX / max;
51
- let end_of_last_chunk = chunk_size * max;
52
-
53
- let mut r = rand_u64();
54
-
55
- while r >= end_of_last_chunk {
56
- r = rand_u64();
57
- }
58
-
59
- r / chunk_size
60
- }
61
-
62
- extern "C" {
63
- fn RAND_bytes(buf: *mut u8, len: libc::size_t) -> libc::c_int;
64
- }
1
+ // Copyright (C) 2018-2019, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ pub fn rand_bytes(buf: &mut [u8]) {
28
+ unsafe {
29
+ RAND_bytes(buf.as_mut_ptr(), buf.len());
30
+ }
31
+ }
32
+
33
+ pub fn rand_u8() -> u8 {
34
+ let mut buf = [0; 1];
35
+
36
+ rand_bytes(&mut buf);
37
+
38
+ buf[0]
39
+ }
40
+
41
+ pub fn rand_u64() -> u64 {
42
+ let mut buf = [0; 8];
43
+
44
+ rand_bytes(&mut buf);
45
+
46
+ u64::from_ne_bytes(buf)
47
+ }
48
+
49
+ pub fn rand_u64_uniform(max: u64) -> u64 {
50
+ let chunk_size = u64::MAX / max;
51
+ let end_of_last_chunk = chunk_size * max;
52
+
53
+ let mut r = rand_u64();
54
+
55
+ while r >= end_of_last_chunk {
56
+ r = rand_u64();
57
+ }
58
+
59
+ r / chunk_size
60
+ }
61
+
62
+ extern "C" {
63
+ fn RAND_bytes(buf: *mut u8, len: libc::size_t) -> libc::c_int;
64
+ }
@@ -1,186 +1,186 @@
1
- // Copyright (C) 2025, Cloudflare, Inc.
2
- // All rights reserved.
3
- //
4
- // Redistribution and use in source and binary forms, with or without
5
- // modification, are permitted provided that the following conditions are
6
- // met:
7
- //
8
- // * Redistributions of source code must retain the above copyright notice,
9
- // this list of conditions and the following disclaimer.
10
- //
11
- // * Redistributions in binary form must reproduce the above copyright
12
- // notice, this list of conditions and the following disclaimer in the
13
- // documentation and/or other materials provided with the distribution.
14
- //
15
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
- // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
- // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
- // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
- // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
- // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
- // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
- // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
- // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
-
27
- use std::cmp;
28
- use std::marker::PhantomData;
29
- use std::ops::Deref;
30
-
31
- use crate::buffers::BufFactory;
32
- use crate::buffers::DefaultBufFactory;
33
-
34
- /// Buffer holding data at a specific offset.
35
- ///
36
- /// The data is stored in a `Vec<u8>` in such a way that it can be shared
37
- /// between multiple `RangeBuf` objects.
38
- ///
39
- /// Each `RangeBuf` will have its own view of that buffer, where the `start`
40
- /// value indicates the initial offset within the `Vec`, and `len` indicates the
41
- /// number of bytes, starting from `start` that are included.
42
- ///
43
- /// In addition, `pos` indicates the current offset within the `Vec`, starting
44
- /// from the very beginning of the `Vec`.
45
- ///
46
- /// Finally, `off` is the starting offset for the specific `RangeBuf` within the
47
- /// stream the buffer belongs to.
48
- #[derive(Clone, Debug, Default)]
49
- pub struct RangeBuf<F = DefaultBufFactory>
50
- where
51
- F: BufFactory,
52
- {
53
- /// The internal buffer holding the data.
54
- ///
55
- /// To avoid needless allocations when a RangeBuf is split, this field
56
- /// should be reference-counted so it can be shared between multiple
57
- /// RangeBuf objects, and sliced using the `start` and `len` values.
58
- pub(crate) data: F::Buf,
59
-
60
- /// The initial offset within the internal buffer.
61
- pub(crate) start: usize,
62
-
63
- /// The current offset within the internal buffer.
64
- pub(crate) pos: usize,
65
-
66
- /// The number of bytes in the buffer, from the initial offset.
67
- pub(crate) len: usize,
68
-
69
- /// The offset of the buffer within a stream.
70
- pub(crate) off: u64,
71
-
72
- /// Whether this contains the final byte in the stream.
73
- pub(crate) fin: bool,
74
-
75
- _bf: PhantomData<F>,
76
- }
77
-
78
- impl<F: BufFactory> RangeBuf<F>
79
- where
80
- F::Buf: Clone,
81
- {
82
- /// Creates a new `RangeBuf` from the given slice.
83
- pub fn from(buf: &[u8], off: u64, fin: bool) -> RangeBuf<F> {
84
- Self::from_raw(F::buf_from_slice(buf), off, fin)
85
- }
86
-
87
- pub fn from_raw(data: F::Buf, off: u64, fin: bool) -> RangeBuf<F> {
88
- RangeBuf {
89
- len: data.as_ref().len(),
90
- data,
91
- start: 0,
92
- pos: 0,
93
- off,
94
- fin,
95
- _bf: Default::default(),
96
- }
97
- }
98
-
99
- /// Returns whether `self` holds the final offset in the stream.
100
- pub fn fin(&self) -> bool {
101
- self.fin
102
- }
103
-
104
- /// Returns the starting offset of `self`.
105
- pub fn off(&self) -> u64 {
106
- (self.off - self.start as u64) + self.pos as u64
107
- }
108
-
109
- /// Returns the final offset of `self`.
110
- pub fn max_off(&self) -> u64 {
111
- self.off() + self.len() as u64
112
- }
113
-
114
- /// Returns the length of `self`.
115
- pub fn len(&self) -> usize {
116
- self.len - (self.pos - self.start)
117
- }
118
-
119
- /// Returns true if `self` has a length of zero bytes.
120
- pub fn is_empty(&self) -> bool {
121
- self.len() == 0
122
- }
123
-
124
- /// Consumes the starting `count` bytes of `self`.
125
- pub fn consume(&mut self, count: usize) {
126
- self.pos += count;
127
- }
128
-
129
- /// Splits the buffer into two at the given index.
130
- pub fn split_off(&mut self, at: usize) -> RangeBuf<F>
131
- where
132
- F::Buf: Clone + AsRef<[u8]>,
133
- {
134
- assert!(
135
- at <= self.len,
136
- "`at` split index (is {}) should be <= len (is {})",
137
- at,
138
- self.len
139
- );
140
-
141
- let buf = RangeBuf {
142
- data: self.data.clone(),
143
- start: self.start + at,
144
- pos: cmp::max(self.pos, self.start + at),
145
- len: self.len - at,
146
- off: self.off + at as u64,
147
- _bf: Default::default(),
148
- fin: self.fin,
149
- };
150
-
151
- self.pos = cmp::min(self.pos, self.start + at);
152
- self.len = at;
153
- self.fin = false;
154
-
155
- buf
156
- }
157
- }
158
-
159
- impl<F: BufFactory> Deref for RangeBuf<F> {
160
- type Target = [u8];
161
-
162
- fn deref(&self) -> &[u8] {
163
- &self.data.as_ref()[self.pos..self.start + self.len]
164
- }
165
- }
166
-
167
- impl<F: BufFactory> Ord for RangeBuf<F> {
168
- fn cmp(&self, other: &RangeBuf<F>) -> cmp::Ordering {
169
- // Invert ordering to implement min-heap.
170
- self.off.cmp(&other.off).reverse()
171
- }
172
- }
173
-
174
- impl<F: BufFactory> PartialOrd for RangeBuf<F> {
175
- fn partial_cmp(&self, other: &RangeBuf<F>) -> Option<cmp::Ordering> {
176
- Some(self.cmp(other))
177
- }
178
- }
179
-
180
- impl<F: BufFactory> Eq for RangeBuf<F> {}
181
-
182
- impl<F: BufFactory> PartialEq for RangeBuf<F> {
183
- fn eq(&self, other: &RangeBuf<F>) -> bool {
184
- self.off == other.off
185
- }
186
- }
1
+ // Copyright (C) 2025, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ use std::cmp;
28
+ use std::marker::PhantomData;
29
+ use std::ops::Deref;
30
+
31
+ use crate::buffers::BufFactory;
32
+ use crate::buffers::DefaultBufFactory;
33
+
34
+ /// Buffer holding data at a specific offset.
35
+ ///
36
+ /// The data is stored in a `Vec<u8>` in such a way that it can be shared
37
+ /// between multiple `RangeBuf` objects.
38
+ ///
39
+ /// Each `RangeBuf` will have its own view of that buffer, where the `start`
40
+ /// value indicates the initial offset within the `Vec`, and `len` indicates the
41
+ /// number of bytes, starting from `start` that are included.
42
+ ///
43
+ /// In addition, `pos` indicates the current offset within the `Vec`, starting
44
+ /// from the very beginning of the `Vec`.
45
+ ///
46
+ /// Finally, `off` is the starting offset for the specific `RangeBuf` within the
47
+ /// stream the buffer belongs to.
48
+ #[derive(Clone, Debug, Default)]
49
+ pub struct RangeBuf<F = DefaultBufFactory>
50
+ where
51
+ F: BufFactory,
52
+ {
53
+ /// The internal buffer holding the data.
54
+ ///
55
+ /// To avoid needless allocations when a RangeBuf is split, this field
56
+ /// should be reference-counted so it can be shared between multiple
57
+ /// RangeBuf objects, and sliced using the `start` and `len` values.
58
+ pub(crate) data: F::Buf,
59
+
60
+ /// The initial offset within the internal buffer.
61
+ pub(crate) start: usize,
62
+
63
+ /// The current offset within the internal buffer.
64
+ pub(crate) pos: usize,
65
+
66
+ /// The number of bytes in the buffer, from the initial offset.
67
+ pub(crate) len: usize,
68
+
69
+ /// The offset of the buffer within a stream.
70
+ pub(crate) off: u64,
71
+
72
+ /// Whether this contains the final byte in the stream.
73
+ pub(crate) fin: bool,
74
+
75
+ _bf: PhantomData<F>,
76
+ }
77
+
78
+ impl<F: BufFactory> RangeBuf<F>
79
+ where
80
+ F::Buf: Clone,
81
+ {
82
+ /// Creates a new `RangeBuf` from the given slice.
83
+ pub fn from(buf: &[u8], off: u64, fin: bool) -> RangeBuf<F> {
84
+ Self::from_raw(F::buf_from_slice(buf), off, fin)
85
+ }
86
+
87
+ pub fn from_raw(data: F::Buf, off: u64, fin: bool) -> RangeBuf<F> {
88
+ RangeBuf {
89
+ len: data.as_ref().len(),
90
+ data,
91
+ start: 0,
92
+ pos: 0,
93
+ off,
94
+ fin,
95
+ _bf: Default::default(),
96
+ }
97
+ }
98
+
99
+ /// Returns whether `self` holds the final offset in the stream.
100
+ pub fn fin(&self) -> bool {
101
+ self.fin
102
+ }
103
+
104
+ /// Returns the starting offset of `self`.
105
+ pub fn off(&self) -> u64 {
106
+ (self.off - self.start as u64) + self.pos as u64
107
+ }
108
+
109
+ /// Returns the final offset of `self`.
110
+ pub fn max_off(&self) -> u64 {
111
+ self.off() + self.len() as u64
112
+ }
113
+
114
+ /// Returns the length of `self`.
115
+ pub fn len(&self) -> usize {
116
+ self.len - (self.pos - self.start)
117
+ }
118
+
119
+ /// Returns true if `self` has a length of zero bytes.
120
+ pub fn is_empty(&self) -> bool {
121
+ self.len() == 0
122
+ }
123
+
124
+ /// Consumes the starting `count` bytes of `self`.
125
+ pub fn consume(&mut self, count: usize) {
126
+ self.pos += count;
127
+ }
128
+
129
+ /// Splits the buffer into two at the given index.
130
+ pub fn split_off(&mut self, at: usize) -> RangeBuf<F>
131
+ where
132
+ F::Buf: Clone + AsRef<[u8]>,
133
+ {
134
+ assert!(
135
+ at <= self.len,
136
+ "`at` split index (is {}) should be <= len (is {})",
137
+ at,
138
+ self.len
139
+ );
140
+
141
+ let buf = RangeBuf {
142
+ data: self.data.clone(),
143
+ start: self.start + at,
144
+ pos: cmp::max(self.pos, self.start + at),
145
+ len: self.len - at,
146
+ off: self.off + at as u64,
147
+ _bf: Default::default(),
148
+ fin: self.fin,
149
+ };
150
+
151
+ self.pos = cmp::min(self.pos, self.start + at);
152
+ self.len = at;
153
+ self.fin = false;
154
+
155
+ buf
156
+ }
157
+ }
158
+
159
+ impl<F: BufFactory> Deref for RangeBuf<F> {
160
+ type Target = [u8];
161
+
162
+ fn deref(&self) -> &[u8] {
163
+ &self.data.as_ref()[self.pos..self.start + self.len]
164
+ }
165
+ }
166
+
167
+ impl<F: BufFactory> Ord for RangeBuf<F> {
168
+ fn cmp(&self, other: &RangeBuf<F>) -> cmp::Ordering {
169
+ // Invert ordering to implement min-heap.
170
+ self.off.cmp(&other.off).reverse()
171
+ }
172
+ }
173
+
174
+ impl<F: BufFactory> PartialOrd for RangeBuf<F> {
175
+ fn partial_cmp(&self, other: &RangeBuf<F>) -> Option<cmp::Ordering> {
176
+ Some(self.cmp(other))
177
+ }
178
+ }
179
+
180
+ impl<F: BufFactory> Eq for RangeBuf<F> {}
181
+
182
+ impl<F: BufFactory> PartialEq for RangeBuf<F> {
183
+ fn eq(&self, other: &RangeBuf<F>) -> bool {
184
+ self.off == other.off
185
+ }
186
+ }