workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

[Feature] Add #[worker::trait_send] for async_trait

Open Enduriel opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

This is an ergonomics problem for when I have to implement a trait using async_trait when passing JsValues, right now in order to make it work I have to do something like

struct MyStruct {
	db: D1Database
}

#[worker::send]
async fn do_foo(value: &MyStruct) {
	// run logic
}

#[async_trait::async_trait]
impl SomeAsyncTrait for MyStruct {
	fn foo(&self) {
		do_foo(self).await;
	}
}

Where it would be much nicer and more readable to be able to do something like this, which should probably be behind an async_trait feature flag

struct MyStruct {
	db: D1Database
}

#[async_trait::async_trait]
impl SomeAsyncTrait for MyStruct {
	#[worker::trait_send]
	async fn foo(&self) {
		// run logic
	}
}

I have next to no experience in writing Rust macros, and at a quick glance there's more going on here than I'd be easily able to dig through and understand with my lacking experience, so I hope someone picks this up since we can now easily use axum-login which requires implementing async traits

Enduriel avatar Apr 11 '24 13:04 Enduriel