Skip to content

Commit 51732cf

Browse files
authored
Use the same WasiTlsView types for both p2 & p3 (#12896)
1 parent 958860e commit 51732cf

9 files changed

Lines changed: 79 additions & 88 deletions

File tree

crates/wasi-tls/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ description = "Wasmtime implementation of the wasi-tls API"
1111
[lints]
1212
workspace = true
1313

14+
[package.metadata.docs.rs]
15+
all-features = true
16+
1417
[features]
1518
default = ["p2", "rustls"]
1619
p2 = ["wasmtime-wasi/p2"]

crates/wasi-tls/src/lib.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
//! component::{Linker, ResourceTable},
1414
//! Store, Engine, Result,
1515
//! };
16-
//! use wasmtime_wasi_tls::{WasiTlsCtx, WasiTlsCtxBuilder};
17-
//! use wasmtime_wasi_tls::p2::{LinkOptions, WasiTls};
16+
//! use wasmtime_wasi_tls::{WasiTlsCtx, WasiTlsCtxBuilder, WasiTlsView, WasiTlsCtxView};
17+
//! use wasmtime_wasi_tls::p2::LinkOptions;
1818
//!
1919
//! struct Ctx {
2020
//! table: ResourceTable,
@@ -28,6 +28,12 @@
2828
//! }
2929
//! }
3030
//!
31+
//! impl WasiTlsView for Ctx {
32+
//! fn tls(&mut self) -> WasiTlsCtxView<'_> {
33+
//! WasiTlsCtxView { ctx: &mut self.wasi_tls_ctx, table: &mut self.table }
34+
//! }
35+
//! }
36+
//!
3137
//! #[tokio::main]
3238
//! async fn main() -> Result<()> {
3339
//! let ctx = Ctx {
@@ -49,15 +55,13 @@
4955
//!
5056
//! // Set up wasi-cli
5157
//! let mut store = Store::new(&engine, ctx);
52-
//! let mut linker = Linker::new(&engine);
58+
//! let mut linker: Linker<Ctx> = Linker::new(&engine);
5359
//! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
5460
//!
5561
//! // Add wasi-tls types and turn on the feature in linker
5662
//! let mut opts = LinkOptions::default();
5763
//! opts.tls(true);
58-
//! wasmtime_wasi_tls::p2::add_to_linker(&mut linker, &mut opts, |h: &mut Ctx| {
59-
//! WasiTls::new(&h.wasi_tls_ctx, &mut h.table)
60-
//! })?;
64+
//! wasmtime_wasi_tls::p2::add_to_linker(&mut linker, &opts)?;
6165
//!
6266
//! // ... use `linker` to instantiate within `store` ...
6367
//! Ok(())
@@ -72,7 +76,6 @@
7276
#![doc(test(attr(allow(dead_code, unused_variables, unused_mut))))]
7377

7478
use tokio::io::{AsyncRead, AsyncWrite};
75-
7679
mod error;
7780
mod providers;
7881

@@ -86,6 +89,9 @@ pub mod p3;
8689
pub use error::Error;
8790
pub use providers::*;
8891

92+
#[cfg(any(feature = "p2", feature = "p3"))]
93+
use wasmtime::component::{HasData, ResourceTable};
94+
8995
/// Builder-style structure used to create a [`WasiTlsCtx`].
9096
#[cfg(any(feature = "p2", feature = "p3"))]
9197
pub struct WasiTlsCtxBuilder {
@@ -131,6 +137,31 @@ pub struct WasiTlsCtx {
131137
pub(crate) provider: Box<dyn TlsProvider>,
132138
}
133139

140+
/// The type for which this crate implements the `wasi:tls` interfaces.
141+
#[cfg(any(feature = "p2", feature = "p3"))]
142+
pub(crate) struct WasiTls;
143+
#[cfg(any(feature = "p2", feature = "p3"))]
144+
impl HasData for WasiTls {
145+
type Data<'a> = WasiTlsCtxView<'a>;
146+
}
147+
148+
/// View into [`WasiTlsCtx`] implementation and [`ResourceTable`].
149+
#[cfg(any(feature = "p2", feature = "p3"))]
150+
pub struct WasiTlsCtxView<'a> {
151+
/// Mutable reference to table used to manage resources.
152+
pub table: &'a mut ResourceTable,
153+
154+
/// Mutable reference to the WASI TLS context.
155+
pub ctx: &'a mut WasiTlsCtx,
156+
}
157+
158+
/// A trait which provides internal WASI TLS state.
159+
#[cfg(any(feature = "p2", feature = "p3"))]
160+
pub trait WasiTlsView: Send {
161+
/// Return a [`WasiTlsCtxView`] from mutable reference to self.
162+
fn tls(&mut self) -> WasiTlsCtxView<'_>;
163+
}
164+
134165
/// The data stream that carries the encrypted TLS data.
135166
/// Typically this is a TCP stream.
136167
pub trait TlsTransport: AsyncRead + AsyncWrite + Send + Unpin + 'static {}

crates/wasi-tls/src/p2/host.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ use wasmtime_wasi::p2::Pollable;
55
use wasmtime_wasi::p2::{DynInputStream, DynOutputStream, DynPollable, IoError};
66

77
use crate::p2::{
8-
WasiTls, bindings,
8+
bindings,
99
io::{
1010
AsyncReadStream, AsyncWriteStream, FutureOutput, WasiFuture, WasiStreamReader,
1111
WasiStreamWriter,
1212
},
1313
};
14-
use crate::{TlsStream, TlsTransport};
14+
use crate::{TlsStream, TlsTransport, WasiTlsCtxView};
1515

16-
impl<'a> bindings::types::Host for WasiTls<'a> {}
16+
impl<'a> bindings::types::Host for WasiTlsCtxView<'a> {}
1717

1818
/// Represents the ClientHandshake which will be used to configure the handshake
1919
pub struct HostClientHandshake {
2020
server_name: String,
2121
transport: Box<dyn TlsTransport>,
2222
}
2323

24-
impl<'a> bindings::types::HostClientHandshake for WasiTls<'a> {
24+
impl<'a> bindings::types::HostClientHandshake for WasiTlsCtxView<'a> {
2525
fn new(
2626
&mut self,
2727
server_name: String,
@@ -86,7 +86,7 @@ impl Pollable for HostFutureClientStreams {
8686
}
8787
}
8888

89-
impl<'a> bindings::types::HostFutureClientStreams for WasiTls<'a> {
89+
impl<'a> bindings::types::HostFutureClientStreams for WasiTlsCtxView<'a> {
9090
fn subscribe(
9191
&mut self,
9292
this: Resource<HostFutureClientStreams>,
@@ -145,7 +145,7 @@ pub struct HostClientConnection(
145145
crate::p2::io::AsyncWriteStream<tokio::io::WriteHalf<Box<dyn TlsStream>>>,
146146
);
147147

148-
impl<'a> bindings::types::HostClientConnection for WasiTls<'a> {
148+
impl<'a> bindings::types::HostClientConnection for WasiTlsCtxView<'a> {
149149
fn close_output(&mut self, this: Resource<HostClientConnection>) -> wasmtime::Result<()> {
150150
self.table.get_mut(&this)?.0.close()
151151
}

crates/wasi-tls/src/p2/mod.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use wasmtime::component::{HasData, ResourceTable};
2-
3-
use crate::WasiTlsCtx;
1+
use crate::{WasiTls, WasiTlsView};
42

53
pub mod bindings;
64
mod host;
@@ -9,30 +7,14 @@ mod io;
97
pub use bindings::types::LinkOptions;
108
pub use host::{HostClientConnection, HostClientHandshake, HostFutureClientStreams};
119

12-
/// Capture the state necessary for use in the `wasi-tls` API implementation.
13-
pub struct WasiTls<'a> {
14-
pub(crate) ctx: &'a WasiTlsCtx,
15-
pub(crate) table: &'a mut ResourceTable,
16-
}
17-
18-
impl<'a> WasiTls<'a> {
19-
/// Create a new Wasi TLS context.
20-
pub fn new(ctx: &'a WasiTlsCtx, table: &'a mut ResourceTable) -> Self {
21-
Self { ctx, table }
22-
}
23-
}
24-
2510
/// Add the `wasi-tls` world's types to a [`wasmtime::component::Linker`].
26-
pub fn add_to_linker<T: Send + 'static>(
11+
pub fn add_to_linker<T>(
2712
l: &mut wasmtime::component::Linker<T>,
28-
opts: &mut LinkOptions,
29-
f: fn(&mut T) -> WasiTls<'_>,
30-
) -> wasmtime::Result<()> {
31-
bindings::types::add_to_linker::<_, HasWasiTls>(l, &opts, f)?;
13+
opts: &LinkOptions,
14+
) -> wasmtime::Result<()>
15+
where
16+
T: WasiTlsView + 'static,
17+
{
18+
bindings::types::add_to_linker::<_, WasiTls>(l, &opts, T::tls)?;
3219
Ok(())
3320
}
34-
35-
struct HasWasiTls;
36-
impl HasData for HasWasiTls {
37-
type Data<'a> = WasiTls<'a>;
38-
}

crates/wasi-tls/src/p3/host.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! p3 host implementation for `wasi:tls`.
22
33
use crate::p3::util::{AsyncReadProducer, AsyncWriteConsumer, Closed, Deferred, Shared, pipe};
4-
use crate::p3::{WasiTls, WasiTlsCtxView, bindings};
5-
use crate::{BoxFutureTlsStream, Error, TlsStream};
4+
use crate::p3::{WasiTls, bindings};
5+
use crate::{BoxFutureTlsStream, Error, TlsStream, WasiTlsCtxView};
66
use std::pin::Pin;
77
use std::task::{Context, Poll};
88
use tokio::{io::AsyncWriteExt as _, sync::oneshot};

crates/wasi-tls/src/p3/mod.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,9 @@ pub mod bindings;
1212
pub mod host;
1313
pub(crate) mod util;
1414

15-
use crate::WasiTlsCtx;
15+
use crate::{WasiTls, WasiTlsView};
1616
use bindings::tls::{client, types};
17-
use wasmtime::component::{HasData, Linker, ResourceTable};
18-
19-
/// The type for which this crate implements the `wasi:tls` interfaces.
20-
pub struct WasiTls;
21-
22-
impl HasData for WasiTls {
23-
type Data<'a> = WasiTlsCtxView<'a>;
24-
}
25-
26-
/// View into [`WasiTlsCtx`] implementation and [`ResourceTable`].
27-
pub struct WasiTlsCtxView<'a> {
28-
/// Mutable reference to table used to manage resources.
29-
pub table: &'a mut ResourceTable,
30-
31-
/// Mutable reference to the WASI TLS context.
32-
pub ctx: &'a mut WasiTlsCtx,
33-
}
34-
35-
/// A trait which provides internal WASI TLS state.
36-
pub trait WasiTlsView: Send {
37-
/// Return a [`WasiTlsCtxView`] from mutable reference to self.
38-
fn tls(&mut self) -> WasiTlsCtxView<'_>;
39-
}
17+
use wasmtime::component::Linker;
4018

4119
/// Add all interfaces from this module into the `linker` provided.
4220
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>

crates/wasi-tls/tests/p2/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use wasmtime::{
44
format_err,
55
};
66
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p2::bindings::Command};
7-
use wasmtime_wasi_tls::{TlsProvider, WasiTlsCtx, WasiTlsCtxBuilder, p2};
7+
use wasmtime_wasi_tls::{
8+
TlsProvider, WasiTlsCtx, WasiTlsCtxBuilder, WasiTlsCtxView, WasiTlsView, p2,
9+
};
810

911
struct Ctx {
1012
table: ResourceTable,
@@ -21,6 +23,15 @@ impl WasiView for Ctx {
2123
}
2224
}
2325

26+
impl WasiTlsView for Ctx {
27+
fn tls(&mut self) -> WasiTlsCtxView<'_> {
28+
WasiTlsCtxView {
29+
ctx: &mut self.wasi_tls_ctx,
30+
table: &mut self.table,
31+
}
32+
}
33+
}
34+
2435
async fn run_test(provider: Box<dyn TlsProvider>, path: &str) -> Result<()> {
2536
let ctx = Ctx {
2637
table: ResourceTable::new(),
@@ -41,9 +52,7 @@ async fn run_test(provider: Box<dyn TlsProvider>, path: &str) -> Result<()> {
4152
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
4253
let mut opts = p2::LinkOptions::default();
4354
opts.tls(true);
44-
p2::add_to_linker(&mut linker, &mut opts, |h: &mut Ctx| {
45-
p2::WasiTls::new(&h.wasi_tls_ctx, &mut h.table)
46-
})?;
55+
wasmtime_wasi_tls::p2::add_to_linker(&mut linker, &opts)?;
4756

4857
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
4958
command

crates/wasi-tls/tests/p3/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use wasmtime::{
44
format_err,
55
};
66
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p3::bindings::Command};
7-
use wasmtime_wasi_tls::{
8-
TlsProvider, WasiTlsCtx, WasiTlsCtxBuilder,
9-
p3::{WasiTlsCtxView, WasiTlsView},
10-
};
7+
use wasmtime_wasi_tls::{TlsProvider, WasiTlsCtx, WasiTlsCtxBuilder, WasiTlsCtxView, WasiTlsView};
118

129
struct Ctx {
1310
table: ResourceTable,

src/commands/run.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuild
3030
use wasmtime_wasi_nn::wit::WasiNnView;
3131
#[cfg(feature = "wasi-threads")]
3232
use wasmtime_wasi_threads::WasiThreadsCtx;
33-
#[cfg(feature = "wasi-tls")]
34-
use wasmtime_wasi_tls::{WasiTlsCtx, p2::WasiTls};
3533

3634
fn parse_preloads(s: &str) -> Result<(String, PathBuf)> {
3735
let parts: Vec<&str> = s.splitn(2, '=').collect();
@@ -1319,14 +1317,7 @@ impl RunCommand {
13191317
CliLinker::Component(linker) => {
13201318
let mut opts = wasmtime_wasi_tls::p2::LinkOptions::default();
13211319
opts.tls(true);
1322-
wasmtime_wasi_tls::p2::add_to_linker(linker, &mut opts, |h| {
1323-
let ctx = h.wasip1_ctx.as_mut().expect("wasi is not configured");
1324-
let ctx = Arc::get_mut(ctx).unwrap().get_mut().unwrap();
1325-
WasiTls::new(
1326-
Arc::get_mut(h.wasi_tls.as_mut().unwrap()).unwrap(),
1327-
ctx.ctx().table,
1328-
)
1329-
})?;
1320+
wasmtime_wasi_tls::p2::add_to_linker(linker, &opts)?;
13301321

13311322
#[cfg(feature = "component-model-async")]
13321323
if self.run.common.wasi.p3.unwrap_or(crate::common::P3_DEFAULT) {
@@ -1502,7 +1493,7 @@ pub struct Host {
15021493
#[cfg(feature = "wasi-keyvalue")]
15031494
wasi_keyvalue: Option<Arc<WasiKeyValueCtx>>,
15041495
#[cfg(feature = "wasi-tls")]
1505-
wasi_tls: Option<Arc<WasiTlsCtx>>,
1496+
wasi_tls: Option<Arc<wasmtime_wasi_tls::WasiTlsCtx>>,
15061497
}
15071498

15081499
impl Host {
@@ -1551,10 +1542,10 @@ impl wasmtime_wasi_http::p3::WasiHttpView for Host {
15511542
}
15521543
}
15531544

1554-
#[cfg(all(feature = "wasi-tls", feature = "component-model-async"))]
1555-
impl wasmtime_wasi_tls::p3::WasiTlsView for Host {
1556-
fn tls(&mut self) -> wasmtime_wasi_tls::p3::WasiTlsCtxView<'_> {
1557-
wasmtime_wasi_tls::p3::WasiTlsCtxView {
1545+
#[cfg(all(feature = "wasi-tls"))]
1546+
impl wasmtime_wasi_tls::WasiTlsView for Host {
1547+
fn tls(&mut self) -> wasmtime_wasi_tls::WasiTlsCtxView<'_> {
1548+
wasmtime_wasi_tls::WasiTlsCtxView {
15581549
table: WasiView::ctx(unwrap_singlethread_context(&mut self.wasip1_ctx)).table,
15591550
ctx: Arc::get_mut(self.wasi_tls.as_mut().unwrap()).unwrap(),
15601551
}

0 commit comments

Comments
 (0)