chore: put env types into container type (#5436)

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
This commit is contained in:
Matthias Seitz
2023-11-15 15:30:09 +01:00
committed by GitHub
parent b98d7c8c12
commit f42db5b4a2

View File

@ -70,8 +70,7 @@ pub struct Environment<E>
where where
E: EnvironmentKind, E: EnvironmentKind,
{ {
env: *mut ffi::MDBX_env, inner: EnvironmentInner,
txn_manager: Option<SyncSender<TxnManagerMessage>>,
_marker: PhantomData<E>, _marker: PhantomData<E>,
} }
@ -102,7 +101,7 @@ where
/// Requires [Mode::ReadWrite] and returns None otherwise. /// Requires [Mode::ReadWrite] and returns None otherwise.
#[inline] #[inline]
pub(crate) fn txn_manager(&self) -> Option<&SyncSender<TxnManagerMessage>> { pub(crate) fn txn_manager(&self) -> Option<&SyncSender<TxnManagerMessage>> {
self.txn_manager.as_ref() self.inner.txn_manager.as_ref()
} }
/// Returns a raw pointer to the underlying MDBX environment. /// Returns a raw pointer to the underlying MDBX environment.
@ -111,7 +110,7 @@ where
/// environment. /// environment.
#[inline] #[inline]
pub fn env(&self) -> *mut ffi::MDBX_env { pub fn env(&self) -> *mut ffi::MDBX_env {
self.env self.inner.env
} }
/// Create a read-only transaction for use with the environment. /// Create a read-only transaction for use with the environment.
@ -223,6 +222,24 @@ where
} }
} }
/// Container type for Environment internals.
///
/// This holds the raw pointer to the MDBX environment and the transaction manager.
/// The env is opened via [mdbx_env_create](ffi::mdbx_env_create) and closed when this type drops.
struct EnvironmentInner {
env: *mut ffi::MDBX_env,
txn_manager: Option<SyncSender<TxnManagerMessage>>,
}
impl Drop for EnvironmentInner {
fn drop(&mut self) {
// Close open mdbx environment on drop
unsafe {
ffi::mdbx_env_close_ex(self.env, false);
}
}
}
/// Environment statistics. /// Environment statistics.
/// ///
/// Contains information about the size and layout of an MDBX environment or database. /// Contains information about the size and layout of an MDBX environment or database.
@ -338,18 +355,7 @@ where
E: EnvironmentKind, E: EnvironmentKind,
{ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Environment").finish() f.debug_struct("Environment").finish_non_exhaustive()
}
}
impl<E> Drop for Environment<E>
where
E: EnvironmentKind,
{
fn drop(&mut self) {
unsafe {
ffi::mdbx_env_close_ex(self.env, false);
}
} }
} }
@ -511,7 +517,7 @@ where
} }
} }
let mut env = Environment { env, txn_manager: None, _marker: PhantomData }; let mut env = EnvironmentInner { env, txn_manager: None };
if let Mode::ReadWrite { .. } = self.flags.mode { if let Mode::ReadWrite { .. } = self.flags.mode {
let (tx, rx) = std::sync::mpsc::sync_channel(0); let (tx, rx) = std::sync::mpsc::sync_channel(0);
@ -556,7 +562,7 @@ where
env.txn_manager = Some(tx); env.txn_manager = Some(tx);
} }
Ok(env) Ok(Environment { inner: env, _marker: Default::default() })
} }
/// Sets the provided options in the environment. /// Sets the provided options in the environment.