Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit e7bbbd0

Browse files
authored
Merge pull request #158 from alexcrichton/accessor-refactor
Replace `GetHost` with a function pointer
2 parents be31d9d + cecb7fd commit e7bbbd0

172 files changed

Lines changed: 6851 additions & 20874 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/component-macro/tests/expanded/char.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,16 @@ const _: () = {
144144
let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
145145
indices.load(&mut store, instance)
146146
}
147-
pub fn add_to_linker<T, U>(
147+
pub fn add_to_linker<T, D>(
148148
linker: &mut wasmtime::component::Linker<T>,
149-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
149+
host_getter: fn(&mut T) -> D::Data<'_>,
150150
) -> wasmtime::Result<()>
151151
where
152-
U: foo::foo::chars::Host,
152+
D: wasmtime::component::HasData,
153+
for<'a> D::Data<'a>: foo::foo::chars::Host,
154+
T: 'static,
153155
{
154-
foo::foo::chars::add_to_linker(linker, get)?;
156+
foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;
155157
Ok(())
156158
}
157159
pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {
@@ -171,22 +173,25 @@ pub mod foo {
171173
/// A function that returns a character
172174
fn return_char(&mut self) -> char;
173175
}
174-
pub trait GetHost<
175-
T,
176-
>: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
177-
type Host: Host;
176+
impl<_T: Host> Host for &mut _T {
177+
/// A function that accepts a character
178+
fn take_char(&mut self, x: char) -> () {
179+
Host::take_char(*self, x)
180+
}
181+
/// A function that returns a character
182+
fn return_char(&mut self) -> char {
183+
Host::return_char(*self)
184+
}
178185
}
179-
impl<F, T, O> GetHost<T> for F
186+
pub fn add_to_linker<T, D>(
187+
linker: &mut wasmtime::component::Linker<T>,
188+
host_getter: fn(&mut T) -> D::Data<'_>,
189+
) -> wasmtime::Result<()>
180190
where
181-
F: Fn(T) -> O + Send + Sync + Copy + 'static,
182-
O: Host,
191+
D: wasmtime::component::HasData,
192+
for<'a> D::Data<'a>: Host,
193+
T: 'static,
183194
{
184-
type Host = O;
185-
}
186-
pub fn add_to_linker_get_host<T, G: for<'a> GetHost<&'a mut T, Host: Host>>(
187-
linker: &mut wasmtime::component::Linker<T>,
188-
host_getter: G,
189-
) -> wasmtime::Result<()> {
190195
let mut inst = linker.instance("foo:foo/chars")?;
191196
inst.func_wrap(
192197
"take-char",
@@ -209,25 +214,6 @@ pub mod foo {
209214
)?;
210215
Ok(())
211216
}
212-
pub fn add_to_linker<T, U>(
213-
linker: &mut wasmtime::component::Linker<T>,
214-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
215-
) -> wasmtime::Result<()>
216-
where
217-
U: Host,
218-
{
219-
add_to_linker_get_host(linker, get)
220-
}
221-
impl<_T: Host + ?Sized> Host for &mut _T {
222-
/// A function that accepts a character
223-
fn take_char(&mut self, x: char) -> () {
224-
Host::take_char(*self, x)
225-
}
226-
/// A function that returns a character
227-
fn return_char(&mut self) -> char {
228-
Host::return_char(*self)
229-
}
230-
}
231217
}
232218
}
233219
}

crates/component-macro/tests/expanded/char_async.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ const _: () = {
150150
let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
151151
indices.load(&mut store, instance)
152152
}
153-
pub fn add_to_linker<T, U>(
153+
pub fn add_to_linker<T, D>(
154154
linker: &mut wasmtime::component::Linker<T>,
155-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
155+
host_getter: fn(&mut T) -> D::Data<'_>,
156156
) -> wasmtime::Result<()>
157157
where
158-
T: Send,
159-
U: foo::foo::chars::Host + Send,
158+
D: wasmtime::component::HasData,
159+
for<'a> D::Data<'a>: foo::foo::chars::Host + Send,
160+
T: 'static + Send,
160161
{
161-
foo::foo::chars::add_to_linker(linker, get)?;
162+
foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;
162163
Ok(())
163164
}
164165
pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {
@@ -179,27 +180,24 @@ pub mod foo {
179180
/// A function that returns a character
180181
async fn return_char(&mut self) -> char;
181182
}
182-
pub trait GetHost<
183-
T,
184-
>: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
185-
type Host: Host + Send;
186-
}
187-
impl<F, T, O> GetHost<T> for F
188-
where
189-
F: Fn(T) -> O + Send + Sync + Copy + 'static,
190-
O: Host + Send,
191-
{
192-
type Host = O;
183+
impl<_T: Host + Send> Host for &mut _T {
184+
/// A function that accepts a character
185+
async fn take_char(&mut self, x: char) -> () {
186+
Host::take_char(*self, x).await
187+
}
188+
/// A function that returns a character
189+
async fn return_char(&mut self) -> char {
190+
Host::return_char(*self).await
191+
}
193192
}
194-
pub fn add_to_linker_get_host<
195-
T,
196-
G: for<'a> GetHost<&'a mut T, Host: Host + Send>,
197-
>(
193+
pub fn add_to_linker<T, D>(
198194
linker: &mut wasmtime::component::Linker<T>,
199-
host_getter: G,
195+
host_getter: fn(&mut T) -> D::Data<'_>,
200196
) -> wasmtime::Result<()>
201197
where
202-
T: Send,
198+
D: wasmtime::component::HasData,
199+
for<'a> D::Data<'a>: Host,
200+
T: 'static + Send,
203201
{
204202
let mut inst = linker.instance("foo:foo/chars")?;
205203
inst.func_wrap_async(
@@ -227,26 +225,6 @@ pub mod foo {
227225
)?;
228226
Ok(())
229227
}
230-
pub fn add_to_linker<T, U>(
231-
linker: &mut wasmtime::component::Linker<T>,
232-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
233-
) -> wasmtime::Result<()>
234-
where
235-
U: Host + Send,
236-
T: Send,
237-
{
238-
add_to_linker_get_host(linker, get)
239-
}
240-
impl<_T: Host + ?Sized + Send> Host for &mut _T {
241-
/// A function that accepts a character
242-
async fn take_char(&mut self, x: char) -> () {
243-
Host::take_char(*self, x).await
244-
}
245-
/// A function that returns a character
246-
async fn return_char(&mut self) -> char {
247-
Host::return_char(*self).await
248-
}
249-
}
250228
}
251229
}
252230
}

0 commit comments

Comments
 (0)