From d14b16ebf324691079f3005055284a74005c53b3 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Wed, 22 May 2024 16:30:09 -0700 Subject: [PATCH 1/2] Make ConnectionIdParser object-safe, and accept trait object impls --- quinn-proto/src/packet.rs | 8 ++++---- quinn-proto/src/shared.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/quinn-proto/src/packet.rs b/quinn-proto/src/packet.rs index 3e5bf0fd0..be132a74f 100644 --- a/quinn-proto/src/packet.rs +++ b/quinn-proto/src/packet.rs @@ -32,7 +32,7 @@ impl PartialDecode { /// Begin decoding a QUIC packet from `bytes`, returning any trailing data not part of that packet pub fn new( bytes: BytesMut, - cid_parser: &impl ConnectionIdParser, + cid_parser: &(impl ConnectionIdParser + ?Sized), supported_versions: &[u32], grease_quic_bit: bool, ) -> Result<(Self, Option), PacketDecodeError> { @@ -564,7 +564,7 @@ impl ProtectedHeader { /// Decode a plain header from given buffer, with given [`ConnectionIdParser`]. pub fn decode( buf: &mut io::Cursor, - cid_parser: &impl ConnectionIdParser, + cid_parser: &(impl ConnectionIdParser + ?Sized), supported_versions: &[u32], grease_quic_bit: bool, ) -> Result { @@ -780,7 +780,7 @@ impl FixedLengthConnectionIdParser { } impl ConnectionIdParser for FixedLengthConnectionIdParser { - fn parse(&self, buffer: &mut impl Buf) -> Result { + fn parse(&self, buffer: &mut dyn Buf) -> Result { (buffer.remaining() >= self.expected_len) .then(|| ConnectionId::from_buf(buffer, self.expected_len)) .ok_or(PacketDecodeError::InvalidHeader("packet too small")) @@ -790,7 +790,7 @@ impl ConnectionIdParser for FixedLengthConnectionIdParser { /// Parse connection id in short header packet pub trait ConnectionIdParser { /// Parse a connection id from given buffer - fn parse(&self, buf: &mut impl Buf) -> Result; + fn parse(&self, buf: &mut dyn Buf) -> Result; } /// Long packet type including non-uniform cases diff --git a/quinn-proto/src/shared.rs b/quinn-proto/src/shared.rs index d4e5029c7..05ffe8caf 100644 --- a/quinn-proto/src/shared.rs +++ b/quinn-proto/src/shared.rs @@ -86,7 +86,7 @@ impl ConnectionId { /// Constructs cid by reading `len` bytes from a `Buf` /// /// Callers need to assure that `buf.remaining() >= len` - pub fn from_buf(buf: &mut impl Buf, len: usize) -> Self { + pub fn from_buf(buf: &mut (impl Buf + ?Sized), len: usize) -> Self { debug_assert!(len <= MAX_CID_SIZE); let mut res = Self { len: len as u8, From 60b17509941d77f54d619ba5b5f77276e65f6b62 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Wed, 22 May 2024 16:51:48 -0700 Subject: [PATCH 2/2] Fix double indirect call in default CID generator factory A lambda expression is a unique ZST, but coercing it to a function pointer discards that information. --- quinn-proto/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quinn-proto/src/config.rs b/quinn-proto/src/config.rs index c51557a33..6d53fd590 100644 --- a/quinn-proto/src/config.rs +++ b/quinn-proto/src/config.rs @@ -630,8 +630,8 @@ pub struct EndpointConfig { impl EndpointConfig { /// Create a default config with a particular `reset_key` pub fn new(reset_key: Arc) -> Self { - let cid_factory: fn() -> Box = - || Box::::default(); + let cid_factory = + || -> Box { Box::::default() }; Self { reset_key, max_udp_payload_size: (1500u32 - 28).into(), // Ethernet MTU minus IP + UDP headers