From 48c2b8e067ef27b512f7e7a882eed563a1c0d626 Mon Sep 17 00:00:00 2001 From: "Brian L. Troutwine" Date: Tue, 15 Oct 2024 10:51:45 -0700 Subject: [PATCH] Allow `Cache` to return a single block In some cases it is valuable for the block cache to be used without `spin`. We expose `next_block` to return a `Block` reference as soon as one is available, blocking the callers meanwhile. Signed-off-by: Brian L. Troutwine --- lading_payload/src/block.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lading_payload/src/block.rs b/lading_payload/src/block.rs index fd1783bff..59605d519 100644 --- a/lading_payload/src/block.rs +++ b/lading_payload/src/block.rs @@ -342,6 +342,23 @@ impl Cache { }, } } + + /// Return a `Block` from the `Cache` + /// + /// This is a blocking function that returns a single `Block` instance as + /// soon as one is ready, blocking the caller until one is available. + pub fn next_block(&mut self) -> &Block { + match self { + Self::Fixed { + ref mut idx, + blocks, + } => { + let block = &blocks[*idx]; + *idx = (*idx + 1) % blocks.len(); + block + } + } + } } /// Construct a new block cache of form defined by `serializer`.