mirror of
https://github.com/element-hq/synapse.git
synced 2026-07-22 17:32:39 +00:00
Explain possible better future for async fn that need to be Send
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user