Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable API documentation for std::os::wasi. #82420

Merged
merged 6 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![stable(feature = "os", since = "1.0.0")]
#![allow(missing_docs, nonstandard_style, missing_debug_implementations)]

// When documenting libstd we want to show unix/windows/linux modules as these are the "main
// When documenting libstd we want to show unix/windows/linux/wasi modules as these are the "main
// modules" that are used across platforms, so all modules are enabled when `cfg(doc)` is set.
// This should help show platform-specific functionality in a hopefully cross-platform way in the
// documentation.
Expand All @@ -22,6 +22,9 @@ pub use crate::sys::windows_ext as windows;
#[doc(cfg(target_os = "linux"))]
pub mod linux;

#[cfg(doc)]
pub use crate::sys::wasi_ext as wasi;

// If we're not documenting libstd then we just expose the main modules as we otherwise would.

#[cfg(not(doc))]
Expand All @@ -38,6 +41,10 @@ pub use crate::sys::ext as windows;
#[cfg(any(target_os = "linux", target_os = "l4re"))]
pub mod linux;

#[cfg(not(doc))]
#[cfg(target_os = "wasi")]
pub mod wasi;

#[cfg(target_os = "android")]
pub mod android;
#[cfg(target_os = "dragonfly")]
Expand Down Expand Up @@ -68,7 +75,5 @@ pub mod redox;
pub mod solaris;
#[cfg(target_os = "vxworks")]
pub mod vxworks;
#[cfg(target_os = "wasi")]
pub mod wasi;

pub mod raw;
30 changes: 26 additions & 4 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ cfg_if::cfg_if! {
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::ext as unix_ext;
} else if #[cfg(any(target_os = "hermit",
target_arch = "wasm32",
all(target_arch = "wasm32", not(target_os = "wasi")),
all(target_vendor = "fortanix", target_env = "sgx")))] {
// On wasm right now the module below doesn't compile
// On non-WASI wasm right now the module below doesn't compile
// (missing things in `libc` which is empty) so just omit everything
// with an empty module
#[unstable(issue = "none", feature = "std_internals")]
Expand All @@ -85,9 +85,9 @@ cfg_if::cfg_if! {
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::ext as windows_ext;
} else if #[cfg(any(target_os = "hermit",
target_arch = "wasm32",
all(target_arch = "wasm32", not(target_os = "wasi")),
all(target_vendor = "fortanix", target_env = "sgx")))] {
// On wasm right now the shim below doesn't compile, so
// On non-WASI wasm right now the shim below doesn't compile, so
// just omit it
#[unstable(issue = "none", feature = "std_internals")]
#[allow(missing_docs)]
Expand All @@ -106,3 +106,25 @@ cfg_if::cfg_if! {
pub mod windows_ext;
}
}

#[cfg(doc)]
cfg_if::cfg_if! {
if #[cfg(target_os = "wasi")] {
// On WASI we'll document what's already available
#[stable(feature = "rust1", since = "1.0.0")]
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
pub use self::ext as wasi_ext;
} else if #[cfg(any(target_os = "hermit",
target_arch = "wasm32",
all(target_vendor = "fortanix", target_env = "sgx")))] {
// On non-WASI wasm right now the module below doesn't compile
// (missing things in `libc` which is empty) so just omit everything
// with an empty module
#[unstable(issue = "none", feature = "std_internals")]
#[allow(missing_docs)]
pub mod wasi_ext {}
} else {
// On other platforms like Windows document the bare bones of WASI
#[path = "wasi/ext/mod.rs"]
pub mod wasi_ext;
}
}
25 changes: 16 additions & 9 deletions library/std/src/sys/wasi/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![unstable(feature = "wasi_ext", issue = "none")]

use crate::ffi::OsStr;
use crate::fs::{self, File, Metadata, OpenOptions};
use crate::io::{self, IoSlice, IoSliceMut};
use crate::path::{Path, PathBuf};
use crate::sys::fs::osstr2str;
use crate::sys_common::{AsInner, AsInnerMut, FromInner};
// Used for `File::read` on intra-doc links
#[allow(unused_imports)]
use io::{Read, Write};

