Path: blob/main/crates/wasi/src/p2/host/io.rs
3082 views
use crate::p2::{1StreamError, StreamResult,2bindings::sync::io::poll::Pollable,3bindings::sync::io::streams::{self, InputStream, OutputStream},4};5use crate::runtime::in_tokio;6use wasmtime::component::{Resource, ResourceTable};7use wasmtime_wasi_io::bindings::wasi::io::streams::{8self as async_streams, Host as AsyncHost, HostInputStream as AsyncHostInputStream,9HostOutputStream as AsyncHostOutputStream,10};1112impl From<async_streams::StreamError> for streams::StreamError {13fn from(other: async_streams::StreamError) -> Self {14match other {15async_streams::StreamError::LastOperationFailed(e) => Self::LastOperationFailed(e),16async_streams::StreamError::Closed => Self::Closed,17}18}19}2021impl streams::Host for ResourceTable {22fn convert_stream_error(&mut self, err: StreamError) -> wasmtime::Result<streams::StreamError> {23Ok(AsyncHost::convert_stream_error(self, err)?.into())24}25}2627impl streams::HostOutputStream for ResourceTable {28fn drop(&mut self, stream: Resource<OutputStream>) -> wasmtime::Result<()> {29in_tokio(async { AsyncHostOutputStream::drop(self, stream).await })30}3132fn check_write(&mut self, stream: Resource<OutputStream>) -> StreamResult<u64> {33Ok(AsyncHostOutputStream::check_write(self, stream)?)34}3536fn write(&mut self, stream: Resource<OutputStream>, bytes: Vec<u8>) -> StreamResult<()> {37Ok(AsyncHostOutputStream::write(self, stream, bytes)?)38}3940fn blocking_write_and_flush(41&mut self,42stream: Resource<OutputStream>,43bytes: Vec<u8>,44) -> StreamResult<()> {45in_tokio(async {46AsyncHostOutputStream::blocking_write_and_flush(self, stream, bytes).await47})48}4950fn blocking_write_zeroes_and_flush(51&mut self,52stream: Resource<OutputStream>,53len: u64,54) -> StreamResult<()> {55in_tokio(async {56AsyncHostOutputStream::blocking_write_zeroes_and_flush(self, stream, len).await57})58}5960fn subscribe(61&mut self,62stream: Resource<OutputStream>,63) -> wasmtime::Result<Resource<Pollable>> {64Ok(AsyncHostOutputStream::subscribe(self, stream)?)65}6667fn write_zeroes(&mut self, stream: Resource<OutputStream>, len: u64) -> StreamResult<()> {68Ok(AsyncHostOutputStream::write_zeroes(self, stream, len)?)69}7071fn flush(&mut self, stream: Resource<OutputStream>) -> StreamResult<()> {72Ok(AsyncHostOutputStream::flush(73self,74Resource::new_borrow(stream.rep()),75)?)76}7778fn blocking_flush(&mut self, stream: Resource<OutputStream>) -> StreamResult<()> {79in_tokio(async {80AsyncHostOutputStream::blocking_flush(self, Resource::new_borrow(stream.rep())).await81})82}8384fn splice(85&mut self,86dst: Resource<OutputStream>,87src: Resource<InputStream>,88len: u64,89) -> StreamResult<u64> {90AsyncHostOutputStream::splice(self, dst, src, len)91}9293fn blocking_splice(94&mut self,95dst: Resource<OutputStream>,96src: Resource<InputStream>,97len: u64,98) -> StreamResult<u64> {99in_tokio(async { AsyncHostOutputStream::blocking_splice(self, dst, src, len).await })100}101}102103impl streams::HostInputStream for ResourceTable {104fn drop(&mut self, stream: Resource<InputStream>) -> wasmtime::Result<()> {105in_tokio(async { AsyncHostInputStream::drop(self, stream).await })106}107108fn read(&mut self, stream: Resource<InputStream>, len: u64) -> StreamResult<Vec<u8>> {109AsyncHostInputStream::read(self, stream, len)110}111112fn blocking_read(&mut self, stream: Resource<InputStream>, len: u64) -> StreamResult<Vec<u8>> {113in_tokio(async { AsyncHostInputStream::blocking_read(self, stream, len).await })114}115116fn skip(&mut self, stream: Resource<InputStream>, len: u64) -> StreamResult<u64> {117AsyncHostInputStream::skip(self, stream, len)118}119120fn blocking_skip(&mut self, stream: Resource<InputStream>, len: u64) -> StreamResult<u64> {121in_tokio(async { AsyncHostInputStream::blocking_skip(self, stream, len).await })122}123124fn subscribe(&mut self, stream: Resource<InputStream>) -> wasmtime::Result<Resource<Pollable>> {125AsyncHostInputStream::subscribe(self, stream)126}127}128129130