values stuff i wrote when i woke up

This commit is contained in:
do butterflies cry? 2026-03-28 10:37:24 +10:00
parent ccfd46cd21
commit d5bc2400c9
Signed by: cry
GPG key ID: F68745A836CA0412
8 changed files with 78 additions and 31 deletions

View file

@ -50,7 +50,7 @@ impl EvalStateBuilder {
sys::nix_eval_state_builder_new(ctx.as_ptr(), store.as_ptr())
})?;
sys::nix_eval_state_builder_load(context, builder);
// sys::nix_eval_state_builder_load(context, builder);
// sys::nix_flake_settings_add_to_eval_state_builder(context, settings, builder);
Ok(EvalStateBuilder {

View file

@ -14,7 +14,7 @@ use crate::{EvalState, NixideResult, StorePath};
pub struct RealisedString<'a> {
inner: RefCell<NonNull<sys::nix_realised_string>>,
pub path: StorePath,
pub children: LazyArray<StorePath, &'a dyn Fn(usize) -> StorePath>,
pub children: LazyArray<StorePath, Box<dyn Fn(usize) -> StorePath + 'a>>,
}
impl<'a> AsInnerPtr<sys::nix_realised_string> for RealisedString<'a> {
@ -76,12 +76,13 @@ impl<'a> RealisedString<'a> {
let size = unsafe { sys::nix_realised_string_get_store_path_count(inner.as_ptr()) };
let delegate = &|_| StorePath::fake_path(unsafe { state.store_ref() }).unwrap();
Ok(Self {
inner: cell,
path: Self::parse_path(inner.as_ptr(), state),
children: LazyArray::new(size, &delegate),
children: LazyArray::new(
size,
Box::new(|_| StorePath::fake_path(unsafe { state.store_ref() }).unwrap()),
),
})
}

View file

@ -7,30 +7,38 @@ use crate::sys;
use crate::util::wrappers::AsInnerPtr;
use crate::util::{panic_issue_call_failed, wrap};
pub use crate::expr::values::{NixBool, NixFloat, NixInt, NixString, NixValue};
/// A Nix value
///
/// This represents any value in the Nix language, including primitives,
/// collections, and functions.
pub struct Value<'a> {
pub(crate) inner: NonNull<sys::nix_value>,
state: &'a EvalState,
}
impl<'a> AsInnerPtr<sys::nix_value> for Value<'a> {
#[inline]
unsafe fn as_ptr(&self) -> *mut sys::nix_value {
self.inner.as_ptr()
}
#[inline]
unsafe fn as_ref(&self) -> &sys::nix_value {
unsafe { self.inner.as_ref() }
}
#[inline]
unsafe fn as_mut(&mut self) -> &mut sys::nix_value {
unsafe { self.inner.as_mut() }
}
///
/// ```cpp
/// pub const ValueType_NIX_TYPE_THUNK: ValueType = 0;
/// pub const ValueType_NIX_TYPE_INT: ValueType = 1;
/// pub const ValueType_NIX_TYPE_FLOAT: ValueType = 2;
/// pub const ValueType_NIX_TYPE_BOOL: ValueType = 3;
/// pub const ValueType_NIX_TYPE_STRING: ValueType = 4;
/// pub const ValueType_NIX_TYPE_PATH: ValueType = 5;
/// pub const ValueType_NIX_TYPE_NULL: ValueType = 6;
/// pub const ValueType_NIX_TYPE_ATTRS: ValueType = 7;
/// pub const ValueType_NIX_TYPE_LIST: ValueType = 8;
/// pub const ValueType_NIX_TYPE_FUNCTION: ValueType = 9;
/// pub const ValueType_NIX_TYPE_EXTERNAL: ValueType = 10;
/// ```
pub enum Value {
// Thunk(NixThunk),
Int(NixInt),
Float(NixFloat),
Bool(NixBool),
String(NixString),
// Path(NixPath),
// Null(NixNull),
// Attrs(NixAttrs),
// List(NixList),
// Function(NixFunction),
// External(NixExternal),
}
impl<'a> Value<'a> {

View file

@ -13,6 +13,15 @@ pub struct NixBool {
value: bool,
}
impl Drop for NixBool {
fn drop(&mut self) {
let ctx = ErrorContext::new();
unsafe {
sys::nix_value_decref(ctx.as_ptr(), self.as_ptr());
}
}
}
impl Display for NixBool {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "<bool>")
@ -44,7 +53,7 @@ impl AsInnerPtr<sys::nix_value> for NixBool {
impl NixValue for NixBool {
#[inline]
fn get_enum_id(&self) -> sys::ValueType {
fn id(&self) -> sys::ValueType {
sys::ValueType_NIX_TYPE_BOOL
}

View file

@ -12,6 +12,15 @@ pub struct NixFloat {
value: f64,
}
impl Drop for NixFloat {
fn drop(&mut self) {
let ctx = ErrorContext::new();
unsafe {
sys::nix_value_decref(ctx.as_ptr(), self.as_ptr());
}
}
}
impl Display for NixFloat {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "<float>")
@ -43,7 +52,7 @@ impl AsInnerPtr<sys::nix_value> for NixFloat {
impl NixValue for NixFloat {
#[inline]
fn get_enum_id(&self) -> sys::ValueType {
fn id(&self) -> sys::ValueType {
sys::ValueType_NIX_TYPE_FLOAT
}

View file

@ -12,6 +12,15 @@ pub struct NixInt {
value: i64,
}
impl Drop for NixInt {
fn drop(&mut self) {
let ctx = ErrorContext::new();
unsafe {
sys::nix_value_decref(ctx.as_ptr(), self.as_ptr());
}
}
}
impl Display for NixInt {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "<int>")
@ -43,7 +52,7 @@ impl AsInnerPtr<sys::nix_value> for NixInt {
impl NixValue for NixInt {
#[inline]
fn get_enum_id(&self) -> sys::ValueType {
fn id(&self) -> sys::ValueType {
sys::ValueType_NIX_TYPE_INT
}

View file

@ -21,9 +21,10 @@ use std::ptr::NonNull;
use crate::sys;
use crate::util::wrappers::AsInnerPtr;
pub trait NixValue: Display + Debug + AsInnerPtr<sys::nix_value> {
pub trait NixValue: Drop + Display + Debug + AsInnerPtr<sys::nix_value> {
/// TODO
fn get_enum_id(&self) -> sys::ValueType;
fn id(&self) -> sys::ValueType;
/// TODO
fn new(inner: NonNull<sys::nix_value>) -> Self;
}

View file

@ -3,6 +3,7 @@ use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ptr::NonNull;
use super::NixValue;
use crate::errors::ErrorContext;
use crate::expr::RealisedString;
use crate::util::panic_issue_call_failed;
use crate::util::wrap;
@ -14,6 +15,15 @@ pub struct NixString {
value: String,
}
impl Drop for NixString {
fn drop(&mut self) {
let ctx = ErrorContext::new();
unsafe {
sys::nix_value_decref(ctx.as_ptr(), self.as_ptr());
}
}
}
impl Display for NixString {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "<string>")
@ -45,7 +55,7 @@ impl AsInnerPtr<sys::nix_value> for NixString {
impl NixValue for NixString {
#[inline]
fn get_enum_id(&self) -> sys::ValueType {
fn id(&self) -> sys::ValueType {
sys::ValueType_NIX_TYPE_STRING
}