Skip to content

Commit e017b7f

Browse files
authored
Expand some docs on wasmtime-wasi (#11598)
Add public docs for APIs added in #11593 and additionally internalize some crate fields to avoid exposing too many internal implementation details.
1 parent 6cd3595 commit e017b7f

File tree

5 files changed

+193
-7
lines changed

5 files changed

+193
-7
lines changed

crates/wasi/src/cli.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,44 @@ pub use self::locked_async::{AsyncStdinStream, AsyncStdoutStream};
2020
#[doc(no_inline)]
2121
pub use tokio::io::{Stderr, Stdin, Stdout, stderr, stdin, stdout};
2222

23+
/// A helper struct which implements [`HasData`] for the `wasi:cli` APIs.
24+
///
25+
/// This can be useful when directly calling `add_to_linker` functions directly,
26+
/// such as [`wasmtime_wasi::p2::bindings::cli::environment::add_to_linker`] as
27+
/// the `D` type parameter. See [`HasData`] for more information about the type
28+
/// parameter's purpose.
29+
///
30+
/// When using this type you can skip the [`WasiCliView`] trait, for
31+
/// example.
32+
///
33+
/// # Examples
34+
///
35+
/// ```
36+
/// use wasmtime::component::{Linker, ResourceTable};
37+
/// use wasmtime::{Engine, Result, Config};
38+
/// use wasmtime_wasi::cli::*;
39+
///
40+
/// struct MyStoreState {
41+
/// table: ResourceTable,
42+
/// cli: WasiCliCtx,
43+
/// }
44+
///
45+
/// fn main() -> Result<()> {
46+
/// let mut config = Config::new();
47+
/// config.async_support(true);
48+
/// let engine = Engine::new(&config)?;
49+
/// let mut linker = Linker::new(&engine);
50+
///
51+
/// wasmtime_wasi::p2::bindings::cli::environment::add_to_linker::<MyStoreState, WasiCli>(
52+
/// &mut linker,
53+
/// |state| WasiCliCtxView {
54+
/// table: &mut state.table,
55+
/// ctx: &mut state.cli,
56+
/// },
57+
/// )?;
58+
/// Ok(())
59+
/// }
60+
/// ```
2361
pub struct WasiCli;
2462

2563
impl HasData for WasiCli {

crates/wasi/src/clocks.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,53 @@ use cap_std::{AmbientAuthority, ambient_authority};
33
use cap_time_ext::{MonotonicClockExt as _, SystemClockExt as _};
44
use wasmtime::component::{HasData, ResourceTable};
55

6+
/// A helper struct which implements [`HasData`] for the `wasi:clocks` APIs.
7+
///
8+
/// This can be useful when directly calling `add_to_linker` functions directly,
9+
/// such as [`wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker`] as
10+
/// the `D` type parameter. See [`HasData`] for more information about the type
11+
/// parameter's purpose.
12+
///
13+
/// When using this type you can skip the [`WasiClocksView`] trait, for
14+
/// example.
15+
///
16+
/// # Examples
17+
///
18+
/// ```
19+
/// use wasmtime::component::{Linker, ResourceTable};
20+
/// use wasmtime::{Engine, Result, Config};
21+
/// use wasmtime_wasi::clocks::*;
22+
///
23+
/// struct MyStoreState {
24+
/// table: ResourceTable,
25+
/// clocks: WasiClocksCtx,
26+
/// }
27+
///
28+
/// fn main() -> Result<()> {
29+
/// let mut config = Config::new();
30+
/// config.async_support(true);
31+
/// let engine = Engine::new(&config)?;
32+
/// let mut linker = Linker::new(&engine);
33+
///
34+
/// wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker::<MyStoreState, WasiClocks>(
35+
/// &mut linker,
36+
/// |state| WasiClocksCtxView {
37+
/// table: &mut state.table,
38+
/// ctx: &mut state.clocks,
39+
/// },
40+
/// )?;
41+
/// Ok(())
42+
/// }
43+
/// ```
644
pub struct WasiClocks;
745

846
impl HasData for WasiClocks {
947
type Data<'a> = WasiClocksCtxView<'a>;
1048
}
1149

1250
pub struct WasiClocksCtx {
13-
pub wall_clock: Box<dyn HostWallClock + Send>,
14-
pub monotonic_clock: Box<dyn HostMonotonicClock + Send>,
51+
pub(crate) wall_clock: Box<dyn HostWallClock + Send>,
52+
pub(crate) monotonic_clock: Box<dyn HostMonotonicClock + Send>,
1553
}
1654

1755
impl Default for WasiClocksCtx {

crates/wasi/src/filesystem.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ use std::sync::Arc;
88
use tracing::debug;
99
use wasmtime::component::{HasData, Resource, ResourceTable};
1010

11+
/// A helper struct which implements [`HasData`] for the `wasi:filesystem` APIs.
12+
///
13+
/// This can be useful when directly calling `add_to_linker` functions directly,
14+
/// such as [`wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker`] as
15+
/// the `D` type parameter. See [`HasData`] for more information about the type
16+
/// parameter's purpose.
17+
///
18+
/// When using this type you can skip the [`WasiFilesystemView`] trait, for
19+
/// example.
20+
///
21+
/// # Examples
22+
///
23+
/// ```
24+
/// use wasmtime::component::{Linker, ResourceTable};
25+
/// use wasmtime::{Engine, Result, Config};
26+
/// use wasmtime_wasi::filesystem::*;
27+
///
28+
/// struct MyStoreState {
29+
/// table: ResourceTable,
30+
/// filesystem: WasiFilesystemCtx,
31+
/// }
32+
///
33+
/// fn main() -> Result<()> {
34+
/// let mut config = Config::new();
35+
/// config.async_support(true);
36+
/// let engine = Engine::new(&config)?;
37+
/// let mut linker = Linker::new(&engine);
38+
///
39+
/// wasmtime_wasi::p2::bindings::filesystem::types::add_to_linker::<MyStoreState, WasiFilesystem>(
40+
/// &mut linker,
41+
/// |state| WasiFilesystemCtxView {
42+
/// table: &mut state.table,
43+
/// ctx: &mut state.filesystem,
44+
/// },
45+
/// )?;
46+
/// Ok(())
47+
/// }
48+
/// ```
1149
pub struct WasiFilesystem;
1250

1351
impl HasData for WasiFilesystem {
@@ -16,8 +54,8 @@ impl HasData for WasiFilesystem {
1654

1755
#[derive(Clone, Default)]
1856
pub struct WasiFilesystemCtx {
19-
pub allow_blocking_current_thread: bool,
20-
pub preopens: Vec<(Dir, String)>,
57+
pub(crate) allow_blocking_current_thread: bool,
58+
pub(crate) preopens: Vec<(Dir, String)>,
2159
}
2260

2361
pub struct WasiFilesystemCtxView<'a> {

crates/wasi/src/random.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
use cap_rand::{Rng as _, RngCore, SeedableRng as _};
22
use wasmtime::component::HasData;
33

4+
/// A helper struct which implements [`HasData`] for the `wasi:random` APIs.
5+
///
6+
/// This can be useful when directly calling `add_to_linker` functions directly,
7+
/// such as [`wasmtime_wasi::p2::bindings::random::random::add_to_linker`] as
8+
/// the `D` type parameter. See [`HasData`] for more information about the type
9+
/// parameter's purpose.
10+
///
11+
/// When using this type you can skip the [`WasiRandomView`] trait, for
12+
/// example.
13+
///
14+
/// # Examples
15+
///
16+
/// ```
17+
/// use wasmtime::component::Linker;
18+
/// use wasmtime::{Engine, Result, Config};
19+
/// use wasmtime_wasi::random::*;
20+
///
21+
/// struct MyStoreState {
22+
/// random: WasiRandomCtx,
23+
/// }
24+
///
25+
/// fn main() -> Result<()> {
26+
/// let mut config = Config::new();
27+
/// config.async_support(true);
28+
/// let engine = Engine::new(&config)?;
29+
/// let mut linker = Linker::new(&engine);
30+
///
31+
/// wasmtime_wasi::p2::bindings::random::random::add_to_linker::<MyStoreState, WasiRandom>(
32+
/// &mut linker,
33+
/// |state| &mut state.random,
34+
/// )?;
35+
/// Ok(())
36+
/// }
37+
/// ```
438
pub struct WasiRandom;
539

640
impl HasData for WasiRandom {
741
type Data<'a> = &'a mut WasiRandomCtx;
842
}
943

1044
pub struct WasiRandomCtx {
11-
pub random: Box<dyn RngCore + Send>,
12-
pub insecure_random: Box<dyn RngCore + Send>,
13-
pub insecure_random_seed: u128,
45+
pub(crate) random: Box<dyn RngCore + Send>,
46+
pub(crate) insecure_random: Box<dyn RngCore + Send>,
47+
pub(crate) insecure_random_seed: u128,
1448
}
1549

1650
impl Default for WasiRandomCtx {

crates/wasi/src/sockets/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,44 @@ pub(crate) use tcp::NonInheritedOptions;
1414
pub use tcp::TcpSocket;
1515
pub use udp::UdpSocket;
1616

17+
/// A helper struct which implements [`HasData`] for the `wasi:sockets` APIs.
18+
///
19+
/// This can be useful when directly calling `add_to_linker` functions directly,
20+
/// such as [`wasmtime_wasi::p2::bindings::sockets::tcp::add_to_linker`] as the
21+
/// `D` type parameter. See [`HasData`] for more information about the type
22+
/// parameter's purpose.
23+
///
24+
/// When using this type you can skip the [`WasiSocketsView`] trait, for
25+
/// example.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// use wasmtime::component::{Linker, ResourceTable};
31+
/// use wasmtime::{Engine, Result, Config};
32+
/// use wasmtime_wasi::sockets::*;
33+
///
34+
/// struct MyStoreState {
35+
/// table: ResourceTable,
36+
/// sockets: WasiSocketsCtx,
37+
/// }
38+
///
39+
/// fn main() -> Result<()> {
40+
/// let mut config = Config::new();
41+
/// config.async_support(true);
42+
/// let engine = Engine::new(&config)?;
43+
/// let mut linker = Linker::new(&engine);
44+
///
45+
/// wasmtime_wasi::p2::bindings::sockets::tcp::add_to_linker::<MyStoreState, WasiSockets>(
46+
/// &mut linker,
47+
/// |state| WasiSocketsCtxView {
48+
/// ctx: &mut state.sockets,
49+
/// table: &mut state.table,
50+
/// },
51+
/// )?;
52+
/// Ok(())
53+
/// }
54+
/// ```
1755
pub struct WasiSockets;
1856

1957
impl HasData for WasiSockets {

0 commit comments

Comments
 (0)