about summary refs log tree commit diff
path: root/tvix/nix-compat/src/wire/bytes/reader/trailer.rs
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2024-04-29T13·57+0000
committeredef <edef@edef.eu>2024-04-29T15·33+0000
commit44bd9543a6d23afd9b735bd8149341ea071019c2 (patch)
treeb6016202187948fdcff6a71be56177e6c503ef76 /tvix/nix-compat/src/wire/bytes/reader/trailer.rs
parent5070f9eff73e2c8f781aceb34df980dbfe929cc7 (diff)
feat(nix-compat/wire/bytes/reader): expose the remaining data length r/8029
The API is a bit odd here, because we don't have a distinct type for a
known-length reader.

Change-Id: I4a1dd07fbed0a400004dbe4aa2095c51898ad3bd
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11538
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: Brian Olsen <me@griff.name>
Diffstat (limited to 'tvix/nix-compat/src/wire/bytes/reader/trailer.rs')
-rw-r--r--tvix/nix-compat/src/wire/bytes/reader/trailer.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/wire/bytes/reader/trailer.rs b/tvix/nix-compat/src/wire/bytes/reader/trailer.rs
index 958cead42d8f..61a77678080a 100644
--- a/tvix/nix-compat/src/wire/bytes/reader/trailer.rs
+++ b/tvix/nix-compat/src/wire/bytes/reader/trailer.rs
@@ -81,6 +81,12 @@ pub(crate) fn read_trailer<R: AsyncRead + Unpin, T: Tag>(
     }
 }
 
+impl<R, T: Tag> ReadTrailer<R, T> {
+    pub fn len(&self) -> u8 {
+        self.data_len
+    }
+}
+
 impl<R: AsyncRead + Unpin, T: Tag> Future for ReadTrailer<R, T> {
     type Output = io::Result<Trailer>;
 
@@ -141,6 +147,17 @@ impl<R: AsyncRead + Unpin> TrailerReader<R> {
     pub fn new(reader: R, data_len: u8) -> Self {
         Self::Reading(read_trailer(reader, data_len))
     }
+
+    pub fn len(&self) -> u8 {
+        match self {
+            TrailerReader::Reading(fut) => fut.len(),
+            &TrailerReader::Releasing {
+                off,
+                data: Trailer { data_len, .. },
+            } => data_len - off,
+            TrailerReader::Done => 0,
+        }
+    }
 }
 
 impl<R: AsyncRead + Unpin> AsyncRead for TrailerReader<R> {