/// WASI-specific extensions to [`File`].
pub trait FileExt {
Expand Down Expand Up @@ -54,11 +57,11 @@ pub trait FileExt {
/// # Errors
///
/// If this function encounters an error of the kind
/// [`ErrorKind::Interrupted`] then the error is ignored and the operation
/// [`io::ErrorKind::Interrupted`] then the error is ignored and the operation
/// will continue.
///
/// If this function encounters an "end of file" before completely filling
/// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
/// the buffer, it returns an error of the kind [`io::ErrorKind::UnexpectedEof`].
/// The contents of `buf` are unspecified in this case.
///
/// If any other read error is encountered then this function immediately
Expand Down Expand Up @@ -131,16 +134,16 @@ pub trait FileExt {
/// The current file cursor is not affected by this function.
///
/// This method will continuously call [`write_at`] until there is no more data
/// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
/// to be written or an error of non-[`io::ErrorKind::Interrupted`] kind is
/// returned. This method will not return until the entire buffer has been
/// successfully written or such an error occurs. The first error that is
/// not of [`ErrorKind::Interrupted`] kind generated from this method will be
/// not of [`io::ErrorKind::Interrupted`] kind generated from this method will be
/// returned.
///
/// # Errors
///
/// This function will return the first error of
/// non-[`ErrorKind::Interrupted`] kind that [`write_at`] returns.
/// non-[`io::ErrorKind::Interrupted`] kind that [`write_at`] returns.
///
/// [`write_at`]: FileExt::write_at
#[stable(feature = "rw_exact_all_at", since = "1.33.0")]
Expand Down Expand Up @@ -426,7 +429,7 @@ impl MetadataExt for fs::Metadata {
}
}

/// WASI-specific extensions for [`FileType`].
/// WASI-specific extensions for [`fs::FileType`].
///
/// Adds support for special WASI file types such as block/character devices,
/// pipes, and sockets.
Expand Down Expand Up @@ -517,8 +520,12 @@ pub fn symlink<P: AsRef<Path>, U: AsRef<Path>>(

/// Create a symbolic link.
///
/// This is a convenience API similar to [`std::os::unix::fs::symlink`] and
/// [`std::os::windows::fs::symlink_file`] and [`symlink_dir`](std::os::windows::fs::symlink_dir).
/// This is a convenience API similar to `std::os::unix::fs::symlink` and
/// `std::os::windows::fs::symlink_file` and `std::os::windows::fs::symlink_dir`.
pub fn symlink_path<P: AsRef<Path>, U: AsRef<Path>>(old_path: P, new_path: U) -> io::Result<()> {
crate::sys::fs::symlink(old_path.as_ref(), new_path.as_ref())
}

fn osstr2str(f: &OsStr) -> io::Result<&str> {
f.to_str().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "input must be utf-8"))
}
12 changes: 6 additions & 6 deletions library/std/src/sys/wasi/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,36 @@ impl IntoRawFd for fs::File {

impl AsRawFd for io::Stdin {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stdin.as_raw_fd()
libc::STDIN_FILENO as RawFd
}
}

impl AsRawFd for io::Stdout {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stdout.as_raw_fd()
libc::STDOUT_FILENO as RawFd
}
}

impl AsRawFd for io::Stderr {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stderr.as_raw_fd()
libc::STDERR_FILENO as RawFd
}
}

impl<'a> AsRawFd for io::StdinLock<'a> {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stdin.as_raw_fd()
libc::STDIN_FILENO as RawFd
}
}

impl<'a> AsRawFd for io::StdoutLock<'a> {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stdout.as_raw_fd()
libc::STDOUT_FILENO as RawFd
}
}

impl<'a> AsRawFd for io::StderrLock<'a> {
fn as_raw_fd(&self) -> RawFd {
sys::stdio::Stderr.as_raw_fd()
libc::STDERR_FILENO as RawFd
}
}
36 changes: 32 additions & 4 deletions library/std/src/sys/wasi/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
//! Platform-specific extensions to `std` for WASI.
//!
//! Provides access to platform-level information on WASI, and exposes
//! WASI-specific functions that would otherwise be inappropriate as
//! part of the core `std` library.
//!
//! It exposes more ways to deal with platform-specific strings (`OsStr`,
//! `OsString`), allows to set permissions more granularly, extract low-level
//! file descriptors from files and sockets, and has platform-specific helpers
//! for spawning processes.
//!
//! # Examples
//!
//! ```no_run
//! use std::fs::File;
//! use std::os::wasi::prelude::*;
//!
//! fn main() -> std::io::Result<()> {
//! let f = File::create("foo.txt")?;
//! let fd = f.as_raw_fd();
//!
//! // use fd with native WASI bindings
//!
//! Ok(())
//! }
//! ```

#![deny(unsafe_op_in_unsafe_fn)]
#![doc(cfg(target_os = "wasi"))]

pub mod ffi;
pub mod fs;
Expand All @@ -11,14 +39,14 @@ pub mod io;
pub mod prelude {
#[doc(no_inline)]
#[stable(feature = "rust1", since = "1.0.0")]
pub use crate::sys::ext::ffi::{OsStrExt, OsStringExt};
pub use super::ffi::{OsStrExt, OsStringExt};
#[doc(no_inline)]
#[stable(feature = "rust1", since = "1.0.0")]
pub use crate::sys::ext::fs::FileTypeExt;
pub use super::fs::FileTypeExt;
#[doc(no_inline)]
#[stable(feature = "rust1", since = "1.0.0")]
pub use crate::sys::ext::fs::{DirEntryExt, FileExt, MetadataExt, OpenOptionsExt};
pub use super::fs::{DirEntryExt, FileExt, MetadataExt, OpenOptionsExt};
#[doc(no_inline)]
#[stable(feature = "rust1", since = "1.0.0")]
pub use crate::sys::ext::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
pub use super::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
}
2 changes: 1 addition & 1 deletion library/std/src/sys/wasi/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(unsafe_op_in_unsafe_fn)]

use super::fd::WasiFd;
use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt;
use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};
Expand All @@ -9,7 +10,6 @@ use crate::os::wasi::ffi::{OsStrExt, OsStringExt};
use crate::path::{Path, PathBuf};
use crate::ptr;
use crate::sync::Arc;
use crate::sys::fd::WasiFd;
use crate::sys::time::SystemTime;
use crate::sys::unsupported;
use crate::sys_common::FromInner;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/wasi/net.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![deny(unsafe_op_in_unsafe_fn)]

use super::fd::WasiFd;
use crate::convert::TryFrom;
use crate::fmt;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
use crate::sys::fd::WasiFd;
use crate::sys::{unsupported, Void};
use crate::sys_common::FromInner;
use crate::time::Duration;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/wasi/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![deny(unsafe_op_in_unsafe_fn)]

use super::fd::WasiFd;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem::ManuallyDrop;
use crate::sys::fd::WasiFd;

pub struct Stdin;
pub struct Stdout;
Expand Down