Skip to content

Commit 386f4af

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 4396f4a commit 386f4af

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;
@@ -755,7 +755,7 @@ impl Connection {
755755
self.receiving_ecn,
756756
&mut SentFrames::default(),
757757
&mut self.spaces[space_id],
758-
buf.buf,
758+
&mut buf,
759759
&mut self.stats,
760760
);
761761
}
@@ -842,7 +842,7 @@ impl Connection {
842842
let sent = self.populate_packet(
843843
now,
844844
space_id,
845-
buf.buf,
845+
&mut buf,
846846
builder.max_size,
847847
builder.exact_number,
848848
);
@@ -3020,7 +3020,7 @@ impl Connection {
30203020
&mut self,
30213021
now: Instant,
30223022
space_id: SpaceId,
3023-
buf: &mut Vec<u8>,
3023+
buf: &mut (impl BufMut + BufLen),
30243024
max_size: usize,
30253025
pn: u64,
30263026
) -> SentFrames {
@@ -3286,7 +3286,7 @@ impl Connection {
32863286
receiving_ecn: bool,
32873287
sent: &mut SentFrames,
32883288
space: &mut PacketSpace,
3289-
buf: &mut Vec<u8>,
3289+
buf: &mut impl BufMut,
32903290
stats: &mut ConnectionStats,
32913291
) {
32923292
debug_assert!(!space.pending_acks.ranges().is_empty());
@@ -3963,6 +3963,23 @@ fn negotiate_max_idle_timeout(x: Option<VarInt>, y: Option<VarInt>) -> Option<Du
39633963
}
39643964
}
39653965

3966+
/// A buffer that can tell how much has been written to it already
3967+
///
3968+
/// This is commonly used for when a buffer is passed and the user may not write past a
3969+
/// given size. It allows the user of such a buffer to know the current cursor position in
3970+
/// the buffer. The maximum write size is usually passed in the same unit as
3971+
/// [`BufLen::len`]: bytes since the buffer start.
3972+
pub(crate) trait BufLen {
3973+
/// Returns the number of bytes written into the buffer so far
3974+
fn len(&self) -> usize;
3975+
}
3976+
3977+
impl BufLen for Vec<u8> {
3978+
fn len(&self) -> usize {
3979+
self.len()
3980+
}
3981+
}
3982+
39663983
#[cfg(test)]
39673984
mod tests {
39683985
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<W: BufMut + BufLen>(&self, w: &mut W) -> PartialEncode {
285286
use Header::*;
286287
let start = w.len();
287288
match *self {

0 commit comments

Comments
 (0)