From 652413c97dd1614f67ec81f00ec8aa433fbac7eb Mon Sep 17 00:00:00 2001 From: edef Date: Thu, 25 Apr 2024 22:29:04 +0000 Subject: refactor(nix-compat/wire/bytes): don't parametrise on RangeBounds This is semantically a RangeInclusive, since we can only have 0..=u64::MAX at most, and monomorphising on the bounds doesn't buy us anything. Change-Id: Ib601d7fd77d703d6c8c5ec27ac9e67bb122ce1c0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11515 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/wire/bytes/reader.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'tvix/nix-compat/src/wire/bytes') diff --git a/tvix/nix-compat/src/wire/bytes/reader.rs b/tvix/nix-compat/src/wire/bytes/reader.rs index 9aea677645e7..9e8bd8704bbe 100644 --- a/tvix/nix-compat/src/wire/bytes/reader.rs +++ b/tvix/nix-compat/src/wire/bytes/reader.rs @@ -1,6 +1,6 @@ use pin_project_lite::pin_project; use std::{ - ops::RangeBounds, + ops::{Bound, RangeBounds, RangeInclusive}, task::{ready, Poll}, }; use tokio::io::AsyncRead; @@ -29,31 +29,40 @@ pin_project! { /// In case of an error due to size constraints, or in case of not reading /// all the way to the end (and getting a EOF), the underlying reader is no /// longer usable and might return garbage. - pub struct BytesReader + pub struct BytesReader where - R: AsyncRead, - S: RangeBounds, - + R: AsyncRead { #[pin] inner: R, - allowed_size: S, + allowed_size: RangeInclusive, payload_size: [u8; 8], state: BytesPacketPosition, } } -impl BytesReader +impl BytesReader where R: AsyncRead + Unpin, - S: RangeBounds, { /// Constructs a new BytesReader, using the underlying passed reader. - pub fn new(r: R, allowed_size: S) -> Self { + pub fn new>(r: R, allowed_size: S) -> Self { + let user_len_min = match allowed_size.start_bound() { + Bound::Included(&n) => n, + Bound::Excluded(&n) => n.saturating_add(1), + Bound::Unbounded => 0, + }; + + let user_len_max = match allowed_size.end_bound() { + Bound::Included(&n) => n, + Bound::Excluded(&n) => n.checked_sub(1).unwrap(), + Bound::Unbounded => u64::MAX, + }; + Self { inner: r, - allowed_size, + allowed_size: user_len_min..=user_len_max, payload_size: [0; 8], state: BytesPacketPosition::Size(0), } @@ -72,10 +81,9 @@ fn ensure_nonzero_bytes_read(bytes_read: usize) -> Result } } -impl AsyncRead for BytesReader +impl AsyncRead for BytesReader where R: AsyncRead, - S: RangeBounds, { fn poll_read( self: std::pin::Pin<&mut Self>, -- cgit 1.4.1