Change signature of from_async_fn to allow capturing the context#4215
Change signature of from_async_fn to allow capturing the context#4215
from_async_fn to allow capturing the context#4215Conversation
0e0fd32 to
f52e78e
Compare
Test262 conformance changes
|
|
Wow, the cool thing about this is that we can use non-desugared normal async functions: async fn get_record(
_this: &JsValue,
_args: &[JsValue],
context: &RefCell<&mut Context>,
) -> JsResult<JsValue> {
todo!()
} |
|
@Razican That's always been the case if you don't use the arguments inside the function. The moment you try to get any argument, the borrow checker complains that you're capturing a lifetime for too long. Also, I think this is only valid for edition 2021, because on edition 2024 all async functions capture the lifetime of all arguments by default. |
Hmmm interesting, I'm able to use it with edition 2024, using the arguments and so on: /// Gets a record from the database.
async fn get(
this: &JsValue,
args: &[JsValue],
context: &RefCell<&mut Context>,
) -> JsResult<JsValue> {
let sys_id = args.get_or_undefined(0).as_string().ok_or_else(|| {
JsNativeError::typ()
.with_message("you must specify a record ID to fetch from the database")
});
let realm = context.borrow().realm().clone();
todo!() // actually some more code that uses the host_defined() from realm and so on
} |
|
@Razican Ohhhh, maybe rustc changed the desugaring of async functions? |
HalidOdat
left a comment
There was a problem hiding this comment.
Looks great! This greatly improves the API's ergonomics. :)
Razican
left a comment
There was a problem hiding this comment.
This is really great :) I also saw some comments in Matrix about possibly hiding the RefCell and have Context use interior mutability and cheap cloning, but for now, this improves the API quite a lot.
This allows carrying the
contextthrough await points, making it much more ergonomic to use for big async functions.