about summary refs log tree commit diff
path: root/tvix/nix-compat/src/wire/bytes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/nix-compat/src/wire/bytes.rs')
-rw-r--r--tvix/nix-compat/src/wire/bytes.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/tvix/nix-compat/src/wire/bytes.rs b/tvix/nix-compat/src/wire/bytes.rs
index 1a25daca8b4f..070f2368fdc3 100644
--- a/tvix/nix-compat/src/wire/bytes.rs
+++ b/tvix/nix-compat/src/wire/bytes.rs
@@ -1,4 +1,7 @@
-use std::ops::RangeBounds;
+use std::{
+    io::{Error, ErrorKind},
+    ops::RangeBounds,
+};
 
 use tokio::io::{AsyncReadExt, AsyncWriteExt};
 
@@ -58,6 +61,21 @@ where
     Ok(buf)
 }
 
+/// Read a Nix daemon string from the AsyncWrite, encoded as utf8.
+/// Rejects reading more than `allowed_size` bytes
+///
+/// A Nix daemon string is made up of two distincts parts:
+/// 1. Its lenght, LE-encoded on 64 bits.
+/// 2. Its content. 0-padded on 64 bits.
+pub async fn read_string<R, S>(r: &mut R, allowed_size: S) -> std::io::Result<String>
+where
+    R: AsyncReadExt + Unpin,
+    S: RangeBounds<u64>,
+{
+    let bytes = read_bytes(r, allowed_size).await?;
+    String::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidData, e))
+}
+
 /// Writes a sequence of sized bits to a (hopefully buffered)
 /// [AsyncWriteExt] handle.
 ///