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

Allow returning Result<*mut T, _> and Result<*const T, _> from async functions #3059

Merged
merged 2 commits into from
Nov 21, 2022
Merged

Allow returning Result<*mut T, _> and Result<*const T, _> from async functions #3059

merged 2 commits into from
Nov 21, 2022

Conversation

ThomasMarches
Copy link
Contributor

@ThomasMarches ThomasMarches commented Sep 2, 2022

The following code does not work currently, the goal of this PR is to fix it.

#[wasm_bindgen]
pub async fn foo() -> Result<*mut (), JsError> {
     Ok(std::ptr::null())
}

The base error is:

the trait bound `std::result::Result<*mut T, _>: wasm_bindgen::__rt::IntoJsResult` is not satisfied
the following other types implement trait `wasm_bindgen::__rt::IntoJsResult`:
  std::result::Result<(), E>
  std::result::Result<T, E>

The same code does compile using a sync function.

Comment on lines +763 to +793
impl<T> From<*mut T> for JsValue {
#[inline]
fn from(s: *mut T) -> JsValue {
JsValue::from(s as usize)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be implemented for *const T as well.

@ThomasMarches ThomasMarches changed the title Allow returning Result<*mut T, _> from async functions Allow returning Result<*mut T, _> and Result<*const T, _> from async functions Sep 3, 2022
@Liamolucko
Copy link
Collaborator

You need to update crates/macro/ui-tests/async-errors.stderr to fix CI:

diff --git a/crates/macro/ui-tests/async-errors.stderr b/crates/macro/ui-tests/async-errors.stderr
index 492492a1..f4ebb478 100644
--- a/crates/macro/ui-tests/async-errors.stderr
+++ b/crates/macro/ui-tests/async-errors.stderr
@@ -40,8 +40,8 @@ error[E0277]: the trait bound `wasm_bindgen::JsValue: From<BadType>` is not sati
                <wasm_bindgen::JsValue as From<&'a String>>
                <wasm_bindgen::JsValue as From<&'a T>>
                <wasm_bindgen::JsValue as From<&'a str>>
-               <wasm_bindgen::JsValue as From<JsError>>
-             and 73 others
+               <wasm_bindgen::JsValue as From<*const T>>
+             and 75 others
      = note: required because of the requirements on the impl of `Into<wasm_bindgen::JsValue>` for `BadType`
      = note: required because of the requirements on the impl of `IntoJsResult` for `BadType`
 note: required by `into_js_result`

ThomasMarches and others added 2 commits November 21, 2022 03:01
- implemented `From<*mut T>` for `JsValue`

- implemented `From<*const T>` for `JsValue`

- update CI stderr
@Liamolucko Liamolucko merged commit 5597988 into rustwasm:main Nov 21, 2022
daxpedda pushed a commit to daxpedda/wasm-bindgen that referenced this pull request Nov 27, 2022
daxpedda pushed a commit to daxpedda/wasm-bindgen that referenced this pull request Jan 13, 2023
daxpedda pushed a commit to daxpedda/wasm-bindgen that referenced this pull request Jan 19, 2023
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.

3 participants