* Allow any type which implements Handle to act as stdio There have been requests to allow more than just raw OS handles to act as stdio in `wasi-common`. This commit makes this possible by loosening the requirement of the `WasiCtxBuilder` to accept any type `T: Handle + 'static` to act as any of the stdio handles. A couple words about correctness of this approach. Currently, since we only have a single `Handle` super-trait to represent all possible WASI handle types (files, dirs, stdio, pipes, virtual, etc.), it is possible to pass in any type to act as stdio which can be wrong. However, I envision this being a problem only in the near(est) future until we work out how to split `Handle` into several traits, each representing a different type of WASI resource. In this particular case, this would be a resource which would implement the interface required for a handle to act as a stdio (with appropriate rights, etc.). * Use OsFile in c-api * Add some documention to the types exposed by this PR, and a few others Signed-off-by: Jakub Konka <kubkon@jakubkonka.com> * Add construction examples and missing docs for Handle trait * Fix example on Windows * Merge wasi_preview_builder into create_preview1_instance Co-authored-by: Pat Hickey <pat@moreproductive.org>
57 lines
1.4 KiB
Rust
57 lines
1.4 KiB
Rust
use crate::sys::AsFile;
|
|
use std::cell::Cell;
|
|
use std::fs::File;
|
|
use std::io;
|
|
use std::mem::ManuallyDrop;
|
|
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
|
|
|
|
#[derive(Debug)]
|
|
pub struct RawOsHandle(Cell<RawHandle>);
|
|
|
|
impl RawOsHandle {
|
|
/// Tries cloning `self`.
|
|
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
|
let handle = self.as_file()?.try_clone()?;
|
|
Ok(Self(Cell::new(handle.into_raw_handle())))
|
|
}
|
|
/// Consumes `other` taking the ownership of the underlying
|
|
/// `RawHandle` file handle.
|
|
pub(crate) fn update_from(&self, other: Self) {
|
|
let new_handle = other.into_raw_handle();
|
|
let old_handle = self.0.get();
|
|
self.0.set(new_handle);
|
|
// We need to remember to close the old_handle.
|
|
unsafe {
|
|
File::from_raw_handle(old_handle);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for RawOsHandle {
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
File::from_raw_handle(self.as_raw_handle());
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AsRawHandle for RawOsHandle {
|
|
fn as_raw_handle(&self) -> RawHandle {
|
|
self.0.get()
|
|
}
|
|
}
|
|
|
|
impl FromRawHandle for RawOsHandle {
|
|
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
|
|
Self(Cell::new(handle))
|
|
}
|
|
}
|
|
|
|
impl IntoRawHandle for RawOsHandle {
|
|
fn into_raw_handle(self) -> RawHandle {
|
|
// We need to prevent dropping of the OsFile
|
|
let wrapped = ManuallyDrop::new(self);
|
|
wrapped.0.get()
|
|
}
|
|
}
|