diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..ffeff7f --- /dev/null +++ b/TODO.md @@ -0,0 +1,24 @@ +- [ ] rename `AsInnerPtr::as_ptr` to `AsInnerPtr::as_mut_ptr` + +- [ ] add NixError::from_nonnull that replaces calls to NonNull::new(...).ok_or(...) +- [ ] replace all `use nixide_sys as sys;` -> `use crate::sys;` +- [ ] store NonNull pointers in structs! +- [ ] improve documentation situation on context.rs + +- [ ] rename `as_ptr()` to `as_inner_ptr()` or `inner_ptr()`? +- [ ] ^^^ this fn should be added to a trait (maybe just `trait NixStructWrapper : AsPtr { ... }`) +- [ ] ^^^ also make `as_ptr()` public + +- [ ] add mutexs and make the library thread safe!! + +- [ ] grep all `self.inner.as_ptr()` calls and replace them with `self.as_ptr()` + + +- [ ] `ErrorContext::peak` should return `Result<(), NixideError>` **not** `Option` +- [ ] `self.expect_type` should instead be a macro to preserve the trace macro location + +- [ ] make `Value` an enum instead because like duhh + +- [ ] ensure we're always calling `ctx.peak()` unless it's ACTUALLY not necessary + +- [ ] replace *most* calls to `ErrorContext::peak()` with `ErrorContext::pop()` diff --git a/nixide/src/errors/context.rs b/nixide/src/errors/context.rs index 0f53046..87d4b8e 100644 --- a/nixide/src/errors/context.rs +++ b/nixide/src/errors/context.rs @@ -182,6 +182,8 @@ impl ErrorContext { /// of `nix_c_context` must be careful to check the `last_err_code` regularly. pub fn clear(&mut self) { unsafe { + // NOTE: previous nixops4 used the line: (maybe for compatability with old versions?) + // sys::nix_set_err_msg(self.inner.as_ptr(), sys::nix_err_NIX_OK, c"".as_ptr()); sys::nix_clear_err(self.as_ptr()); } } @@ -349,7 +351,7 @@ impl ErrorContext { impl Drop for ErrorContext { fn drop(&mut self) { unsafe { - sys::nix_c_context_free(self.as_ptr()); + sys::nix_c_context_free(self.inner.as_ptr()); } } } diff --git a/nixide/src/expr/evalstate.rs b/nixide/src/expr/evalstate.rs index 1c6bb28..304409a 100644 --- a/nixide/src/expr/evalstate.rs +++ b/nixide/src/expr/evalstate.rs @@ -97,8 +97,9 @@ impl EvalState { impl Drop for EvalState { fn drop(&mut self) { + // SAFETY: We own the state and it's valid until drop unsafe { - sys::nix_state_free(self.as_ptr()); + sys::nix_state_free(self.inner.as_ptr()); } } } diff --git a/nixide/src/expr/evalstatebuilder.rs b/nixide/src/expr/evalstatebuilder.rs index c289fbd..82908dc 100644 --- a/nixide/src/expr/evalstatebuilder.rs +++ b/nixide/src/expr/evalstatebuilder.rs @@ -77,8 +77,9 @@ impl EvalStateBuilder { impl Drop for EvalStateBuilder { fn drop(&mut self) { + // SAFETY: We own the builder and it's valid until drop unsafe { - sys::nix_eval_state_builder_free(self.as_ptr()); + sys::nix_eval_state_builder_free(self.inner.as_ptr()); } } } diff --git a/nixide/src/store/mod.rs b/nixide/src/store/mod.rs index 420d807..4bca2a8 100644 --- a/nixide/src/store/mod.rs +++ b/nixide/src/store/mod.rs @@ -23,10 +23,10 @@ use std::result::Result; use crate::errors::{new_nixide_error, ErrorContext}; use crate::stdext::CCharPtrExt; -use crate::sys; use crate::util::wrap; use crate::util::wrappers::AsInnerPtr; use crate::{NixideError, NixideResult}; +use nixide_sys as sys; /// Nix store for managing packages and derivations. /// @@ -129,7 +129,7 @@ impl Store { sys::nix_store_realise( ctx.as_ptr(), - self.as_ptr(), + self.inner.as_ptr(), path.as_ptr(), (*state).inner_ptr() as *mut c_void, Some(callback), @@ -173,7 +173,7 @@ impl Store { |callback, userdata: *mut __UserData, ctx: &ErrorContext| unsafe { sys::nix_store_get_version( ctx.as_ptr(), - self.as_ptr(), + self.inner.as_ptr(), Some(callback), userdata as *mut c_void, ) @@ -187,7 +187,7 @@ impl Store { |callback, userdata: *mut __UserData, ctx: &ErrorContext| unsafe { sys::nix_store_get_uri( ctx.as_ptr(), - self.as_ptr(), + self.inner.as_ptr(), Some(callback), userdata as *mut c_void, ) @@ -200,7 +200,7 @@ impl Store { |callback, userdata: *mut __UserData, ctx: &ErrorContext| unsafe { sys::nix_store_get_storedir( ctx.as_ptr(), - self.as_ptr(), + self.inner.as_ptr(), Some(callback), userdata as *mut c_void, ) @@ -242,7 +242,7 @@ impl Store { impl Drop for Store { fn drop(&mut self) { unsafe { - sys::nix_store_free(self.as_ptr()); + sys::nix_store_free(self.inner.as_ptr()); } } } diff --git a/nixide/src/store/path.rs b/nixide/src/store/path.rs index d037c0e..0c4e832 100644 --- a/nixide/src/store/path.rs +++ b/nixide/src/store/path.rs @@ -4,12 +4,13 @@ use std::ptr::NonNull; use super::Store; use crate::errors::{new_nixide_error, ErrorContext}; -use crate::sys; use crate::util::panic_issue_call_failed; use crate::util::wrap; use crate::util::wrappers::AsInnerPtr; use crate::NixideResult; +use nixide_sys as sys; + /// A path in the Nix store. /// /// Represents a store path that can be realized, queried, or manipulated. @@ -19,20 +20,9 @@ pub struct StorePath { } impl AsInnerPtr for StorePath { - #[inline] unsafe fn as_ptr(&self) -> *mut sys::StorePath { self.inner.as_ptr() } - - #[inline] - unsafe fn as_ref(&self) -> &sys::StorePath { - unsafe { self.inner.as_ref() } - } - - #[inline] - unsafe fn as_mut(&mut self) -> &mut sys::StorePath { - unsafe { self.inner.as_mut() } - } } impl StorePath { @@ -71,7 +61,7 @@ impl StorePath { /// pub fn name(&self) -> NixideResult { wrap::nix_string_callback!(|callback, userdata: *mut __UserData, _| unsafe { - sys::nix_store_path_name(self.as_ptr(), Some(callback), userdata as *mut c_void); + sys::nix_store_path_name(self.inner.as_ptr(), Some(callback), userdata as *mut c_void); // NOTE: nix_store_path_name doesn't return nix_err, so we force it to return successfully // XXX: NOTE: now `nix_string_callback` is a macro this isn't necessary // sys::nix_err_NIX_OK diff --git a/nixide/src/util/wrappers.rs b/nixide/src/util/wrappers.rs index c3e4b07..6fa0378 100644 --- a/nixide/src/util/wrappers.rs +++ b/nixide/src/util/wrappers.rs @@ -10,7 +10,7 @@ pub trait AsInnerPtr { /// Returns a shared reference to the inner `libnix` C struct. /// - /// For the mutable counterpart see [Self::as_mut]. + /// For the mutable counterpart see [AsInnerPtr::as_mut]. /// /// # Safety /// @@ -21,7 +21,7 @@ pub trait AsInnerPtr { /// Returns a unique reference to the inner `libnix` C struct. /// - /// For the shared counterpart see [Self::as_ref]. + /// For the shared counterpart see [AsInnerPtr::as_ref]. /// /// # Safety ///