Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #![feature(unboxed_closures, fn_traits)]
  2.  
  3. use std::future::Future;
  4.  
  5. fn main() {
  6. let mut conn = Connection;
  7. conn.transaction(|conn| async move {
  8. conn.execute("Hi").await?;
  9. conn.execute("Hi2").await
  10. });
  11. }
  12.  
  13. struct Connection;
  14.  
  15. impl Connection {
  16. async fn execute(&mut self, s: &str) -> Result<(), ()> {
  17. println!("{}", s);
  18. Ok(())
  19. }
  20.  
  21. async fn transaction<F, T>(&mut self, f: F) -> Result<T, ()>
  22. where
  23. for<'a> F: FnOnce<(&'a mut Self,)>,
  24. for<'a> <F as FnOnce<(&'a mut Self,)>>::Output: Future<Output = Result<T, ()>>,
  25. {
  26. self.execute("BEGIN").await?;
  27. let res = f(self).await;
  28. if res.is_ok() {
  29. self.execute("COMMIT").await?;
  30. } else {
  31. self.execute("ROLLBACK").await?;
  32. }
  33. res
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement