Explain possible better future for async fn that need to be Send

This commit is contained in:
Eric Eastwood
2026-06-22 16:47:31 -05:00
parent 1d6eb1e524
commit 4eec02ce00
2 changed files with 30 additions and 1 deletions
+29
View File
@@ -33,8 +33,37 @@ pub trait DatabasePool: Send + Sync {
/// Starts a transaction on the database and runs the given function,
/// returning its result.
///
/// `name` should be a descriptive identifier for logging/metrics
///
/// `func` may be called multiple times under certain failure modes (like
/// serialization and deadlock errors), so it is `Fn` rather than `FnOnce`.
///
/// Usage:
/// ```rust
/// db_pool
/// .run_interaction(|txn| {
/// async move {
/// /* do stuff with txn */
/// }
/// .boxed()
/// })
/// ```
//
// Ideally, this method signature would be slightly different to allow downstream
// usage to look like the following (simpler) but because allow the work to happen
// on other threads, the `Future` needs to be `Send`; As of 2026-06-22, the
// `AsyncFn` trait doesn't have a clean way to express that "the future this async
// closure produces is `Send`."
// ```
// db_pool.run_interaction("description", async move |txn| {
// /* do stuff with txn */
// })
// ```
//
// Refs:
// - [RFC 3668: Async closures](https://github.com/rust-lang/rfcs/pull/3668)
// - [RFC 3654: Return Type Notation](https://github.com/rust-lang/rfcs/pull/3654)
// - [Tracking Issue for return type notation](https://github.com/rust-lang/rust/issues/109417)
fn run_interaction<R, F>(
&self,
name: &'static str,
+1 -1
View File
@@ -63,7 +63,7 @@ impl<P: DatabasePool> Store<P> {
//
// Owned copies so the callback can be `'static` (it may be moved to
// another thread and called multiple times under retries).
let user_id = user_id.to_string();
let user_id = user_id.to_owned();
let feature = feature.to_string();
let is_feature_enabled_for_user = self