use crate::__private::wasip1;
use crate::__private::wasip1::Size;
#[allow(unused_imports)]
use crate::memory::WasmAccess;
use crate::transporter::Wasip1Transporter;
#[cfg(all(not(feature = "multi_memory"), feature = "alloc"))]
use crate::memory::WasmAccessDynCompatible;
#[cfg(not(feature = "multi_memory"))]
use crate::memory::WasmAccessDynCompatibleRaw;
#[derive(Debug)]
pub struct DefaultStdIO;
#[cfg(any(feature = "embedded-fs", feature = "dynamic-fs"))]
impl StdIO for DefaultStdIO {
fn read(buf: &mut [u8]) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::read_from_stdin(buf)
}
#[cfg(not(feature = "multi_memory"))]
fn read_direct<Wasm: WasmAccess>(buf: *mut u8, len: usize) -> Result<Size, wasip1::Errno> {
use crate::transporter::Wasip1Transporter;
Wasip1Transporter::read_from_stdin_direct::<Wasm>(buf, len)
}
#[cfg(not(feature = "multi_memory"))]
fn read_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *mut u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
use crate::transporter::Wasip1Transporter;
Wasip1Transporter::read_from_stdin_direct_dyn_compatible(access, buf, len)
}
fn write(buf: &[u8]) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stdout(buf)
}
#[cfg(not(feature = "multi_memory"))]
fn write_direct<Wasm: WasmAccess>(buf: *const u8, len: usize) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stdout_direct::<Wasm>(buf, len)
}
#[cfg(not(feature = "multi_memory"))]
fn write_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *const u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stdout_direct_dyn_compatible(access, buf, len)
}
fn ewrite(buf: &[u8]) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stderr(buf)
}
#[cfg(not(feature = "multi_memory"))]
fn ewrite_direct<Wasm: WasmAccess>(buf: *const u8, len: usize) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stderr_direct::<Wasm>(buf, len)
}
#[cfg(not(feature = "multi_memory"))]
fn ewrite_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *const u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
Wasip1Transporter::write_to_stderr_direct_dyn_compatible(access, buf, len)
}
}
#[cfg(any(feature = "embedded-fs", feature = "dynamic-fs"))]
pub trait StdIO: core::fmt::Debug {
#[allow(unused_variables)]
fn read(buf: &mut [u8]) -> Result<Size, wasip1::Errno> {
Err(wasip1::ERRNO_NOSYS)
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn read_direct<Wasm: WasmAccess>(buf: *mut u8, len: usize) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
use crate::utils::alloc_buff;
let (_, size) = unsafe {
alloc_buff(len, |b| {
let size = Self::read(b)?;
Wasm::memcpy(buf, &b[..size]);
Ok(size)
})
};
size
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn read_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *mut u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
use crate::utils::alloc_buff;
let (_, size) = unsafe {
alloc_buff(len, |b| {
let size = Self::read(b)?;
access.memcpy_with(buf, &b[..size]);
Ok(size)
})
};
size
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
#[allow(unused_variables)]
fn write(buf: &[u8]) -> Result<Size, wasip1::Errno> {
Err(wasip1::ERRNO_NOSYS)
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn write_direct<Wasm: WasmAccess>(buf: *const u8, len: usize) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
Self::write(&Wasm::get_array(buf, len))
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn write_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *const u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
Self::write(&access.get_array_with(buf, len))
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
#[allow(unused_variables)]
fn ewrite(buf: &[u8]) -> Result<Size, wasip1::Errno> {
Err(wasip1::ERRNO_NOSYS)
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn ewrite_direct<Wasm: WasmAccess>(buf: *const u8, len: usize) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
Self::ewrite(&Wasm::get_array(buf, len))
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
#[cfg(not(feature = "multi_memory"))]
#[allow(unused_variables)]
fn ewrite_direct_dyn_compatible(
access: &dyn WasmAccessDynCompatibleRaw,
buf: *const u8,
len: usize,
) -> Result<Size, wasip1::Errno> {
#[cfg(feature = "alloc")]
{
Self::ewrite(&access.get_array_with(buf, len))
}
#[cfg(not(feature = "alloc"))]
{
Err(wasip1::ERRNO_NOSYS)
}
}
}