From 4749964f06a7aa20ee19c5f7b3c97079e5c67911 Mon Sep 17 00:00:00 2001 From: edef Date: Fri, 9 May 2025 22:31:45 +0000 Subject: [PATCH] refactor(nix-daemon/framed): simplify partial header read Rather than having separate branches, just make it part of the state machine discipline. Change-Id: Ib21456227515506495ca06ac2a8a529d04f95fde Reviewed-on: https://cl.snix.dev/c/snix/+/30496 Reviewed-by: Brian Olsen Tested-by: besadii Reviewed-by: Florian Klink --- .../src/nix_daemon/framing/framed_read.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/snix/nix-compat/src/nix_daemon/framing/framed_read.rs b/snix/nix-compat/src/nix_daemon/framing/framed_read.rs index 333924e07..62e584e6d 100644 --- a/snix/nix-compat/src/nix_daemon/framing/framed_read.rs +++ b/snix/nix-compat/src/nix_daemon/framing/framed_read.rs @@ -70,6 +70,12 @@ impl AsyncRead for NixFramedReader { State::Eof => { return Ok(()).into(); } + State::Length { buf, filled: 8 } => { + *this.state = match NonZeroU64::new(u64::from_le_bytes(*buf)) { + None => State::Eof, + Some(remaining) => State::Chunk { remaining }, + }; + } State::Length { buf, filled } => { let bytes_read = { let mut b = ReadBuf::new(&mut buf[*filled as usize..]); @@ -82,13 +88,6 @@ impl AsyncRead for NixFramedReader { } *filled += bytes_read; - - if *filled == 8 { - *this.state = match NonZeroU64::new(u64::from_le_bytes(*buf)) { - None => State::Eof, - Some(remaining) => State::Chunk { remaining }, - }; - } } State::Chunk { remaining } => { let bytes_read = ready!(with_limited(buf, remaining.get(), |buf| {