Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created February 22, 2019 09:58
Show Gist options
  • Select an option

  • Save rust-play/6a8e161d7920004fce9a0099dbf4156d to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/6a8e161d7920004fce9a0099dbf4156d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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