Skip to content

Commit bb0ebb7

Browse files
authored
feat(async): impl Read and Write for Direct{Destination,Source} (#12175)
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent 87ed3b6 commit bb0ebb7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ pub struct DirectDestination<'a, D: 'static> {
336336
store: StoreContextMut<'a, D>,
337337
}
338338

339+
impl<D: 'static> std::io::Write for DirectDestination<'_, D> {
340+
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
341+
let rem = self.remaining();
342+
let n = rem.len().min(buf.len());
343+
rem[..n].copy_from_slice(&buf[..n]);
344+
self.mark_written(n);
345+
Ok(n)
346+
}
347+
348+
fn flush(&mut self) -> std::io::Result<()> {
349+
Ok(())
350+
}
351+
}
352+
339353
impl<D: 'static> DirectDestination<'_, D> {
340354
/// Provide direct access to the writer's buffer.
341355
pub fn remaining(&mut self) -> &mut [u8] {
@@ -836,6 +850,16 @@ pub struct DirectSource<'a, D: 'static> {
836850
store: StoreContextMut<'a, D>,
837851
}
838852

853+
impl<D: 'static> std::io::Read for DirectSource<'_, D> {
854+
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
855+
let rem = self.remaining();
856+
let n = rem.len().min(buf.len());
857+
buf[..n].copy_from_slice(&rem[..n]);
858+
self.mark_read(n);
859+
Ok(n)
860+
}
861+
}
862+
839863
impl<D: 'static> DirectSource<'_, D> {
840864
/// Provide direct access to the writer's buffer.
841865
pub fn remaining(&mut self) -> &[u8] {

0 commit comments

Comments
 (0)