Last active
February 24, 2022 14:19
-
-
Save stellasia/601316e54ead6507008e302f4e96af6a to your computer and use it in GitHub Desktop.
Medium_From_Python_To_Rust_Transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Transaction: | |
| def execute_query(self, query: str): | |
| # run query | |
| pass | |
| def close(self): | |
| # close transaction | |
| pass | |
| def do_stuff(tx: Transaction): | |
| # do stuff | |
| # commit or rollback | |
| tx.close() | |
| def main(): | |
| tx = Transaction() | |
| do_stuff(tx) | |
| # will raise error at Runtime, but nothing prevents you from writting the following line: | |
| tx.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Transaction {} | |
| impl Transaction { | |
| pub fn execute_query(&self, query: &str) { | |
| // function "borrows" self => transaction is rendered to main thread | |
| // run query | |
| } | |
| pub fn close(self) { | |
| // function "takes ownership" of self, which does not exist anymore in main thread! | |
| } | |
| } | |
| fn do_stuff(tx: Transaction) { | |
| // do stuff | |
| // function takes ownership of "tx" | |
| tx.close(); | |
| } | |
| fn main() { | |
| tx = Transaction {} ; | |
| do_stuff(tx); | |
| // tx is not useable anymore here | |
| // tx.close() // => will raise error at compilation! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment