Skip to content

Commit 2286b0c

Browse files
rw1nklerlpawelcz
authored andcommitted
modules/zstd: Add Buffer use-case example
This commit adds a simple test that shows, how one can use the Buffer struct inside a Proc. Internal-tag: [#50221] Signed-off-by: Robert Winkler <[email protected]>
1 parent bed3f1d commit 2286b0c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

xls/modules/zstd/buffer.x

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,66 @@ pub fn buffer_new<CAPACITY: u32>() -> Buffer<CAPACITY> {
359359
fn test_buffer_new() {
360360
assert_eq(buffer_new<u32:32>(), Buffer { content: u32:0, length: u32:0 });
361361
}
362+
363+
// Example use case
364+
//
365+
// The Buffer structure is meant to aggregate data received from the Proc's
366+
// channels. This data may be collected in multiple evaluations of the next
367+
// functions. Once the required amount of data is collected, it can be poped-out
368+
// in chanks of any length. A simple example that shows how the Buffer structure
369+
// can be used is presented below. It uses the structure to combine several
370+
// smaller transactions into bigger ones.
371+
372+
proc BufferExampleUsage {
373+
input_r: chan<u32> in;
374+
output_s: chan<u48> out;
375+
376+
config(
377+
input_r: chan<u32> in,
378+
output_s: chan<u48> out
379+
) { (input_r, output_s) }
380+
381+
init { buffer_new<u32:64>() }
382+
383+
next(tok: token, buffer: Buffer<u32:64>) {
384+
let (tok, recv_data) = recv(tok, input_r);
385+
let buffer = buffer_append<u32:64>(buffer, recv_data);
386+
387+
if buffer.length >= u32:48 {
388+
let (buffer, data_to_send) = buffer_fixed_pop<u32:64, u32:48>(buffer);
389+
let tok = send(tok, output_s, data_to_send);
390+
buffer
391+
} else {
392+
buffer
393+
}
394+
}
395+
}
396+
397+
#[test_proc]
398+
proc BufferExampleUsageTest {
399+
terminator: chan<bool> out;
400+
data32_s: chan<u32> out;
401+
data48_r: chan<u48> in;
402+
403+
config(terminator: chan<bool> out) {
404+
let (data32_s, data32_r) = chan<u32>;
405+
let (data48_s, data48_r) = chan<u48>;
406+
spawn BufferExampleUsage(data32_r, data48_s);
407+
(terminator, data32_s, data48_r)
408+
}
409+
410+
init {}
411+
412+
next(tok: token, state: ()) {
413+
let tok = send(tok, data32_s, u32:0xDEADBEEF);
414+
let tok = send(tok, data32_s, u32:0xBEEFCAFE);
415+
let tok = send(tok, data32_s, u32:0xCAFEDEAD);
416+
417+
let (tok, received_data) = recv(tok, data48_r);
418+
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF);
419+
let (tok, received_data) = recv(tok, data48_r);
420+
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF);
421+
422+
send(tok, terminator, true);
423+
}
424+
}

0 commit comments

Comments
 (0)