1 use crate::{Connection, Result};
2 
3 /// RAII guard temporarily enabling SQLite extensions to be loaded.
4 ///
5 /// ## Example
6 ///
7 /// ```rust,no_run
8 /// # use rusqlite::{Connection, Result, LoadExtensionGuard};
9 /// # use std::path::{Path};
10 /// fn load_my_extension(conn: &Connection) -> Result<()> {
11 ///     unsafe {
12 ///         let _guard = LoadExtensionGuard::new(conn)?;
13 ///         conn.load_extension("trusted/sqlite/extension", None)
14 ///     }
15 /// }
16 /// ```
17 #[cfg_attr(docsrs, doc(cfg(feature = "load_extension")))]
18 pub struct LoadExtensionGuard<'conn> {
19     conn: &'conn Connection,
20 }
21 
22 impl LoadExtensionGuard<'_> {
23     /// Attempt to enable loading extensions. Loading extensions will be
24     /// disabled when this guard goes out of scope. Cannot be meaningfully
25     /// nested.
26     ///
27     /// # Safety
28     ///
29     /// You must not run untrusted queries while extension loading is enabled.
30     ///
31     /// See the safety comment on [`Connection::load_extension_enable`] for more
32     /// details.
33     #[inline]
new(conn: &Connection) -> Result<LoadExtensionGuard<'_>>34     pub unsafe fn new(conn: &Connection) -> Result<LoadExtensionGuard<'_>> {
35         conn.load_extension_enable()
36             .map(|_| LoadExtensionGuard { conn })
37     }
38 }
39 
40 #[allow(unused_must_use)]
41 impl Drop for LoadExtensionGuard<'_> {
42     #[inline]
drop(&mut self)43     fn drop(&mut self) {
44         self.conn.load_extension_disable();
45     }
46 }
47