rusqlite/src/load_extension_guard.rs

47 lines
1.3 KiB
Rust
Raw Normal View History

2018-10-31 03:11:35 +08:00
use crate::{Connection, Result};
2021-09-06 17:49:29 +08:00
/// 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<()> {
2021-09-06 17:49:29 +08:00
/// unsafe {
/// let _guard = LoadExtensionGuard::new(conn)?;
/// conn.load_extension("trusted/sqlite/extension", None)
/// }
/// }
/// ```
2021-06-13 15:17:35 +08:00
#[cfg_attr(docsrs, doc(cfg(feature = "load_extension")))]
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.
2021-09-06 17:49:29 +08:00
///
/// # Safety
///
/// You must not run untrusted queries while extension loading is enabled.
///
/// See the safety comment on [`Connection::load_extension_enable`] for more
/// details.
#[inline]
2021-09-06 17:49:29 +08:00
pub unsafe 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<'_> {
#[inline]
fn drop(&mut self) {
self.conn.load_extension_disable();
}
}