SenzingGuard

Struct SenzingGuard 

Source
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

  1. Creation: Initializes or obtains the Senzing environment
  2. Usage: Access the environment via Deref or env() method
  3. 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()

AspectSenzingGuardManual destroy()
Resource releaseAutomatic on dropExplicit call required
Error handlingPanics on cleanup failureReturns SzResult
Multiple referencesMust be sole ownerReturns 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

Source

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 purposes
  • ini_params - JSON string with Senzing configuration
  • verbose_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()?;
Source

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()?;
Source

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().

Source

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()?;
Source

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);
}

Trait Implementations§

Source§

impl Deref for SenzingGuard

Source§

type Target = Arc<SzEnvironmentCore>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for SenzingGuard

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for SenzingGuard

Source§

impl Sync for SenzingGuard

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.