rusqlite/src/load_extension_guard.rs

36 lines
1007 B
Rust
Raw Normal View History

2018-10-31 03:11:35 +08:00
use crate::{Connection, Result};
/// RAII guard temporarily enabling SQLite extensions to be loaded.
///
/// ## Example
///
/// ```rust,no_run
/// # use rusqlite::{Connection, Result, LoadExtensionGuard};
/// # use std::path::{Path};
2015-12-13 03:06:03 +08:00
/// fn load_my_extension(conn: &Connection) -> Result<()> {
2018-10-31 03:11:35 +08:00
/// let _guard = LoadExtensionGuard::new(conn)?;
///
/// conn.load_extension(Path::new("my_sqlite_extension"), None)
/// }
/// ```
pub struct LoadExtensionGuard<'conn> {
conn: &'conn Connection,
}
2019-02-03 18:02:38 +08:00
impl LoadExtensionGuard<'_> {
2018-08-17 00:29:46 +08:00
/// Attempt to enable loading extensions. Loading extensions will be
/// disabled when this guard goes out of scope. Cannot be meaningfully
/// nested.
2018-12-08 04:57:04 +08:00
pub fn new(conn: &Connection) -> Result<LoadExtensionGuard<'_>> {
2017-04-08 01:43:24 +08:00
conn.load_extension_enable()
2018-05-05 01:55:55 +08:00
.map(|_| LoadExtensionGuard { conn })
}
}
#[allow(unused_must_use)]
2019-02-03 18:02:38 +08:00
impl Drop for LoadExtensionGuard<'_> {
fn drop(&mut self) {
self.conn.load_extension_disable();
}
}