From f6359bf91f5b398264973efd3015114fb296024f Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 8 Apr 2024 00:14:39 +0300 Subject: refactor(tvix/nix-compat): drop read_bytes_unchecked This isn't used anywhere so far, and if someone really wants to, they can simply make the range open-ended. Change-Id: Iae9bcaa1f7ea032dd3ee76c8c142a38b6b72894d Reviewed-on: https://cl.tvl.fyi/c/depot/+/11373 Reviewed-by: picnoir picnoir Tested-by: BuildkiteCI --- tvix/nix-compat/src/wire/bytes.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/tvix/nix-compat/src/wire/bytes.rs b/tvix/nix-compat/src/wire/bytes.rs index 070f2368fd..f7fb0b9c6e 100644 --- a/tvix/nix-compat/src/wire/bytes.rs +++ b/tvix/nix-compat/src/wire/bytes.rs @@ -102,15 +102,6 @@ pub async fn write_bytes(w: &mut W, b: &[u8]) -> std:: Ok(()) } -#[allow(dead_code)] -/// Read an unlimited number of bytes from the AsyncRead. -/// Note this can exhaust memory. -/// Internally uses [read_bytes], which takes care of dealing with the padding, -/// so the returned `Vec` only contains the payload. -pub async fn read_bytes_unchecked(r: &mut R) -> std::io::Result> { - read_bytes(r, 0u64..).await -} - /// Computes the number of bytes we should add to len (a length in /// bytes) to be alined on 64 bits (8 bytes). pub(crate) fn padding_len(len: u64) -> u8 { @@ -129,8 +120,12 @@ mod tests { use super::*; use hex_literal::hex; + /// The maximum length of bytes packets we're willing to accept in the test + /// cases. + const MAX_LEN: u64 = 1024; + #[tokio::test] - async fn test_read_8_bytes_unchecked() { + async fn test_read_8_bytes() { let mut mock = Builder::new() .read(&8u64.to_le_bytes()) .read(&12345678u64.to_le_bytes()) @@ -138,12 +133,15 @@ mod tests { assert_eq!( &12345678u64.to_le_bytes(), - read_bytes_unchecked(&mut mock).await.unwrap().as_slice() + read_bytes(&mut mock, 0u64..MAX_LEN) + .await + .unwrap() + .as_slice() ); } #[tokio::test] - async fn test_read_9_bytes_unchecked() { + async fn test_read_9_bytes() { let mut mock = Builder::new() .read(&9u64.to_le_bytes()) .read(&hex!("01020304050607080900000000000000")) @@ -151,19 +149,25 @@ mod tests { assert_eq!( hex!("010203040506070809"), - read_bytes_unchecked(&mut mock).await.unwrap().as_slice() + read_bytes(&mut mock, 0u64..MAX_LEN) + .await + .unwrap() + .as_slice() ); } #[tokio::test] - async fn test_read_0_bytes_unchecked() { + async fn test_read_0_bytes() { // A empty byte packet is essentially just the 0 length field. // No data is read, and there's zero padding. let mut mock = Builder::new().read(&0u64.to_le_bytes()).build(); assert_eq!( hex!(""), - read_bytes_unchecked(&mut mock).await.unwrap().as_slice() + read_bytes(&mut mock, 0u64..MAX_LEN) + .await + .unwrap() + .as_slice() ); } -- cgit 1.4.1