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 <brian@maven-group.org>
Tested-by: besadii
Reviewed-by: Florian Klink <flokli@flokli.de>
This commit is contained in:
edef 2025-05-09 22:31:45 +00:00
parent d5c5269ca4
commit 4749964f06

View file

@ -70,6 +70,12 @@ impl<R: AsyncRead> AsyncRead for NixFramedReader<R> {
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<R: AsyncRead> AsyncRead for NixFramedReader<R> {
}
*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| {