From 22272e0bc2589e8e6cf6174d5faff4293dce0c5d Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 15 Nov 2023 14:10:10 +0100 Subject: [PATCH 1/3] chore: put env types into container type --- crates/storage/libmdbx-rs/src/environment.rs | 45 +++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/crates/storage/libmdbx-rs/src/environment.rs b/crates/storage/libmdbx-rs/src/environment.rs index 7315c9f082a4..5f80c5a557d8 100644 --- a/crates/storage/libmdbx-rs/src/environment.rs +++ b/crates/storage/libmdbx-rs/src/environment.rs @@ -70,9 +70,7 @@ pub struct Environment where E: EnvironmentKind, { - env: *mut ffi::MDBX_env, - txn_manager: Option>, - _marker: PhantomData, + inner: EnvironmentInner, } impl Environment @@ -102,7 +100,7 @@ where /// Requires [Mode::ReadWrite] and returns None otherwise. #[inline] pub(crate) fn txn_manager(&self) -> Option<&SyncSender> { - self.txn_manager.as_ref() + self.inner.txn_manager.as_ref() } /// Returns a raw pointer to the underlying MDBX environment. @@ -111,7 +109,7 @@ where /// environment. #[inline] pub fn env(&self) -> *mut ffi::MDBX_env { - self.env + self.inner.env } /// Create a read-only transaction for use with the environment. @@ -223,6 +221,26 @@ where } } +/// Container type for Environment internals +/// +/// This olds the 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>, + _marker: PhantomData, +} + +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. /// /// Contains information about the size and layout of an MDBX environment or database. @@ -338,18 +356,7 @@ where E: EnvironmentKind, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Environment").finish() - } -} - -impl Drop for Environment -where - E: EnvironmentKind, -{ - fn drop(&mut self) { - unsafe { - ffi::mdbx_env_close_ex(self.env, false); - } + f.debug_struct("Environment").finish_non_exhaustive() } } @@ -511,7 +518,7 @@ where } } - let mut env = Environment { env, txn_manager: None, _marker: PhantomData }; + let mut env = EnvironmentInner { env, txn_manager: None, _marker: PhantomData }; if let Mode::ReadWrite { .. } = self.flags.mode { let (tx, rx) = std::sync::mpsc::sync_channel(0); @@ -556,7 +563,7 @@ where env.txn_manager = Some(tx); } - Ok(env) + Ok(Environment { inner: env }) } /// Sets the provided options in the environment. From a60cfc529f53950d4daef754a6dee6f67d346b76 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 15 Nov 2023 15:11:08 +0100 Subject: [PATCH 2/3] Update crates/storage/libmdbx-rs/src/environment.rs Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/storage/libmdbx-rs/src/environment.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/storage/libmdbx-rs/src/environment.rs b/crates/storage/libmdbx-rs/src/environment.rs index 5f80c5a557d8..20c69695d8c4 100644 --- a/crates/storage/libmdbx-rs/src/environment.rs +++ b/crates/storage/libmdbx-rs/src/environment.rs @@ -221,9 +221,9 @@ where } } -/// Container type for Environment internals +/// Container type for Environment internals. /// -/// This olds the the raw pointer to the MDBX environment and the transaction manager. +/// 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, From c14077a7a4328f2dae831c821850577d21d25340 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 15 Nov 2023 15:13:27 +0100 Subject: [PATCH 3/3] chore: move marker to outer type --- crates/storage/libmdbx-rs/src/environment.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/storage/libmdbx-rs/src/environment.rs b/crates/storage/libmdbx-rs/src/environment.rs index 20c69695d8c4..e8ace3115f67 100644 --- a/crates/storage/libmdbx-rs/src/environment.rs +++ b/crates/storage/libmdbx-rs/src/environment.rs @@ -70,7 +70,8 @@ pub struct Environment where E: EnvironmentKind, { - inner: EnvironmentInner, + inner: EnvironmentInner, + _marker: PhantomData, } impl Environment @@ -225,14 +226,12 @@ where /// /// 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 { +struct EnvironmentInner { env: *mut ffi::MDBX_env, txn_manager: Option>, - _marker: PhantomData, } -impl Drop for EnvironmentInner -{ +impl Drop for EnvironmentInner { fn drop(&mut self) { // Close open mdbx environment on drop unsafe { @@ -518,7 +517,7 @@ where } } - let mut env = EnvironmentInner { env, txn_manager: None, _marker: PhantomData }; + let mut env = EnvironmentInner { env, txn_manager: None }; if let Mode::ReadWrite { .. } = self.flags.mode { let (tx, rx) = std::sync::mpsc::sync_channel(0); @@ -563,7 +562,7 @@ where env.txn_manager = Some(tx); } - Ok(Environment { inner: env }) + Ok(Environment { inner: env, _marker: Default::default() }) } /// Sets the provided options in the environment.