mirror of
https://github.com/isar/rusqlite.git
synced 2024-10-23 23:21:09 +08:00
Introduce two distinct lifetime parameters.
This commit is contained in:
parent
86ffc24bb9
commit
7bdf80ccdb
24
src/cache.rs
24
src/cache.rs
@ -12,27 +12,27 @@ pub struct StatementCache<'conn> {
|
|||||||
cache: RefCell<VecDeque<Statement<'conn>>>, // back = LRU
|
cache: RefCell<VecDeque<Statement<'conn>>>, // back = LRU
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CachedStatement<'conn> {
|
pub struct CachedStatement<'c: 's, 's> {
|
||||||
stmt: Option<Statement<'conn>>,
|
stmt: Option<Statement<'c>>,
|
||||||
cache: &'conn StatementCache<'conn>,
|
cache: &'s StatementCache<'c>,
|
||||||
pub cacheable: bool,
|
pub cacheable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> Deref for CachedStatement<'conn> {
|
impl<'c, 's> Deref for CachedStatement<'c, 's> {
|
||||||
type Target = Statement<'conn>;
|
type Target = Statement<'c>;
|
||||||
|
|
||||||
fn deref(&self) -> &Statement<'conn> {
|
fn deref(&self) -> &Statement<'c> {
|
||||||
self.stmt.as_ref().unwrap()
|
self.stmt.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> DerefMut for CachedStatement<'conn> {
|
impl<'c, 's> DerefMut for CachedStatement<'c, 's> {
|
||||||
fn deref_mut(&mut self) -> &mut Statement<'conn> {
|
fn deref_mut(&mut self) -> &mut Statement<'c> {
|
||||||
self.stmt.as_mut().unwrap()
|
self.stmt.as_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> Drop for CachedStatement<'conn> {
|
impl<'c, 's> Drop for CachedStatement<'c, 's> {
|
||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if self.cacheable {
|
if self.cacheable {
|
||||||
@ -43,8 +43,8 @@ impl<'conn> Drop for CachedStatement<'conn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'conn> CachedStatement<'conn> {
|
impl<'c, 's> CachedStatement<'c, 's> {
|
||||||
fn new(stmt: Statement<'conn>, cache: &'conn StatementCache<'conn>) -> CachedStatement<'conn> {
|
fn new(stmt: Statement<'c>, cache: &'s StatementCache<'c>) -> CachedStatement<'c, 's> {
|
||||||
CachedStatement {
|
CachedStatement {
|
||||||
stmt: Some(stmt),
|
stmt: Some(stmt),
|
||||||
cache: cache,
|
cache: cache,
|
||||||
@ -68,7 +68,7 @@ impl<'conn> StatementCache<'conn> {
|
|||||||
/// # Failure
|
/// # Failure
|
||||||
///
|
///
|
||||||
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails.
|
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails.
|
||||||
pub fn get(&'conn self, sql: &str) -> Result<CachedStatement<'conn>> {
|
pub fn get<'s>(&'s self, sql: &str) -> Result<CachedStatement<'conn, 's>> {
|
||||||
let stmt = match self.cache.borrow().iter().rposition(|entry| entry.eq(sql)) {
|
let stmt = match self.cache.borrow().iter().rposition(|entry| entry.eq(sql)) {
|
||||||
Some(index) => Ok(self.cache.borrow_mut().swap_remove_front(index).unwrap()), // FIXME Not LRU compliant
|
Some(index) => Ok(self.cache.borrow_mut().swap_remove_front(index).unwrap()), // FIXME Not LRU compliant
|
||||||
_ => self.conn.prepare(sql),
|
_ => self.conn.prepare(sql),
|
||||||
|
Loading…
Reference in New Issue
Block a user