-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GetHost
to generated bindings for more flexible linking
#8448
Conversation
GetHost
to generated bindings for more flexible linking
9581950
to
3ed3078
Compare
Tracking for myself: next step is to update the top-level world linking: add a e.g. |
@alexcrichton
I'm just going to drop the |
Is that actually the type parameter |
Ah, you are correct. That explains why I couldn't figure out where this would be happening... |
This has run into a bit of a snag. rust-lang/rust#124984 shows code this PR was going to generate which compiled with Rust 1.77.0 but does not compile with Rust 1.78.0. |
Use <https://users.rust-lang.org/t/generic-closure-returns-that-can-capture-arguments/76513/3> as a guide of how to implement this by making the `GetHost` trait a bit uglier.
Also enable this for WASI crates since they do their own thing with `WasiView` for now. A future refactoring should be able to remove this option entirely and switch wasi crates to a new design of `WasiView`.
Ok I think most tests should be passing now. A summary of changes now at its current state: Previously pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> wasmtime::Result<()>
where
U: Host, Now it generates: pub trait GetHost<
T,
>: Fn(T) -> <Self as GetHost<T>>::Output + Send + Sync + Copy + 'static {
type Output: Host;
}
impl<F, T, O> GetHost<T> for F
where
F: Fn(T) -> O + Send + Sync + Copy + 'static,
O: Host,
{
type Output = O;
}
pub fn add_to_linker_get_host<T>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: impl for<'a> GetHost<&'a mut T>,
) -> wasmtime::Result<()> {
// ...
}
pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> wasmtime::Result<()>
where
U: Host,
{
add_to_linker_get_host(linker, get)
} Effectively the old fn type_annotate<T, F>(val: F) -> F
where
F: Fn(&mut T) -> &mut T,
{
val
}
let closure = type_annotate::<T, _>(|t| t); Usage of I would like to, in a future commit I want to refactor all of wasmtime-wasi{,-http} to remove |
I should also say there are other minor details here and there but the comment above is the main thrust of the change, everything else is "smith a few things until everything compiles again" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: we should pick a small subset of these tests with good feature coverage to expand. As-is these are useful for review but its hard to know how many/which files to review before stopping, and having so many changed files screws with Github's UI...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point yeah I've been a bit sad that the actual changes were all hidden by default as well...
This commit builds on the support from bytecodealliance#8448 to remove all blanket impls from the WASI crates and instead replace them with concrete impls. This is slightly functionally different from before where impls are now on trait objects meaning dynamic dispatch is involved where previously dynamic dispatch was used. That being said the perf hit here is expected to be negligible-to-nonexistent since the implementations are large enough that the dynamic dispatch won't be the hot path. The motivations for this commit are: * Removes the need for an odd `skip_mut_forwarding_impls` option - but this'll be left for a bit in case others need it. * Improves incremental compile time of these crates where the crates themselves now contain all object code for all of WASI instead of forcing the final consume to codegen everything (although there's still a significant amount monomorphized). * Improves future compatibility with refactorings of bindgen-generated-traits and such because blanket impls are pretty hard to work around where concrete impls are easier to reason about (and document).
This commit builds on the support from #8448 to remove all blanket impls from the WASI crates and instead replace them with concrete impls. This is slightly functionally different from before where impls are now on trait objects meaning dynamic dispatch is involved where previously dynamic dispatch was used. That being said the perf hit here is expected to be negligible-to-nonexistent since the implementations are large enough that the dynamic dispatch won't be the hot path. The motivations for this commit are: * Removes the need for an odd `skip_mut_forwarding_impls` option - but this'll be left for a bit in case others need it. * Improves incremental compile time of these crates where the crates themselves now contain all object code for all of WASI instead of forcing the final consume to codegen everything (although there's still a significant amount monomorphized). * Improves future compatibility with refactorings of bindgen-generated-traits and such because blanket impls are pretty hard to work around where concrete impls are easier to reason about (and document).
…dealliance#8448) * Remove unused generated `add_root_to_linker` method * WIP: bindgen GetHost * Compile with Rust 1.78+ Use <https://users.rust-lang.org/t/generic-closure-returns-that-can-capture-arguments/76513/3> as a guide of how to implement this by making the `GetHost` trait a bit uglier. * Add an option to skip `&mut T -> T` impls Also enable this for WASI crates since they do their own thing with `WasiView` for now. A future refactoring should be able to remove this option entirely and switch wasi crates to a new design of `WasiView`. * Update test expectations * Review comments * Undo temporary change * Handle some TODOs * Remove no-longer-relevant note * Fix wasmtime-wasi-http doc link --------- Co-authored-by: Alex Crichton <[email protected]>
…#8620) * Remove unused generated `add_root_to_linker` method * WIP: bindgen GetHost * Compile with Rust 1.78+ Use <https://users.rust-lang.org/t/generic-closure-returns-that-can-capture-arguments/76513/3> as a guide of how to implement this by making the `GetHost` trait a bit uglier. * Add an option to skip `&mut T -> T` impls Also enable this for WASI crates since they do their own thing with `WasiView` for now. A future refactoring should be able to remove this option entirely and switch wasi crates to a new design of `WasiView`. * Update test expectations * Review comments * Undo temporary change * Handle some TODOs * Remove no-longer-relevant note * Fix wasmtime-wasi-http doc link --------- Co-authored-by: Lann <[email protected]>
Work in progress for #8382