Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(unboxed_closures, fn_traits)]
- use std::future::Future;
- fn main() {
- let mut conn = Connection;
- conn.transaction(|conn| async move {
- conn.execute("Hi").await?;
- conn.execute("Hi2").await
- });
- }
- struct Connection;
- impl Connection {
- async fn execute(&mut self, s: &str) -> Result<(), ()> {
- println!("{}", s);
- Ok(())
- }
- async fn transaction<F, T>(&mut self, f: F) -> Result<T, ()>
- where
- for<'a> F: FnOnce<(&'a mut Self,)>,
- for<'a> <F as FnOnce<(&'a mut Self,)>>::Output: Future<Output = Result<T, ()>>,
- {
- self.execute("BEGIN").await?;
- let res = f(self).await;
- if res.is_ok() {
- self.execute("COMMIT").await?;
- } else {
- self.execute("ROLLBACK").await?;
- }
- res
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement