pub struct SenzingGuard {
env: Option<Arc<SzEnvironmentCore>>,
}Expand description
RAII guard for automatic Senzing environment cleanup.
SenzingGuard wraps an Arc<SzEnvironmentCore> and ensures that native
Senzing resources are properly released when the guard goes out of scope.
This provides idiomatic Rust resource management without requiring explicit
destroy() calls.
§Lifecycle
- Creation: Initializes or obtains the Senzing environment
- Usage: Access the environment via
Dereforenv()method - Destruction: When dropped, removes the singleton reference and attempts to release native resources
§Example
use sz_rust_sdk::prelude::*;
// Guard automatically manages the lifecycle
let guard = SenzingGuard::from_env(env);
// Access environment and components
let engine = guard.get_engine()?;
let product = guard.get_product()?;
// Add records
engine.add_record("TEST", "GUARD_1", r#"{"NAME_FULL": "John"}"#, None)?;
// Resources released automatically when guard drops§Thread Safety
SenzingGuard is thread-safe (Send + Sync) because it wraps an
Arc<SzEnvironmentCore>. The underlying environment can be safely
shared across threads.
§Comparison with Manual destroy()
| Aspect | SenzingGuard | Manual destroy() |
|---|---|---|
| Resource release | Automatic on drop | Explicit call required |
| Error handling | Panics on cleanup failure | Returns SzResult |
| Multiple references | Must be sole owner | Returns error if refs exist |
| Idiomatic Rust | ✅ RAII pattern | ❌ Explicit lifecycle |
§Why Use This Instead of a Custom Drop
The SzEnvironmentCore::destroy()
method takes Arc<Self> by value, which makes it tricky to call from a
Drop implementation (since Drop only gives &mut self).
SenzingGuard solves this by storing the Arc in an Option and using
.take() to safely move it out before calling destroy(). If you write
your own wrapper, follow the same pattern — see the safety notes on
SzEnvironmentCore::destroy()
for details.
§Panic Behavior
The guard will panic if cleanup fails during Drop. This is intentional
to prevent silent resource leaks. If you need to handle cleanup errors
gracefully, use the explicit into_inner() method and call destroy()
manually.
Fields§
§env: Option<Arc<SzEnvironmentCore>>Implementations§
Source§impl SenzingGuard
impl SenzingGuard
Sourcepub fn new(
module_name: &str,
ini_params: &str,
verbose_logging: bool,
) -> SzResult<Self>
pub fn new( module_name: &str, ini_params: &str, verbose_logging: bool, ) -> SzResult<Self>
Creates a new SenzingGuard with the specified configuration.
This initializes the Senzing environment singleton or returns the existing instance if one already exists with compatible parameters.
§Arguments
module_name- Name for logging purposesini_params- JSON string with Senzing configurationverbose_logging- Enable verbose logging
§Example
use sz_rust_sdk::prelude::*;
// SenzingGuard::new(name, settings, verbose) initializes directly.
// Here we use from_env with a pre-initialized environment:
let guard = SenzingGuard::from_env(env);
let product = guard.get_product()?;Sourcepub fn from_env(env: Arc<SzEnvironmentCore>) -> Self
pub fn from_env(env: Arc<SzEnvironmentCore>) -> Self
Creates a guard from an existing environment instance.
Use this when you already have an Arc<SzEnvironmentCore> and want
to transfer ownership to a guard for automatic cleanup.
§Example
use sz_rust_sdk::prelude::*;
let guard = SenzingGuard::from_env(env);
// guard now owns the Arc and will clean up on drop
let product = guard.get_product()?;Sourcepub fn env(&self) -> &Arc<SzEnvironmentCore>
pub fn env(&self) -> &Arc<SzEnvironmentCore>
Gets a reference to the inner environment.
§Panics
Panics if the guard has already been consumed via into_inner().
Sourcepub fn into_inner(self) -> Arc<SzEnvironmentCore>
pub fn into_inner(self) -> Arc<SzEnvironmentCore>
Consumes the guard and returns the inner Arc<SzEnvironmentCore>.
After calling this, the guard will NOT perform automatic cleanup.
You become responsible for calling destroy() on the returned Arc.
§Example
use sz_rust_sdk::prelude::*;
let guard = SenzingGuard::from_env(env);
let env = guard.into_inner();
// Manual cleanup required after into_inner()
env.destroy()?;Sourcepub fn try_cleanup(self) -> SzResult<()>
pub fn try_cleanup(self) -> SzResult<()>
Attempts cleanup without panicking.
Returns an error if cleanup fails, allowing graceful error handling. After calling this, the guard is consumed and Drop will be a no-op.
§Example
use sz_rust_sdk::prelude::*;
let guard = SenzingGuard::from_env(env);
// ... use guard ...
if let Err(e) = guard.try_cleanup() {
eprintln!("Cleanup failed: {}", e);
}