too drunk rn idk

This commit is contained in:
do butterflies cry? 2026-03-28 00:35:11 +10:00
parent c15b025777
commit ccfd46cd21
Signed by: cry
GPG key ID: F68745A836CA0412
2 changed files with 18 additions and 31 deletions

View file

@ -1,6 +1,6 @@
use std::cell::RefCell;
use std::ffi::c_char; use std::ffi::c_char;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::sync::Arc;
use crate::errors::ErrorContext; use crate::errors::ErrorContext;
use crate::expr::values::NixString; use crate::expr::values::NixString;
@ -11,31 +11,30 @@ use crate::util::LazyArray;
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};
pub struct RealisedString { pub struct RealisedString<'a> {
inner: NonNull<sys::nix_realised_string>, inner: RefCell<NonNull<sys::nix_realised_string>>,
pub path: StorePath, pub path: StorePath,
pub children: pub children: LazyArray<StorePath, &'a dyn Fn(usize) -> StorePath>,
LazyArray<StorePath, fn(&LazyArray<StorePath, fn(usize) -> StorePath>, usize) -> StorePath>,
} }
impl AsInnerPtr<sys::nix_realised_string> for RealisedString { 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 {
self.inner.as_ptr() self.inner.borrow().as_ptr()
} }
#[inline] #[inline]
unsafe fn as_ref(&self) -> &sys::nix_realised_string { unsafe fn as_ref(&self) -> &sys::nix_realised_string {
unsafe { self.inner.as_ref() } unsafe { self.inner.borrow().as_ref() }
} }
#[inline] #[inline]
unsafe fn as_mut(&mut self) -> &mut sys::nix_realised_string { unsafe fn as_mut(&mut self) -> &mut sys::nix_realised_string {
unsafe { self.inner.as_mut() } unsafe { self.inner.borrow_mut().as_mut() }
} }
} }
impl Drop for RealisedString { impl<'a> Drop for RealisedString<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
sys::nix_realised_string_free(self.as_ptr()); sys::nix_realised_string_free(self.as_ptr());
@ -43,7 +42,7 @@ impl Drop for RealisedString {
} }
} }
impl RealisedString { impl<'a> RealisedString<'a> {
/// Realise a string context. /// Realise a string context.
/// ///
/// This will /// This will
@ -64,7 +63,7 @@ impl RealisedString {
/// # Returns /// # Returns
/// ///
/// NULL if failed, or a new nix_realised_string, which must be freed with nix_realised_string_free /// NULL if failed, or a new nix_realised_string, which must be freed with nix_realised_string_free
pub fn new(value: &NixString, state: &Arc<EvalState>) -> NixideResult<Self> { 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(
ctx.as_ptr(), ctx.as_ptr(),
@ -73,32 +72,20 @@ impl RealisedString {
false, // don't copy more false, // don't copy more
) )
})?; })?;
let cell = RefCell::new(inner);
fn delegate(
inner: &LazyArray<StorePath, Box<dyn Fn(usize) -> StorePath>>,
index: usize,
) -> StorePath {
// XXX: TODO
// inner[index]
StorePath::fake_path(unsafe { state.store_ref() }).unwrap()
}
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()) };
let delegate = &|_| StorePath::fake_path(unsafe { state.store_ref() }).unwrap();
Ok(Self { Ok(Self {
inner, inner: cell,
path: Self::parse_path(inner.as_ptr(), state), path: Self::parse_path(inner.as_ptr(), state),
children: LazyArray::<StorePath, Box<dyn Fn(usize) -> StorePath>>::new( children: LazyArray::new(size, &delegate),
size,
Box::new(delegate),
),
}) })
} }
fn parse_path( fn parse_path(realised_string: *mut sys::nix_realised_string, state: &EvalState) -> StorePath {
realised_string: *mut sys::nix_realised_string,
state: &Arc<EvalState>,
) -> 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) };

View file

@ -1,7 +1,7 @@
use std::cell::{Ref, RefCell}; use std::cell::{Ref, RefCell};
#[derive(Debug)] #[derive(Debug)]
pub struct LazyArray<T, F> pub struct LazyArray<T, F = fn(usize) -> T>
where where
F: Fn(usize) -> T, F: Fn(usize) -> T,
{ {