Advertisement
Guest User

Untitled

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