Skip to content
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

Support Result<(), impl Debug> in #[wasm_bindgen(start)] #3263

Closed
wants to merge 1 commit into from

Conversation

daxpedda
Copy link
Collaborator

@daxpedda daxpedda commented Jan 24, 2023

This adds support to #[wasm_bindgen(start)] to support more types closer to what Rusts fn main() supports while not changing the current behavior of throwing JsValues when available.

Looking at Termination, which is what fn main() uses, I added support for Infallible and Result<(), impl Debug>.
I intentionally left ExitCode unaddressed because I don't know exactly what to throw in that case.

I contemplated introducing a new trait, like JsTermination to mirror exactly what Rust does, but that's not really compatible with the current way #[wasm_bindgen(start)] works. We could however introduce it with a new #[wasm_bindgen(main)] if that is desired? See #3252 for a similar proposal.

The way this problem was solved is by using Autoref-based specialization, see this case study by dtolnay, which is sometimes used to solve this kind of problem in macros.

Fixes #3262.

@ranile
Copy link
Collaborator

ranile commented Jan 25, 2023

What's the use case for this? I don't really see how impl Debug is better than String. Anything that's impl Debug can be trivially converted to a string so I'm not really sure about adding support for it

@daxpedda
Copy link
Collaborator Author

daxpedda commented Jan 25, 2023

The use case is cross-platform compatibility. See #3262.

The reason why usually people use fn main() -> Result<(), anyhow::Error>, or similar, is because it can be used to casually return any error type. You can't use fn main() -> Result<(), String> and return any error, you would have to write a .map_err() conversion for every single ?.

I didn't pick Debug randomly, it is the chosen trait by Rust to return from fn main(), see Termination. So all I'm trying is to make fn main() as compatible as possible between wasm-bindgen and Rust.

@daxpedda
Copy link
Collaborator Author

See also #2994 and #2970.

@daxpedda
Copy link
Collaborator Author

See #3252 (comment).

I will introduce a new attribute that is supposed to only be used with fn main().

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.

Support more types in Result<(), E> for #[wasm_bindgen(start)]
2 participants