Skip to content

Commit 11ed60a

Browse files
committed
Switch to using impl InitContext
Signed-off-by: Alex Crichton <[email protected]>
1 parent 91a5231 commit 11ed60a

File tree

11 files changed

+97
-98
lines changed

11 files changed

+97
-98
lines changed

crates/factor-key-value/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ impl Factor for KeyValueFactor {
3838
type AppState = AppState;
3939
type InstanceBuilder = InstanceBuilder;
4040

41-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
42-
where
43-
C: InitContext<Self>,
44-
{
41+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
4542
ctx.link_bindings(spin_world::v1::key_value::add_to_linker)?;
4643
ctx.link_bindings(spin_world::v2::key_value::add_to_linker)?;
4744
ctx.link_bindings(spin_world::wasi::keyvalue::store::add_to_linker)?;

crates/factor-llm/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ impl Factor for LlmFactor {
3636
type AppState = AppState;
3737
type InstanceBuilder = InstanceState;
3838

39-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
40-
where
41-
C: spin_factors::InitContext<Self>,
42-
{
39+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
4340
ctx.link_bindings(spin_world::v1::llm::add_to_linker)?;
4441
ctx.link_bindings(spin_world::v2::llm::add_to_linker)?;
4542
Ok(())

crates/factor-outbound-http/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ impl Factor for OutboundHttpFactor {
5252
type AppState = ();
5353
type InstanceBuilder = InstanceState;
5454

55-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
56-
where
57-
C: spin_factors::InitContext<Self>,
58-
{
55+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
5956
ctx.link_bindings(spin_world::v1::http::add_to_linker)?;
6057
wasi::add_to_linker(ctx)?;
6158
Ok(())

crates/factor-outbound-mqtt/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ impl Factor for OutboundMqttFactor {
3131
type AppState = ();
3232
type InstanceBuilder = InstanceState;
3333

34-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
35-
where
36-
C: spin_factors::InitContext<Self>,
37-
{
34+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
3835
ctx.link_bindings(spin_world::v2::mqtt::add_to_linker)?;
3936
Ok(())
4037
}

crates/factor-outbound-mysql/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {
1717
type AppState = ();
1818
type InstanceBuilder = InstanceState<C>;
1919

20-
fn init<I>(&mut self, ctx: &mut I) -> anyhow::Result<()>
21-
where
22-
I: InitContext<Self>,
23-
{
20+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
2421
ctx.link_bindings(v1::add_to_linker)?;
2522
ctx.link_bindings(v2::add_to_linker)?;
2623
Ok(())

crates/factor-outbound-pg/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundPgFactor<C> {
1717
type AppState = ();
1818
type InstanceBuilder = InstanceState<C>;
1919

20-
fn init<I>(&mut self, ctx: &mut I) -> anyhow::Result<()>
21-
where
22-
I: spin_factors::InitContext<Self>,
23-
{
20+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
2421
ctx.link_bindings(spin_world::v1::postgres::add_to_linker)?;
2522
ctx.link_bindings(spin_world::v2::postgres::add_to_linker)?;
2623
ctx.link_bindings(spin_world::spin::postgres::postgres::add_to_linker)?;

crates/factor-outbound-redis/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ impl Factor for OutboundRedisFactor {
2323
type AppState = ();
2424
type InstanceBuilder = InstanceState;
2525

26-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
27-
where
28-
C: spin_factors::InitContext<Self>,
29-
{
26+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
3027
ctx.link_bindings(spin_world::v1::redis::add_to_linker)?;
3128
ctx.link_bindings(spin_world::v2::redis::add_to_linker)?;
3229
Ok(())

crates/factor-sqlite/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ impl Factor for SqliteFactor {
3232
type AppState = AppState;
3333
type InstanceBuilder = InstanceState;
3434

35-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
36-
where
37-
C: spin_factors::InitContext<Self>,
38-
{
35+
fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
3936
ctx.link_bindings(v1::add_to_linker)?;
4037
ctx.link_bindings(v2::add_to_linker)?;
4138
ctx.link_bindings(v3::add_to_linker)?;

crates/factor-variables/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ impl Factor for VariablesFactor {
2828
type AppState = AppState;
2929
type InstanceBuilder = InstanceState;
3030

31-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
32-
where
33-
C: InitContext<Self>,
34-
{
31+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
3532
ctx.link_bindings(spin_world::v1::config::add_to_linker)?;
3633
ctx.link_bindings(spin_world::v2::variables::add_to_linker)?;
3734
ctx.link_bindings(spin_world::wasi::config::store::add_to_linker)?;

crates/factor-wasi/src/lib.rs

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -44,66 +44,98 @@ impl WasiFactor {
4444
}
4545
}
4646

47-
impl Factor for WasiFactor {
48-
type RuntimeConfig = ();
49-
type AppState = ();
50-
type InstanceBuilder = InstanceBuilder;
47+
/// Helper trait to extend `InitContext` with some more `link_*_bindings`
48+
/// methods related to `wasmtime-wasi` and `wasmtime-wasi-io`-specific
49+
/// signatures.
50+
#[allow(clippy::type_complexity, reason = "sorry, blame alex")]
51+
trait InitContextExt: InitContext<WasiFactor> {
52+
fn get_io(data: &mut Self::StoreData) -> IoImpl<WasiImplInner<'_>> {
53+
let (state, table) = Self::get_data_with_table(data);
54+
IoImpl(WasiImplInner {
55+
ctx: &mut state.ctx,
56+
table,
57+
})
58+
}
5159

52-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
60+
fn link_io_bindings(
61+
&mut self,
62+
add_to_linker: fn(
63+
&mut wasmtime::component::Linker<Self::StoreData>,
64+
fn(&mut Self::StoreData) -> IoImpl<WasiImplInner<'_>>,
65+
) -> anyhow::Result<()>,
66+
) -> anyhow::Result<()> {
67+
add_to_linker(self.linker(), Self::get_io)
68+
}
69+
70+
fn get_wasi(data: &mut Self::StoreData) -> WasiImpl<WasiImplInner<'_>> {
71+
WasiImpl(Self::get_io(data))
72+
}
73+
74+
fn link_wasi_bindings(
75+
&mut self,
76+
add_to_linker: fn(
77+
&mut wasmtime::component::Linker<Self::StoreData>,
78+
fn(&mut Self::StoreData) -> WasiImpl<WasiImplInner<'_>>,
79+
) -> anyhow::Result<()>,
80+
) -> anyhow::Result<()> {
81+
add_to_linker(self.linker(), Self::get_wasi)
82+
}
83+
84+
fn link_wasi_default_bindings<O>(
85+
&mut self,
86+
add_to_linker: fn(
87+
&mut wasmtime::component::Linker<Self::StoreData>,
88+
&O,
89+
fn(&mut Self::StoreData) -> WasiImpl<WasiImplInner<'_>>,
90+
) -> anyhow::Result<()>,
91+
) -> anyhow::Result<()>
5392
where
54-
C: InitContext<Self>,
93+
O: Default,
5594
{
56-
fn get_io<C: InitContext<WasiFactor>>(
57-
data: &mut C::StoreData,
58-
) -> IoImpl<WasiImplInner<'_>> {
59-
let (state, table) = C::get_data_with_table(data);
60-
IoImpl(WasiImplInner {
61-
ctx: &mut state.ctx,
62-
table,
63-
})
64-
}
95+
add_to_linker(self.linker(), &O::default(), Self::get_wasi)
96+
}
97+
}
6598

66-
fn get_wasi<C: InitContext<WasiFactor>>(
67-
data: &mut C::StoreData,
68-
) -> WasiImpl<WasiImplInner<'_>> {
69-
WasiImpl(get_io::<C>(data))
70-
}
99+
impl<T> InitContextExt for T where T: InitContext<WasiFactor> {}
100+
101+
impl Factor for WasiFactor {
102+
type RuntimeConfig = ();
103+
type AppState = ();
104+
type InstanceBuilder = InstanceBuilder;
71105

72-
let get_io = get_io::<C> as fn(&mut C::StoreData) -> IoImpl<WasiImplInner<'_>>;
73-
let get_wasi = get_wasi::<C> as fn(&mut C::StoreData) -> WasiImpl<WasiImplInner<'_>>;
74-
let linker = ctx.linker();
106+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
75107
use wasmtime_wasi::bindings;
76-
bindings::clocks::wall_clock::add_to_linker_get_host(linker, get_wasi)?;
77-
bindings::clocks::monotonic_clock::add_to_linker_get_host(linker, get_wasi)?;
78-
bindings::filesystem::types::add_to_linker_get_host(linker, get_wasi)?;
79-
bindings::filesystem::preopens::add_to_linker_get_host(linker, get_wasi)?;
80-
bindings::io::error::add_to_linker_get_host(linker, get_io)?;
81-
bindings::io::poll::add_to_linker_get_host(linker, get_io)?;
82-
bindings::io::streams::add_to_linker_get_host(linker, get_io)?;
83-
bindings::random::random::add_to_linker_get_host(linker, get_wasi)?;
84-
bindings::random::insecure::add_to_linker_get_host(linker, get_wasi)?;
85-
bindings::random::insecure_seed::add_to_linker_get_host(linker, get_wasi)?;
86-
bindings::cli::exit::add_to_linker_get_host(linker, &Default::default(), get_wasi)?;
87-
bindings::cli::environment::add_to_linker_get_host(linker, get_wasi)?;
88-
bindings::cli::stdin::add_to_linker_get_host(linker, get_wasi)?;
89-
bindings::cli::stdout::add_to_linker_get_host(linker, get_wasi)?;
90-
bindings::cli::stderr::add_to_linker_get_host(linker, get_wasi)?;
91-
bindings::cli::terminal_input::add_to_linker_get_host(linker, get_wasi)?;
92-
bindings::cli::terminal_output::add_to_linker_get_host(linker, get_wasi)?;
93-
bindings::cli::terminal_stdin::add_to_linker_get_host(linker, get_wasi)?;
94-
bindings::cli::terminal_stdout::add_to_linker_get_host(linker, get_wasi)?;
95-
bindings::cli::terminal_stderr::add_to_linker_get_host(linker, get_wasi)?;
96-
bindings::sockets::tcp::add_to_linker_get_host(linker, get_wasi)?;
97-
bindings::sockets::tcp_create_socket::add_to_linker_get_host(linker, get_wasi)?;
98-
bindings::sockets::udp::add_to_linker_get_host(linker, get_wasi)?;
99-
bindings::sockets::udp_create_socket::add_to_linker_get_host(linker, get_wasi)?;
100-
bindings::sockets::instance_network::add_to_linker_get_host(linker, get_wasi)?;
101-
bindings::sockets::network::add_to_linker_get_host(linker, &Default::default(), get_wasi)?;
102-
bindings::sockets::ip_name_lookup::add_to_linker_get_host(linker, get_wasi)?;
103-
104-
wasi_2023_10_18::add_to_linker(linker, get_wasi)?;
105-
wasi_2023_11_10::add_to_linker(linker, get_wasi)?;
106108

109+
ctx.link_wasi_bindings(bindings::clocks::wall_clock::add_to_linker_get_host)?;
110+
ctx.link_wasi_bindings(bindings::clocks::monotonic_clock::add_to_linker_get_host)?;
111+
ctx.link_wasi_bindings(bindings::filesystem::types::add_to_linker_get_host)?;
112+
ctx.link_wasi_bindings(bindings::filesystem::preopens::add_to_linker_get_host)?;
113+
ctx.link_io_bindings(bindings::io::error::add_to_linker_get_host)?;
114+
ctx.link_io_bindings(bindings::io::poll::add_to_linker_get_host)?;
115+
ctx.link_io_bindings(bindings::io::streams::add_to_linker_get_host)?;
116+
ctx.link_wasi_bindings(bindings::random::random::add_to_linker_get_host)?;
117+
ctx.link_wasi_bindings(bindings::random::insecure::add_to_linker_get_host)?;
118+
ctx.link_wasi_bindings(bindings::random::insecure_seed::add_to_linker_get_host)?;
119+
ctx.link_wasi_default_bindings(bindings::cli::exit::add_to_linker_get_host)?;
120+
ctx.link_wasi_bindings(bindings::cli::environment::add_to_linker_get_host)?;
121+
ctx.link_wasi_bindings(bindings::cli::stdin::add_to_linker_get_host)?;
122+
ctx.link_wasi_bindings(bindings::cli::stdout::add_to_linker_get_host)?;
123+
ctx.link_wasi_bindings(bindings::cli::stderr::add_to_linker_get_host)?;
124+
ctx.link_wasi_bindings(bindings::cli::terminal_input::add_to_linker_get_host)?;
125+
ctx.link_wasi_bindings(bindings::cli::terminal_output::add_to_linker_get_host)?;
126+
ctx.link_wasi_bindings(bindings::cli::terminal_stdin::add_to_linker_get_host)?;
127+
ctx.link_wasi_bindings(bindings::cli::terminal_stdout::add_to_linker_get_host)?;
128+
ctx.link_wasi_bindings(bindings::cli::terminal_stderr::add_to_linker_get_host)?;
129+
ctx.link_wasi_bindings(bindings::sockets::tcp::add_to_linker_get_host)?;
130+
ctx.link_wasi_bindings(bindings::sockets::tcp_create_socket::add_to_linker_get_host)?;
131+
ctx.link_wasi_bindings(bindings::sockets::udp::add_to_linker_get_host)?;
132+
ctx.link_wasi_bindings(bindings::sockets::udp_create_socket::add_to_linker_get_host)?;
133+
ctx.link_wasi_bindings(bindings::sockets::instance_network::add_to_linker_get_host)?;
134+
ctx.link_wasi_default_bindings(bindings::sockets::network::add_to_linker_get_host)?;
135+
ctx.link_wasi_bindings(bindings::sockets::ip_name_lookup::add_to_linker_get_host)?;
136+
137+
ctx.link_wasi_bindings(wasi_2023_10_18::add_to_linker)?;
138+
ctx.link_wasi_bindings(wasi_2023_11_10::add_to_linker)?;
107139
Ok(())
108140
}
109141

crates/factors/src/factor.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ pub trait Factor: Any + Sized {
2828
/// This will be called at most once, before any call to
2929
/// [`Factor::prepare`]. `InitContext` provides access to a wasmtime
3030
/// `Linker`, so this is where any bindgen `add_to_linker` calls go.
31-
///
32-
/// The type parameter `T` here is the same as the [`wasmtime::Store`] type
33-
/// parameter `T`, which will contain the [`RuntimeFactors::InstanceState`].
34-
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
35-
where
36-
C: InitContext<Self>,
37-
{
31+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
3832
let _ = ctx;
3933
Ok(())
4034
}
@@ -79,6 +73,7 @@ pub type FactorInstanceState<F> =
7973
/// An InitContext is passed to [`Factor::init`], giving access to the global
8074
/// common [`wasmtime::component::Linker`].
8175
pub trait InitContext<F: Factor> {
76+
/// The `T` in `Store<T>`.
8277
type StoreData: Send + 'static;
8378

8479
/// Returns a mutable reference to the [`wasmtime::component::Linker`].
@@ -103,8 +98,7 @@ pub trait InitContext<F: Factor> {
10398
fn(&mut Self::StoreData) -> &mut FactorInstanceState<F>,
10499
) -> anyhow::Result<()>,
105100
) -> anyhow::Result<()> {
106-
let get_data = Self::get_data;
107-
add_to_linker(self.linker(), get_data)
101+
add_to_linker(self.linker(), Self::get_data)
108102
}
109103
}
110104

0 commit comments

Comments
 (0)