internally pass EvalState via Rc<RefCell<NonNull<sys::EvalState>>>

This commit is contained in:
do butterflies cry? 2026-03-30 15:05:20 +10:00
parent 18fcc93c36
commit c5c3f6c8d4
Signed by: cry
GPG key ID: F68745A836CA0412
16 changed files with 127 additions and 110 deletions

View file

@ -1,7 +1,8 @@
use std::ffi::{CString, c_void}; use std::cell::RefCell;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use crate::errors::new_nixide_error; use crate::stdext::AsCPtr as _;
use super::Value; use super::Value;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -15,9 +16,9 @@ use crate::{NixideResult, Store};
/// This provides the main interface for evaluating Nix expressions /// This provides the main interface for evaluating Nix expressions
/// and creating values. /// and creating values.
pub struct EvalState { pub struct EvalState {
inner: NonNull<sys::EvalState>, inner: Rc<RefCell<NonNull<sys::EvalState>>>,
store: Store, store: Rc<RefCell<Store>>,
} }
// impl Clone for EvalState { // impl Clone for EvalState {
@ -39,31 +40,37 @@ pub struct EvalState {
impl AsInnerPtr<sys::EvalState> for EvalState { impl AsInnerPtr<sys::EvalState> for EvalState {
#[inline] #[inline]
unsafe fn as_ptr(&self) -> *mut sys::EvalState { unsafe fn as_ptr(&self) -> *mut sys::EvalState {
self.inner.as_ptr() self.inner.borrow().as_ptr()
} }
#[inline] #[inline]
unsafe fn as_ref(&self) -> &sys::EvalState { unsafe fn as_ref(&self) -> &sys::EvalState {
unsafe { self.inner.as_ref() } unsafe { self.inner.borrow().as_ref() }
} }
#[inline] #[inline]
unsafe fn as_mut(&mut self) -> &mut sys::EvalState { unsafe fn as_mut(&mut self) -> &mut sys::EvalState {
unsafe { self.inner.as_mut() } unsafe { self.inner.borrow_mut().as_mut() }
} }
} }
impl EvalState { impl EvalState {
/// Construct a new EvalState directly from its attributes /// Construct a new EvalState directly from its attributes
/// ///
pub(super) fn new(inner: NonNull<sys::EvalState>, store: &Store) -> Self { pub(super) fn from(inner: NonNull<sys::EvalState>, store: Rc<RefCell<Store>>) -> Self {
Self { Self {
inner, inner: Rc::new(RefCell::new(inner)),
store: store.clone(), store,
} }
} }
pub fn store_ref(&self) -> &Store { #[inline]
pub fn inner_ref(&self) -> &Rc<RefCell<NonNull<sys::EvalState>>> {
&self.inner
}
#[inline]
pub fn store_ref(&self) -> &Rc<RefCell<Store>> {
&self.store &self.store
} }
@ -77,9 +84,10 @@ impl EvalState {
/// # Errors /// # Errors
/// ///
/// Returns an error if evaluation fails. /// Returns an error if evaluation fails.
///
pub fn interpret(&self, expr: &str, path: &str) -> NixideResult<Value> { pub fn interpret(&self, expr: &str, path: &str) -> NixideResult<Value> {
let expr_c = CString::new(expr).or(Err(new_nixide_error!(StringNulByte)))?; let expr = expr.as_c_ptr()?;
let path_c = CString::new(path).or(Err(new_nixide_error!(StringNulByte)))?; let path = path.as_c_ptr()?;
// Allocate value for result // Allocate value for result
// XXX: TODO: create a method for this (``) // XXX: TODO: create a method for this (``)
@ -90,13 +98,7 @@ impl EvalState {
// Evaluate expression // Evaluate expression
wrap::nix_fn!(|ctx: &ErrorContext| unsafe { wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_expr_eval_from_string( sys::nix_expr_eval_from_string(ctx.as_ptr(), self.as_ptr(), expr, path, value.as_ptr());
ctx.as_ptr(),
self.as_ptr(),
expr_c.as_ptr(),
path_c.as_ptr(),
value.as_ptr(),
);
value value
}) })
.map(|ptr| Value::from((ptr, self))) .map(|ptr| Value::from((ptr, self)))
@ -110,7 +112,3 @@ impl Drop for EvalState {
} }
} }
} }
// SAFETY: EvalState can be shared between threads
unsafe impl Send for EvalState {}
unsafe impl Sync for EvalState {}

