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,
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 {
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(())
7276#![ doc( test( attr( allow( dead_code, unused_variables, unused_mut) ) ) ) ]
7377
7478use tokio:: io:: { AsyncRead , AsyncWrite } ;
75-
7679mod error;
7780mod providers;
7881
@@ -86,6 +89,9 @@ pub mod p3;
8689pub use error:: Error ;
8790pub 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" ) ) ]
9197pub 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.
136167pub trait TlsTransport : AsyncRead + AsyncWrite + Send + Unpin + ' static { }
0 commit comments