Skip to content

Refactor how InitContext works with factors #3134

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

Merged
merged 2 commits into from
May 20, 2025

Conversation

alexcrichton
Copy link
Contributor

This commit is a refactoring to replace the concrete InitContext structure with a generic type parameter instead. The motivation for this is to prepare for handling bytecodealliance/wasmtime#10770. That's a big change to how add_to_linker functions work, notably that the argument to the generated functions is a fn, not a F: Fn. WASI factors currently rely on the closure-like nature this argument which means it's not compatible with that change. The refactoring to use a trait InitContext here enables plumbing a type parameter through a function to be able to get a function pointer without relying on closures.

@alexcrichton alexcrichton force-pushed the refactor-for-has-data branch from 005adbf to ec308bb Compare May 12, 2025 09:02
Copy link
Contributor

@fibonacci1729 fibonacci1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM but i'll defer to someone with a bit more of an eye for the meat and potatoes.

@fibonacci1729 fibonacci1729 requested review from lann and dicej May 14, 2025 18:43
get_data_with_table,
}
}
pub trait InitContext<F: Factor> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously avoided F: Factor since F: Fn is so common, but I was always on the fence about it. 🤷

self.get_data
/// Get the instance state for this factor from the store's state.
fn get_data(store: &mut Self::StoreData) -> &mut FactorInstanceState<F> {
Self::get_data_with_table(store).0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I guess that makes sense 🙂

Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, I lost my only substantive comment to the depths of "didn't click Add".

Comment on lines 34 to 36
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
where
C: InitContext<Self>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an impl Trait?

Suggested change
fn init<C>(&mut self, ctx: &mut C) -> anyhow::Result<()>
where
C: InitContext<Self>,
fn init(&mut self, ctx: &mut impl InitContext<Self>) -> anyhow::Result<()>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wanted to do this, but it causes issues here for example where the type parameter is being used to monomorphize a new function pointer. There's nothing stopping me though from taking that init and moving it to its own function which has a type parameter and otherwise using impl InitContext everywhere else, however. I wasn't sure how often it'd be necessary to get C for something, and so far it's just WASI but even some of the WASI bits are already in separate functions which can use a parameter instead of an impl Trait.

Would you prefer the impl Trait form for keeping the factor trait relatively clean? I'm kind of on the fence myself but happy to implement either method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Yeah, just seemed like it would be less noisy to read but I don't feel strongly about it here.

You seem to be on team "always where clause" which is understandable but we could also fn init<C: InitContext<Self>>. 🤷

Anyway we can merge whenever you'd like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh personally I don't have much of a preference. When using impl Trait vs T: Trait in a trait itself the Rust compiler ensures that downstream impls all use the same style. For example if the Factor trait used &mut impl InitContext<Self> then all downstream impls must also do so, they can't do C: InitContext<Self>. For inline-bounds vs where clauses, however, there's no such restriction. The trait can use C: InitContext<Self> and impls can use where C: InitContext<Self>. I'm definitely stylistically on the side of preferring where clauses, but that's purely a stylistic preference and I'm happy to change.

I wanted to see what this felt like though so I went ahead and implemented the change with &mut impl InitContext<Self>. I swtiched up the style for how WASI bindings works and I'm liking how it turned out. Mind taking a second look? (no more type parameters or where clauses mostly)

@alexcrichton alexcrichton force-pushed the refactor-for-has-data branch from cbbc2db to 307da24 Compare May 18, 2025 16:14
@lann
Copy link
Collaborator

lann commented May 18, 2025

I'll take a look tomorrow. Good to merge?

@alexcrichton
Copy link
Contributor Author

Indeed yeah should be good to go

@lann
Copy link
Collaborator

lann commented May 19, 2025

Looks like 307da24 isn't signed.

This commit is a refactoring to replace the concrete `InitContext`
structure with a generic type parameter instead. The motivation for this
is to prepare for handling bytecodealliance/wasmtime#10770. That's a big
change to how `add_to_linker` functions work, notably that the argument
to the generated functions is a `fn`, not a `F: Fn`. WASI factors
currently rely on the closure-like nature this argument which means it's
not compatible with that change. The refactoring to use a `trait
InitContext` here enables plumbing a type parameter through a function
to be able to get a function pointer without relying on closures.

Signed-off-by: Alex Crichton <[email protected]>
@alexcrichton alexcrichton force-pushed the refactor-for-has-data branch from 872b5f6 to 11ed60a Compare May 19, 2025 13:15
@alexcrichton
Copy link
Contributor Author

One day I'll set up auto-signing...

@alexcrichton
Copy link
Contributor Author

If y'all are waiting on me to merge, mind merging for me? (I don't have The Button)

If you're waiting to get a chance to review again, no worries!

@lann lann merged commit b7e1a22 into spinframework:main May 20, 2025
17 checks passed
@lann lann mentioned this pull request May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants