From 9cfa368c48dc10a65e926d196ed5f900c623aea9 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Wed, 18 Sep 2024 12:39:09 +0200 Subject: [PATCH] fix(tests): address possible short `leftpad` write Signed-off-by: Roman Volosatovs --- tests/components/wasi/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/components/wasi/src/lib.rs b/tests/components/wasi/src/lib.rs index bcda695..5a32ee8 100644 --- a/tests/components/wasi/src/lib.rs +++ b/tests/components/wasi/src/lib.rs @@ -39,21 +39,20 @@ impl bindings::exports::west_test::leftpad::leftpad::Guest for Handler { let rx: InputStream = in_.into_inner(); let tx: &OutputStream = out.get(); - let mut cs = zip(0..len, iter::repeat(c)).map(|(_, c)| c); + let mut cs = zip(0..len, iter::repeat(c)).flat_map(|(_, c)| String::from(c).into_bytes()); + let mut buf = Vec::default(); loop { let mut n = tx.check_write()?; if n == 0 { tx.subscribe().block(); n = tx.check_write()?; } - let s: Box = cs - .by_ref() - .take(n.try_into().unwrap_or(usize::MAX) / c.len_utf8()) - .collect(); - if s.is_empty() { + buf.extend(cs.by_ref().take(n.try_into().unwrap_or(usize::MAX))); + if buf.is_empty() { break; } - tx.write(s.as_bytes())?; + tx.write(&buf)?; + buf.clear(); } loop { let n = tx.splice(&rx, 4096)?;