diff options
author | Florian Klink <flokli@flokli.de> | 2024-04-07T21·22+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-04-08T06·18+0000 |
commit | eb910dfa3abbfa162fb6140b4cebf1ccfe04819c (patch) | |
tree | 49e9628e27075c11ec6a189119a7ab688e3f03d2 /tvix/nix-compat/src/wire | |
parent | f6359bf91f5b398264973efd3015114fb296024f (diff) |
docs(tvix/nix-compat/wire): update docstrings r/7871
Only describe the format once (in `read_bytes`, and simplify the other docstrings a bit. Change-Id: Iff898f3c4173d506a357bc14bdffbf69c4c6e0e0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11374 Tested-by: BuildkiteCI Reviewed-by: picnoir picnoir <picnoir@alternativebit.fr> Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/wire')
-rw-r--r-- | tvix/nix-compat/src/wire/bytes.rs | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/tvix/nix-compat/src/wire/bytes.rs b/tvix/nix-compat/src/wire/bytes.rs index f7fb0b9c6eb0..e2f34ead6b79 100644 --- a/tvix/nix-compat/src/wire/bytes.rs +++ b/tvix/nix-compat/src/wire/bytes.rs @@ -8,12 +8,23 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use super::primitive; #[allow(dead_code)] -/// Read a limited number of bytes from the AsyncRead. +/// Read a "bytes wire packet" from the AsyncRead. /// Rejects reading more than `allowed_size` bytes of payload. -/// Internally takes care of dealing with the padding, so the returned `Vec<u8>` -/// only contains the payload. -/// This always buffers the entire contents into memory, we'll add a streaming -/// version later. +/// +/// The packet is made up of three parts: +/// - a length header, u64, LE-encoded +/// - the payload itself +/// - null bytes to the next 8 byte boundary +/// +/// Ensures the payload size fits into the `allowed_size` passed, +/// and that the padding is actual null bytes. +/// +/// On success, the returned `Vec<u8>` only contains the payload itself. +/// On failure (for example if a too large byte packet was sent), the reader +/// becomes unusable. +/// +/// This buffers the entire payload into memory, a streaming version will be +/// added later. pub async fn read_bytes<R, S>(r: &mut R, allowed_size: S) -> std::io::Result<Vec<u8>> where R: AsyncReadExt + Unpin, @@ -61,12 +72,9 @@ 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. +/// Read a "bytes wire packet" of from the AsyncRead and tries to parse as string. +/// Internally uses [read_bytes]. +/// Rejects reading more than `allowed_size` bytes of payload. pub async fn read_string<R, S>(r: &mut R, allowed_size: S) -> std::io::Result<String> where R: AsyncReadExt + Unpin, @@ -76,15 +84,9 @@ where String::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidData, e)) } -/// Writes a sequence of sized bits to a (hopefully buffered) -/// [AsyncWriteExt] handle. -/// -/// On the wire, it looks as follows: +/// Writes a "bytes wire packet" to a (hopefully buffered) [AsyncWriteExt]. /// -/// 1. Number of bytes contained in the buffer we're about to write on -/// the wire. (LE-encoded on 64 bits) -/// 2. Raw payload. -/// 3. Null padding up until the next 8 bytes alignment block. +/// See [read_bytes] for a description of the format. /// /// Note: if performance matters to you, make sure your /// [AsyncWriteExt] handle is buffered. This function is quite |