Skip to content

Commit b7e1a22

Browse files
authored
Merge pull request #3134 from alexcrichton/refactor-for-has-data
Refactor how `InitContext` works with factors
2 parents dd6b7b7 + 11ed60a commit b7e1a22

File tree

18 files changed

+208
-166
lines changed

18 files changed

+208
-166
lines changed

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

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

41-
fn init<T: Send + 'static>(&mut self, mut ctx: InitContext<T, Self>) -> anyhow::Result<()> {
41+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
4242
ctx.link_bindings(spin_world::v1::key_value::add_to_linker)?;
4343
ctx.link_bindings(spin_world::v2::key_value::add_to_linker)?;
4444
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<T: Send + 'static>(
40-
&mut self,
41-
mut ctx: spin_factors::InitContext<T, Self>,
42-
) -> anyhow::Result<()> {
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ impl Factor for OutboundHttpFactor {
5252
type AppState = ();
5353
type InstanceBuilder = InstanceState;
5454

55-
fn init<T: Send + 'static>(
56-
&mut self,
57-
mut ctx: spin_factors::InitContext<T, Self>,
58-
) -> anyhow::Result<()> {
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)?;
60-
wasi::add_to_linker::<T>(&mut ctx)?;
57+
wasi::add_to_linker(ctx)?;
6158
Ok(())
6259
}
6360

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,24 @@ use crate::{
2323
wasi_2023_10_18, wasi_2023_11_10, InstanceState, OutboundHttpFactor, SelfRequestOrigin,
2424
};
2525

26-
pub(crate) fn add_to_linker<T: Send + 'static>(
27-
ctx: &mut spin_factors::InitContext<T, OutboundHttpFactor>,
28-
) -> anyhow::Result<()> {
29-
fn type_annotate<T, F>(f: F) -> F
26+
pub(crate) fn add_to_linker<C>(ctx: &mut C) -> anyhow::Result<()>
27+
where
28+
C: spin_factors::InitContext<OutboundHttpFactor>,
29+
{
30+
fn get_http<C>(store: &mut C::StoreData) -> WasiHttpImpl<WasiHttpImplInner<'_>>
3031
where
31-
F: Fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner>,
32+
C: spin_factors::InitContext<OutboundHttpFactor>,
3233
{
33-
f
34-
}
35-
let get_data_with_table = ctx.get_data_with_table_fn();
36-
let closure = type_annotate(move |data| {
37-
let (state, table) = get_data_with_table(data);
34+
let (state, table) = C::get_data_with_table(store);
3835
WasiHttpImpl(IoImpl(WasiHttpImplInner { state, table }))
39-
});
36+
}
37+
let get_http = get_http::<C> as fn(&mut C::StoreData) -> WasiHttpImpl<WasiHttpImplInner<'_>>;
4038
let linker = ctx.linker();
41-
wasmtime_wasi_http::bindings::http::outgoing_handler::add_to_linker_get_host(linker, closure)?;
42-
wasmtime_wasi_http::bindings::http::types::add_to_linker_get_host(linker, closure)?;
39+
wasmtime_wasi_http::bindings::http::outgoing_handler::add_to_linker_get_host(linker, get_http)?;
40+
wasmtime_wasi_http::bindings::http::types::add_to_linker_get_host(linker, get_http)?;
4341

44-
wasi_2023_10_18::add_to_linker(linker, closure)?;
45-
wasi_2023_11_10::add_to_linker(linker, closure)?;
42+
wasi_2023_10_18::add_to_linker(linker, get_http)?;
43+
wasi_2023_11_10::add_to_linker(linker, get_http)?;
4644

4745
Ok(())
4846
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ use wasi::io::streams::{InputStream, OutputStream};
5454

5555
use crate::wasi::WasiHttpImplInner;
5656

57-
pub(crate) fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
57+
pub(crate) fn add_to_linker<T>(
58+
linker: &mut Linker<T>,
59+
closure: fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner<'_>>,
60+
) -> Result<()>
5861
where
59-
T: Send,
60-
F: Fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner> + Send + Sync + Copy + 'static,
62+
T: Send + 'static,
6163
{
6264
wasi::http::types::add_to_linker_get_host(linker, closure)?;
6365
wasi::http::outgoing_handler::add_to_linker_get_host(linker, closure)?;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ use wasi::io::streams::{Error as IoError, InputStream, OutputStream};
5959

6060
use crate::wasi::WasiHttpImplInner;
6161

62-
pub(crate) fn add_to_linker<T, F>(linker: &mut Linker<T>, closure: F) -> Result<()>
62+
pub(crate) fn add_to_linker<T>(
63+
linker: &mut Linker<T>,
64+
closure: fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner<'_>>,
65+
) -> Result<()>
6366
where
64-
T: Send,
65-
F: Fn(&mut T) -> WasiHttpImpl<WasiHttpImplInner> + Send + Sync + Copy + 'static,
67+
T: Send + 'static,
6668
{
6769
wasi::http::types::add_to_linker_get_host(linker, closure)?;
6870
wasi::http::outgoing_handler::add_to_linker_get_host(linker, closure)?;

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<T: Send + 'static>(
35-
&mut self,
36-
mut ctx: spin_factors::InitContext<T, Self>,
37-
) -> anyhow::Result<()> {
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {
1717
type AppState = ();
1818
type InstanceBuilder = InstanceState<C>;
1919

20-
fn init<T: Send + 'static>(&mut self, mut ctx: InitContext<T, Self>) -> anyhow::Result<()> {
20+
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()> {
2121
ctx.link_bindings(v1::add_to_linker)?;
2222
ctx.link_bindings(v2::add_to_linker)?;
2323
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<T: Send + 'static>(
21-
&mut self,
22-
mut ctx: spin_factors::InitContext<T, Self>,
23-
) -> anyhow::Result<()> {
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<T: Send + 'static>(
27-
&mut self,
28-
mut ctx: spin_factors::InitContext<T, Self>,
29-
) -> anyhow::Result<()> {
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(())

0 commit comments

Comments
 (0)