diff --git a/nixide/src/store/path.rs b/nixide/src/store/path.rs index 40c139b..5eed4fd 100644 --- a/nixide/src/store/path.rs +++ b/nixide/src/store/path.rs @@ -1,6 +1,8 @@ +use std::cell::RefCell; use std::ffi::{CString, c_void}; use std::path::PathBuf; use std::ptr::NonNull; +use std::rc::Rc; use super::Store; use crate::NixideResult; @@ -15,7 +17,8 @@ use crate::util::wrappers::AsInnerPtr; /// Represents a store path that can be realized, queried, or manipulated. /// pub struct StorePath { - pub(crate) inner: NonNull, + inner: NonNull, + store: Rc>, } impl Clone for StorePath { @@ -25,7 +28,10 @@ impl Clone for StorePath { panic_issue_call_failed!("nix_store_path_clone returned None for valid path") }); - StorePath { inner } + StorePath { + inner, + store: self.store.clone(), + } } } @@ -65,17 +71,17 @@ impl StorePath { /// # Errors /// /// Returns an error if the path cannot be parsed. - pub fn parse(store: &Store, path: &str) -> NixideResult { + pub fn parse(store: Rc>, path: &str) -> NixideResult { let c_path = CString::new(path).or(Err(new_nixide_error!(StringNulByte)))?; let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { - sys::nix_store_parse_path(ctx.as_ptr(), store.as_ptr(), c_path.as_ptr()) + sys::nix_store_parse_path(ctx.as_ptr(), store.borrow().as_ptr(), c_path.as_ptr()) })?; - Ok(Self { inner }) + Ok(Self { inner, store }) } - pub fn fake_path(store: &Store) -> NixideResult { + pub fn fake_path(store: Rc>) -> NixideResult { Self::parse(store, "/nix/store/00000000000000000000000000000000-fake") } @@ -108,22 +114,18 @@ impl StorePath { /// Not all types of stores support this operation. /// /// # Arguments - /// * `context` [in] - Optional, stores error information - /// * `store` [in] - nix store reference - /// * `path` [in] - the path to get the real path from - /// * `callback` [in] - called with the real path - /// * `user_data` [in] - arbitrary data, passed to the callback when it's called. + /// * `store` - nix store reference /// /// # Arguments /// /// * `store` - The store containing the path /// - pub fn real_path(&self, store: &Store) -> NixideResult { + pub fn real_path(&self) -> NixideResult { wrap::nix_pathbuf_callback!( |callback, userdata: *mut __UserData, ctx: &ErrorContext| unsafe { sys::nix_store_real_path( ctx.as_ptr(), - store.inner.as_ptr(), + self.store.borrow().as_ptr(), self.as_ptr(), Some(callback), userdata as *mut c_void,