Skip to content

POC on non static callback in TransactionBuilder::run #7

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,8 @@ impl<Err> TransactionBuilder<Err> {
// untested and unsupported code path.
pub async fn run<Ret>(
self,
transaction: impl 'static + AsyncFnOnce(Transaction<Err>) -> crate::Result<Ret, Err>,
transaction: impl AsyncFnOnce(Transaction<Err>) -> crate::Result<Ret, Err>,
) -> crate::Result<Ret, Err>
where
Ret: 'static,
Err: 'static,
{
let t = self
.db
Expand All @@ -131,7 +128,19 @@ impl<Err> TransactionBuilder<Err> {
return_value
}
};
unsafe_jar::run(t, fut);

let fut_pin = std::pin::pin!(fut);
let fut_pin_with_dyn_dispatch: std::pin::Pin<&mut dyn std::future::Future<Output=Result<(), ()>>> = fut_pin;
// SAFETY: this is fine as long as we don't return from the current function since
// the future is pinned here.
let fut_pin_with_dyn_dispatch_and_static_lifetime: std::pin::Pin<&'static mut dyn std::future::Future<Output=Result<(), ()>>> = unsafe { std::mem::transmute(fut_pin_with_dyn_dispatch) };
// TODO: A possible safety could be added to ensure the future is not polled after
// the current function has returned (which would cause UB).
// The idea would be to check `tx.is_cancelled()` is false before polling the
// `Pin<&dyn Future> reference on the future (since this check indicates that `rx`
// still exists, which itself lives in this function just like our pinned future).

unsafe_jar::run(t, fut_pin_with_dyn_dispatch_and_static_lifetime);
let res = rx.await;
if unsafe_jar::POLLED_FORBIDDEN_THING.get() {
panic!("Transaction blocked without any request under way");
Expand Down
22 changes: 22 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ async fn smoke_test() {
})
.await
.unwrap();

// Run a non-static async function
let key = "key2".to_string();
let value = "value2".to_string();
let key_ref = key.as_ref();
let value_ref = value.as_ref();

db.transaction(&["objects"])
.rw()
.run(async move |t| {
let objects = t.object_store("objects")?;

objects
.add_kv(&JsString::from(key_ref), &JsString::from(value_ref))
.await?;
assert_eq!(
objects.get(&JsString::from(key_ref)).await?.unwrap(),
**JsString::from(value_ref)
);

Ok(())
}).await.unwrap();
}

#[wasm_bindgen_test]
Expand Down