feat: impl Clone for Store

(cherry picked from commit 90750c3c82ab0a5973de634661c5284c74fc89a8)
This commit is contained in:
Robert Hensing 2024-06-26 21:00:22 +02:00
parent 9cebf1d131
commit 03f6c63373

View file

@ -7,6 +7,7 @@ use nix_util::{check_call, result_string_init};
use std::ffi::{c_char, CString}; use std::ffi::{c_char, CString};
use std::ptr::null_mut; use std::ptr::null_mut;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::sync::Arc;
/* TODO make Nix itself thread safe */ /* TODO make Nix itself thread safe */
lazy_static! { lazy_static! {
@ -33,7 +34,7 @@ impl Drop for StoreRef {
} }
pub struct Store { pub struct Store {
inner: StoreRef, inner: Arc<StoreRef>,
/* An error context to reuse. This way we don't have to allocate them for each store operation. */ /* An error context to reuse. This way we don't have to allocate them for each store operation. */
context: Context, context: Context,
} }
@ -84,9 +85,9 @@ impl Store {
panic!("nix_c_store_open returned a null pointer without an error"); panic!("nix_c_store_open returned a null pointer without an error");
} }
let store = Store { let store = Store {
inner: StoreRef { inner: Arc::new(StoreRef {
inner: NonNull::new(store).unwrap(), inner: NonNull::new(store).unwrap(),
}, }),
context, context,
}; };
Ok(store) Ok(store)
@ -110,6 +111,15 @@ impl Store {
} }
} }
impl Clone for Store {
fn clone(&self) -> Self {
Store {
inner: self.inner.clone(),
context: Context::new(),
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::HashMap; use std::collections::HashMap;