View file

@ -1,5 +1,7 @@
use std::ffi::{CString, c_char, c_void}; use std::cell::RefCell;
use std::ffi::{CString, c_char};
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
use std::rc::Rc;
use super::EvalState; use super::EvalState;
#[cfg(feature = "flakes")] #[cfg(feature = "flakes")]
@ -16,8 +18,8 @@ use crate::util::{panic_issue_call_failed, wrap};
/// the evaluation state. /// the evaluation state.
/// ///
pub struct EvalStateBuilder { pub struct EvalStateBuilder {
inner: NonNull<sys::nix_eval_state_builder>, inner: Rc<RefCell<NonNull<sys::nix_eval_state_builder>>>,
store: Store, store: Rc<RefCell<Store>>,
} }
// impl Clone for EvalStateBuilder { // impl Clone for EvalStateBuilder {
@ -39,17 +41,17 @@ pub struct EvalStateBuilder {
impl AsInnerPtr<sys::nix_eval_state_builder> for EvalStateBuilder { impl AsInnerPtr<sys::nix_eval_state_builder> for EvalStateBuilder {
#[inline] #[inline]
unsafe fn as_ptr(&self) -> *mut sys::nix_eval_state_builder { unsafe fn as_ptr(&self) -> *mut sys::nix_eval_state_builder {
self.inner.as_ptr() self.inner.borrow().as_ptr()
} }
#[inline] #[inline]
unsafe fn as_ref(&self) -> &sys::nix_eval_state_builder { unsafe fn as_ref(&self) -> &sys::nix_eval_state_builder {
unsafe { self.inner.as_ref() } unsafe { self.inner.borrow().as_ref() }
} }
#[inline] #[inline]
unsafe fn as_mut(&mut self) -> &mut sys::nix_eval_state_builder { unsafe fn as_mut(&mut self) -> &mut sys::nix_eval_state_builder {
unsafe { self.inner.as_mut() } unsafe { self.inner.borrow_mut().as_mut() }
} }
} }
@ -64,14 +66,14 @@ impl EvalStateBuilder {
/// ///
/// Returns an error if the builder cannot be created. /// Returns an error if the builder cannot be created.
/// ///
pub fn new(store: &Store) -> NixideResult<Self> { pub fn new(store: Rc<RefCell<Store>>) -> NixideResult<Self> {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_eval_state_builder_new(ctx.as_ptr(), store.as_ptr()) sys::nix_eval_state_builder_new(ctx.as_ptr(), store.borrow().as_ptr())
})?; })?;
Ok(EvalStateBuilder { Ok(EvalStateBuilder {
inner, inner: Rc::new(RefCell::new(inner)),
store: store.clone(), store,
}) })
} }
@ -87,7 +89,7 @@ impl EvalStateBuilder {
sys::nix_eval_state_build(ctx.as_ptr(), self.as_ptr()) sys::nix_eval_state_build(ctx.as_ptr(), self.as_ptr())
})?; })?;
Ok(EvalState::new(inner, &self.store)) Ok(EvalState::from(inner, self.store.clone()))
} }
// XXX: TODO: use `flakes()` instead // XXX: TODO: use `flakes()` instead

View file

