-
-
Save jpastuszek/49c189fcc4c0f3ccb4b89290be2d34ee to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
| use std::io; | |
| struct Connection; | |
| struct Handle<'c>(&'c Connection); | |
| struct Statement<'c>(&'c Connection); | |
| struct Rows<'c, 'l>(Statement<'c>, &'l mut Handle<'c>); | |
| impl<'c> Handle<'c> { | |
| fn statement<'h>(&'h mut self,) -> Statement<'c> { | |
| Statement(self.0) | |
| } | |
| fn exec<'h>(&'h mut self, statement: Statement<'c>) -> Rows<'c, 'h> { | |
| Rows(statement, self) | |
| } | |
| } | |
| /* | |
| * crate any number of statements | |
| * run one query at a time | |
| * can't crate statement if query result is not consumed | |
| * can't run query if query result is not consumed | |
| So query result has to lock connection. But we need to have any number statements borrowing it before executing query. | |
| */ | |
| fn main() { | |
| let connection = Connection; | |
| let mut handle = Handle(&connection); | |
| let s1 = handle.statement(); | |
| let r1 = handle.exec(s1); | |
| let s2 = handle.statement(); | |
| let r2 = handle.exec(s2); | |
| drop(r1); | |
| drop(r2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment