Skip to content

Commit 28ff406

Browse files
committed
Do not pass buffer around as a Vec
We want to hide the actual buffer implementation from most of these locations. Currently it is a TransmitBuf but it likely will become something else in the future. So make this generic.
1 parent 6f32168 commit 28ff406

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

quinn-proto/src/connection/datagrams.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::VecDeque;
22

3-
use bytes::Bytes;
3+
use bytes::{BufMut, Bytes};
44
use thiserror::Error;
55
use tracing::{debug, trace};
66

7-
use super::Connection;
7+
use super::{BufLen, Connection};
88
use crate::{
99
TransportError,
1010
frame::{Datagram, FrameStruct},
@@ -163,7 +163,7 @@ impl DatagramState {
163163
///
164164
/// Returns whether a frame was written. At most `max_size` bytes will be written, including
165165
/// framing.
166-
pub(super) fn write(&mut self, buf: &mut Vec<u8>, max_size: usize) -> bool {
166+
pub(super) fn write(&mut self, buf: &mut (impl BufMut + BufLen), max_size: usize) -> bool {
167167
let datagram = match self.outgoing.pop_front() {
168168
Some(x) => x,
169169
None => return false,

quinn-proto/src/connection/mod.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
sync::Arc,
88
};
99

10-
use bytes::{Bytes, BytesMut};
10+
use bytes::{BufMut, Bytes, BytesMut};
1111
use frame::StreamMetaVec;
1212
use rand::{Rng, SeedableRng, rngs::StdRng};
1313
use thiserror::Error;
@@ -757,7 +757,7 @@ impl Connection {
757757
self.receiving_ecn,
758758
&mut SentFrames::default(),
759759
&mut self.spaces[space_id],
760-
buf.buf,
760+
&mut buf,
761761
&mut self.stats,
762762
);
763763
}
@@ -844,7 +844,7 @@ impl Connection {
844844
let sent = self.populate_packet(
845845
now,
846846
space_id,
847-
buf.buf,
847+
&mut buf,
848848
builder.max_size,
849849
builder.exact_number,
850850
);
@@ -3022,7 +3022,7 @@ impl Connection {
30223022
&mut self,
30233023
now: Instant,
30243024
space_id: SpaceId,
3025-
buf: &mut Vec<u8>,
3025+
buf: &mut (impl BufMut + BufLen),
30263026
max_size: usize,
30273027
pn: u64,
30283028
) -> SentFrames {
@@ -3288,7 +3288,7 @@ impl Connection {
32883288
receiving_ecn: bool,
32893289
sent: &mut SentFrames,
32903290
space: &mut PacketSpace,
3291-
buf: &mut Vec<u8>,
3291+
buf: &mut impl BufMut,
32923292
stats: &mut ConnectionStats,
32933293
) {
32943294
debug_assert!(!space.pending_acks.ranges().is_empty());
@@ -3958,6 +3958,23 @@ fn negotiate_max_idle_timeout(x: Option<VarInt>, y: Option<VarInt>) -> Option<Du
39583958
}
39593959
}
39603960

3961+
/// A buffer that can tell how much has been written to it already
3962+
///
3963+
/// This is commonly used for when a buffer is passed and the user may not write past a
3964+
/// given size. It allows the user of such a buffer to know the current cursor position in
3965+
/// the buffer. The maximum write size is usually passed in the same unit as
3966+
/// [`BufLen::len`]: bytes since the buffer start.
3967+
pub(crate) trait BufLen {
3968+
/// Returns the number of bytes written into the buffer so far
3969+
fn len(&self) -> usize;
3970+
}
3971+
3972+
impl BufLen for Vec<u8> {
3973+
fn len(&self) -> usize {
3974+
self.len()
3975+
}
3976+
}
3977+
39613978
#[cfg(test)]
39623979
mod tests {
39633980
use super::*;

quinn-proto/src/connection/streams/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::{
1515
use crate::{
1616
Dir, MAX_STREAM_COUNT, Side, StreamId, TransportError, VarInt,
1717
coding::BufMutExt,
18-
connection::stats::FrameStats,
18+
connection::{BufLen, stats::FrameStats},
1919
frame::{self, FrameStruct, StreamMetaVec},
2020
transport_parameters::TransportParameters,
2121
};
@@ -411,7 +411,7 @@ impl StreamsState {
411411

412412
pub(in crate::connection) fn write_control_frames(
413413
&mut self,
414-
buf: &mut Vec<u8>,
414+
buf: &mut (impl BufMut + BufLen),
415415
pending: &mut Retransmits,
416416
retransmits: &mut ThinRetransmits,
417417
stats: &mut FrameStats,
@@ -541,7 +541,7 @@ impl StreamsState {
541541

542542
pub(crate) fn write_stream_frames(
543543
&mut self,
544-
buf: &mut Vec<u8>,
544+
buf: &mut (impl BufMut + BufLen),
545545
max_buf_size: usize,
546546
fair: bool,
547547
) -> StreamMetaVec {

quinn-proto/src/connection/transmit_buf.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use bytes::BufMut;
22

3+
use super::BufLen;
4+
35
/// The buffer in which to write datagrams for [`Connection::poll_transmit`]
46
///
57
/// The `poll_transmit` function writes zero or more datagrams to a buffer. Multiple
@@ -200,3 +202,9 @@ unsafe impl BufMut for TransmitBuf<'_> {
200202
self.buf.chunk_mut()
201203
}
202204
}
205+
206+
impl BufLen for TransmitBuf<'_> {
207+
fn len(&self) -> usize {
208+
self.len()
209+
}
210+
}

quinn-proto/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,13 +899,13 @@ impl FrameStruct for Datagram {
899899
}
900900

901901
impl Datagram {
902-
pub(crate) fn encode(&self, length: bool, out: &mut Vec<u8>) {
902+
pub(crate) fn encode(&self, length: bool, out: &mut impl BufMut) {
903903
out.write(FrameType(*DATAGRAM_TYS.start() | u64::from(length))); // 1 byte
904904
if length {
905905
// Safe to unwrap because we check length sanity before queueing datagrams
906906
out.write(VarInt::from_u64(self.data.len() as u64).unwrap()); // <= 8 bytes
907907
}
908-
out.extend_from_slice(&self.data);
908+
out.put_slice(&self.data);
909909
}
910910

911911
pub(crate) fn size(&self, length: bool) -> usize {

quinn-proto/src/packet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use thiserror::Error;
66
use crate::{
77
ConnectionId,
88
coding::{self, BufExt, BufMutExt},
9+
connection::BufLen,
910
crypto,
1011
};
1112

@@ -281,7 +282,7 @@ pub(crate) enum Header {
281282
}
282283

283284
impl Header {
284-
pub(crate) fn encode(&self, w: &mut Vec<u8>) -> PartialEncode {
285+
pub(crate) fn encode(&self, w: &mut (impl BufMut + BufLen)) -> PartialEncode {
285286
use Header::*;
286287
let start = w.len();
287288
match *self {

0 commit comments

Comments
 (0)