@ -1,15 +1,15 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::ffi::{c_char, c_void}; use std::ffi::c_char;
use std::ptr::NonNull; use std::ptr::NonNull;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
use crate::expr::values::NixString; use crate::expr::values::NixString;
use crate::stdext::CCharPtrExt; use crate::stdext::CCharPtrExt;
use crate::sys;
use crate::util::LazyArray; use crate::util::LazyArray;
use crate::util::wrappers::AsInnerPtr; use crate::util::wrappers::AsInnerPtr;
use crate::util::{panic_issue_call_failed, wrap}; use crate::util::{panic_issue_call_failed, wrap};
use crate::{EvalState, NixideResult, StorePath}; use crate::{EvalState, NixideResult, StorePath};
use crate::{Store, sys};
pub struct RealisedString<'a> { pub struct RealisedString<'a> {
inner: RefCell<NonNull<sys::nix_realised_string>>, inner: RefCell<NonNull<sys::nix_realised_string>>,
@ -17,23 +17,6 @@ pub struct RealisedString<'a> {
pub children: LazyArray<StorePath, Box<dyn Fn(usize) -> StorePath + 'a>>, pub children: LazyArray<StorePath, Box<dyn Fn(usize) -> StorePath + 'a>>,
} }
// impl<'a> Clone for RealisedString<'a> {
// fn clone(&self) -> Self {
// let inner = self.inner.clone();
// wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
// sys::nix_gc_incref(ctx.as_ptr(), self.as_ptr() as *mut c_void);
// })
// .unwrap();
// Self {
// inner,
// path: self.path.clone(),
// children: self.children,
// }
// }
// }
impl<'a> AsInnerPtr<sys::nix_realised_string> for RealisedString<'a> { impl<'a> AsInnerPtr<sys::nix_realised_string> for RealisedString<'a> {
#[inline] #[inline]
unsafe fn as_ptr(&self) -> *mut sys::nix_realised_string { unsafe fn as_ptr(&self) -> *mut sys::nix_realised_string {
@ -63,7 +46,7 @@ impl<'a> RealisedString<'a> {
/// Realise a string context. /// Realise a string context.
/// ///
/// This will /// This will
/// - realise the store paths referenced by the string's context, and /// - realise the store paths referenced by the string's content, and
/// - perform the replacement of placeholders. /// - perform the replacement of placeholders.
/// - create temporary garbage collection roots for the store paths, for /// - create temporary garbage collection roots for the store paths, for
/// the lifetime of the current process. /// the lifetime of the current process.
@ -77,9 +60,6 @@ impl<'a> RealisedString<'a> {
/// You should set this to true when this call is part of a primop. /// You should set this to true when this call is part of a primop.
/// You should set this to false when building for your application's purpose. /// You should set this to false when building for your application's purpose.
/// ///
/// # Returns
///
/// NULL if failed, or a new nix_realised_string, which must be freed with nix_realised_string_free
pub fn new(value: &NixString, state: &'a EvalState) -> NixideResult<RealisedString<'a>> { pub fn new(value: &NixString, state: &'a EvalState) -> NixideResult<RealisedString<'a>> {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_string_realise( sys::nix_string_realise(
@ -89,21 +69,20 @@ impl<'a> RealisedString<'a> {
false, // don't copy more false, // don't copy more
) )
})?; })?;
let cell = RefCell::new(inner);
let size = unsafe { sys::nix_realised_string_get_store_path_count(inner.as_ptr()) }; let size = unsafe { sys::nix_realised_string_get_store_path_count(inner.as_ptr()) };
Ok(Self { Ok(Self {
inner: cell, inner: RefCell::new(inner),
path: Self::parse_path(inner.as_ptr(), state), path: Self::parse_path(inner.as_ptr(), &state.store_ref().borrow()),
children: LazyArray::new( children: LazyArray::new(
size, size,
Box::new(|_| StorePath::fake_path(state.store_ref()).unwrap()), Box::new(|_| StorePath::fake_path(&state.store_ref().borrow()).unwrap()),
), ),
}) })
} }
fn parse_path(realised_string: *mut sys::nix_realised_string, state: &EvalState) -> StorePath { fn parse_path(realised_string: *mut sys::nix_realised_string, store: &Store) -> StorePath {
let buffer_ptr = unsafe { sys::nix_realised_string_get_buffer_start(realised_string) }; let buffer_ptr = unsafe { sys::nix_realised_string_get_buffer_start(realised_string) };
let buffer_size = unsafe { sys::nix_realised_string_get_buffer_size(realised_string) }; let buffer_size = unsafe { sys::nix_realised_string_get_buffer_size(realised_string) };
@ -115,7 +94,7 @@ impl<'a> RealisedString<'a> {
err err
) )
}); });
StorePath::parse(state.store_ref(), &path_str).unwrap_or_else(|err| { StorePath::parse(store, &path_str).unwrap_or_else(|err| {
panic_issue_call_failed!( panic_issue_call_failed!(
"`sys::nix_realised_string_get_buffer_(start|size)` invalid store path ({})", "`sys::nix_realised_string_get_buffer_(start|size)` invalid store path ({})",
err err

View file

@ -1,5 +1,3 @@
use std::rc::Rc;
use serial_test::serial; use serial_test::serial;
use super::{EvalStateBuilder, Value}; use super::{EvalStateBuilder, Value};
@ -8,19 +6,18 @@ use crate::Store;
#[test] #[test]
#[serial] #[serial]
fn test_eval_state_builder() { fn test_eval_state_builder() {
let store = Rc::new(Store::default().expect("Failed to open store")); let store = Store::default().expect("Failed to open store");
let _state = EvalStateBuilder::new(&store) let _state = EvalStateBuilder::new(store.clone())
.expect("Failed to create builder") .expect("Failed to create builder")
.build() .build()
.expect("Failed to build state"); .expect("Failed to build state");
// State should be dropped automatically
} }
#[test] #[test]
#[serial] #[serial]
fn test_simple_evaluation() { fn test_simple_evaluation() {
let store = Rc::new(Store::default().expect("Failed to open store")); let store = Store::default().expect("Failed to open store");
let state = EvalStateBuilder::new(&store) let state = EvalStateBuilder::new(store.clone())
.expect("Failed to create builder") .expect("Failed to create builder")
.build() .build()
.expect("Failed to build state"); .expect("Failed to build state");
@ -41,7 +38,7 @@ fn test_simple_evaluation() {
#[serial] #[serial]
fn test_value_types() { fn test_value_types() {
let store = Store::default().expect("Failed to open store"); let store = Store::default().expect("Failed to open store");
let state = EvalStateBuilder::new(&store) let state = EvalStateBuilder::new(store.clone())
.expect("Failed to create builder") .expect("Failed to create builder")
.build() .build()
.expect("Failed to build state"); .expect("Failed to build state");
@ -84,7 +81,7 @@ fn test_value_types() {
#[serial] #[serial]
fn test_value_formatting() { fn test_value_formatting() {
let store = Store::default().expect("Failed to open store"); let store = Store::default().expect("Failed to open store");
let state = EvalStateBuilder::new(&store) let state = EvalStateBuilder::new(store.clone())
.expect("Failed to create builder") .expect("Failed to create builder")
.build() .build()
.expect("Failed to build state"); .expect("Failed to build state");

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
use std::rc::Rc;
use super::{NixThunk, NixValue, Value}; use super::{NixThunk, NixValue, Value};
use crate::errors::{ErrorContext, NixideError}; use crate::errors::{ErrorContext, NixideError};
@ -11,7 +13,7 @@ use crate::{EvalState, NixError};
pub struct NixAttrs { pub struct NixAttrs {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
len: u32, len: u32,
} }
@ -84,7 +86,7 @@ impl NixValue for NixAttrs {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
len, len,
} }
} }
@ -107,7 +109,7 @@ impl NixAttrs {
sys::nix_get_attr_byidx( sys::nix_get_attr_byidx(
ctx.as_ptr(), ctx.as_ptr(),
self.as_ptr(), self.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
index, index,
name_ptr, name_ptr,
) )
@ -134,7 +136,7 @@ impl NixAttrs {
sys::nix_get_attr_byidx_lazy( sys::nix_get_attr_byidx_lazy(
ctx.as_ptr(), ctx.as_ptr(),
self.as_ptr(), self.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
index, index,
name_ptr, name_ptr,
) )
@ -156,7 +158,12 @@ impl NixAttrs {
} }
let name_ptr = wrap::nix_fn!(|ctx: &ErrorContext| unsafe { let name_ptr = wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_get_attr_name_byidx(ctx.as_ptr(), self.as_ptr(), self.state.as_ptr(), index) sys::nix_get_attr_name_byidx(
ctx.as_ptr(),
self.as_ptr(),
self.state.borrow().as_ptr(),
index,
)
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
@ -175,7 +182,7 @@ impl NixAttrs {
sys::nix_get_attr_byname( sys::nix_get_attr_byname(
ctx.as_ptr(), ctx.as_ptr(),
self.as_ptr(), self.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
name.as_ref() name.as_ref()
.into_c_ptr() .into_c_ptr()
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)), .unwrap_or_else(|err| panic_issue_call_failed!("{}", err)),
@ -201,7 +208,7 @@ impl NixAttrs {
sys::nix_get_attr_byname_lazy( sys::nix_get_attr_byname_lazy(
ctx.as_ptr(), ctx.as_ptr(),
self.as_ptr(), self.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
name.as_ref() name.as_ref()
.into_c_ptr() .into_c_ptr()
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)), .unwrap_or_else(|err| panic_issue_call_failed!("{}", err)),

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -10,7 +12,7 @@ use crate::{EvalState, sys};
pub struct NixBool { pub struct NixBool {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
value: bool, value: bool,
} }
@ -85,7 +87,7 @@ impl NixValue for NixBool {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
value, value,
} }
} }

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::EvalState; use crate::EvalState;
@ -10,7 +12,7 @@ use crate::util::{panic_issue_call_failed, wrap};
pub struct NixFloat { pub struct NixFloat {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
value: f64, value: f64,
} }
@ -85,7 +87,7 @@ impl NixValue for NixFloat {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
value, value,
} }
} }

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::{NixValue, Value}; use super::{NixValue, Value};
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -10,7 +12,7 @@ use crate::{EvalState, sys};
pub struct NixFunction { pub struct NixFunction {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
} }
impl Clone for NixFunction { impl Clone for NixFunction {
@ -76,7 +78,7 @@ impl NixValue for NixFunction {
fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self { fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
} }
} }
} }
@ -87,14 +89,14 @@ impl NixFunction {
T: NixValue, T: NixValue,
{ {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_alloc_value(ctx.as_ptr(), self.state.as_ptr()) sys::nix_alloc_value(ctx.as_ptr(), self.state.borrow().as_ptr())
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
wrap::nix_fn!(|ctx: &ErrorContext| unsafe { wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_value_call( sys::nix_value_call(
ctx.as_ptr(), ctx.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
self.as_ptr(), self.as_ptr(),
arg.as_ptr(), arg.as_ptr(),
inner.as_ptr(), inner.as_ptr(),
@ -110,14 +112,14 @@ impl NixFunction {
T: NixValue, T: NixValue,
{ {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_alloc_value(ctx.as_ptr(), self.state.as_ptr()) sys::nix_alloc_value(ctx.as_ptr(), self.state.borrow().as_ptr())
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
wrap::nix_fn!(|ctx: &ErrorContext| unsafe { wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_value_call_multi( sys::nix_value_call_multi(
ctx.as_ptr(), ctx.as_ptr(),
self.state.as_ptr(), self.state.borrow().as_ptr(),
self.as_ptr(), self.as_ptr(),
args.len(), args.len(),
args.into_c_array(), args.into_c_array(),

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::EvalState; use crate::EvalState;
@ -10,7 +12,7 @@ use crate::util::{panic_issue_call_failed, wrap};
pub struct NixInt { pub struct NixInt {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
value: i64, value: i64,
} }
@ -83,7 +85,7 @@ impl NixValue for NixInt {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
value, value,
} }
} }

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::{NixThunk, NixValue, Value}; use super::{NixThunk, NixValue, Value};
use crate::EvalState; use crate::EvalState;
@ -10,7 +12,7 @@ use crate::util::{panic_issue_call_failed, wrap};
pub struct NixList { pub struct NixList {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
} }
impl Clone for NixList { impl Clone for NixList {
@ -76,7 +78,7 @@ impl NixValue for NixList {
fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self { fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
} }
} }
} }
@ -116,7 +118,12 @@ impl NixList {
pub fn get(&self, index: u32) -> Value { pub fn get(&self, index: u32) -> Value {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_get_list_byidx(ctx.as_ptr(), self.as_ptr(), self.state.as_ptr(), index) sys::nix_get_list_byidx(
ctx.as_ptr(),
self.as_ptr(),
self.state.borrow().as_ptr(),
index,
)
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));
@ -125,7 +132,12 @@ impl NixList {
pub fn get_lazy(&self, index: u32) -> NixThunk { pub fn get_lazy(&self, index: u32) -> NixThunk {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_get_list_byidx_lazy(ctx.as_ptr(), self.as_ptr(), self.state.as_ptr(), index) sys::nix_get_list_byidx_lazy(
ctx.as_ptr(),
self.as_ptr(),
self.state.borrow().as_ptr(),
index,
)
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::EvalState; use crate::EvalState;
@ -10,7 +12,7 @@ use crate::util::wrappers::AsInnerPtr;
pub struct NixNull { pub struct NixNull {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
} }
impl Clone for NixNull { impl Clone for NixNull {
@ -76,7 +78,7 @@ impl NixValue for NixNull {
fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self { fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
} }
} }
} }

View file

@ -1,6 +1,8 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::path::PathBuf; use std::path::PathBuf;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -12,7 +14,7 @@ use crate::{EvalState, sys};
pub struct NixPath { pub struct NixPath {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
value: PathBuf, value: PathBuf,
} }
@ -87,7 +89,7 @@ impl NixValue for NixPath {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
value, value,
} }
} }

View file

@ -1,5 +1,8 @@
use std::cell::RefCell;
use std::ffi::c_void;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::NixValue; use super::NixValue;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -10,7 +13,7 @@ use crate::{EvalState, sys};
pub struct NixString { pub struct NixString {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
value: String, value: String,
} }
@ -90,7 +93,7 @@ impl NixValue for NixString {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
value, value,
} }
} }

View file

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::rc::Rc;
use super::{NixValue, Value}; use super::{NixValue, Value};
use crate::EvalState; use crate::EvalState;
@ -10,7 +12,7 @@ use crate::util::{panic_issue_call_failed, wrap};
pub struct NixThunk { pub struct NixThunk {
inner: NonNull<sys::nix_value>, inner: NonNull<sys::nix_value>,
state: EvalState, state: Rc<RefCell<NonNull<sys::EvalState>>>,
} }
impl Clone for NixThunk { impl Clone for NixThunk {
@ -76,7 +78,7 @@ impl NixValue for NixThunk {
fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self { fn from(inner: NonNull<sys::nix_value>, state: &EvalState) -> Self {
Self { Self {
inner, inner,
state: state.clone(), state: state.inner_ref().clone(),
} }
} }
} }
@ -84,7 +86,11 @@ impl NixValue for NixThunk {
impl NixThunk { impl NixThunk {
pub fn eval(self) -> Value { pub fn eval(self) -> Value {
wrap::nix_fn!(|ctx: &ErrorContext| unsafe { wrap::nix_fn!(|ctx: &ErrorContext| unsafe {
sys::nix_value_force(ctx.as_ptr(), self.state.as_ptr(), self.inner.as_ptr()) sys::nix_value_force(
ctx.as_ptr(),
self.state.borrow().as_ptr(),
self.inner.as_ptr(),
)
}) })
.unwrap_or_else(|err| panic_issue_call_failed!("{}", err)); .unwrap_or_else(|err| panic_issue_call_failed!("{}", err));

View file

@ -1,4 +1,3 @@
use std::ffi::c_void;
use std::ptr::NonNull; use std::ptr::NonNull;
use crate::NixideResult; use crate::NixideResult;

View file

@ -7,9 +7,11 @@ mod tests;
mod path; mod path;
pub use path::*; pub use path::*;
use std::cell::RefCell;
use std::ffi::{c_char, c_void}; use std::ffi::{c_char, c_void};
use std::path::PathBuf; use std::path::PathBuf;
use std::ptr::{NonNull, null, null_mut}; use std::ptr::{NonNull, null, null_mut};
use std::rc::Rc;
use crate::NixideResult; use crate::NixideResult;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
@ -68,24 +70,24 @@ impl Store {
/// ///
/// Returns an error if the store cannot be opened. /// Returns an error if the store cannot be opened.
/// ///
pub fn open(uri: &str) -> NixideResult<Self> { pub fn open(uri: &str) -> NixideResult<Rc<RefCell<Self>>> {
Self::open_ptr(uri.as_c_ptr()?) Self::open_ptr(uri.as_c_ptr()?)
} }
/// Opens a connection to the default Nix store. /// Opens a connection to the default Nix store.
/// ///
pub fn default() -> NixideResult<Self> { pub fn default() -> NixideResult<Rc<RefCell<Self>>> {
Self::open_ptr(null()) Self::open_ptr(null())
} }
#[inline] #[inline]
fn open_ptr(uri_ptr: *const c_char) -> NixideResult<Self> { fn open_ptr(uri_ptr: *const c_char) -> NixideResult<Rc<RefCell<Self>>> {
let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe { let inner = wrap::nix_ptr_fn!(|ctx: &ErrorContext| unsafe {
// XXX: TODO: allow args to be parsed instead of just `null_mut` // XXX: TODO: allow args to be parsed instead of just `null_mut`
sys::nix_store_open(ctx.as_ptr(), uri_ptr, null_mut()) sys::nix_store_open(ctx.as_ptr(), uri_ptr, null_mut())
})?; })?;
Ok(Store { inner }) Ok(Rc::new(RefCell::new(Store { inner })))
} }
/// Realize a store path. /// Realize a store path.