difftreelog
feat list building
in: trunk
1 file changed
crates/nix-eval/src/lib.rsdiffbeforeafterboth1use std::alloc::{GlobalAlloc, Layout};2use std::borrow::Cow;1use std::borrow::Cow;3use std::cell::RefCell;2use std::cell::RefCell;4use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};3use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};582 }581 }583}582}583584struct ListBuilder(*mut c_list_builder, c_uint);585impl ListBuilder {586 fn new(capacity: usize) -> Self {587 with_default_context(|c, es| unsafe { make_list_builder(c, es, capacity) })588 .map(|l| Self(l, 0))589 .expect("alloc should not fail")590 }591}592impl ListBuilder {593 fn push(&mut self, v: Value) {594 with_default_context(|c, _| unsafe {595 list_builder_insert(596 c,597 self.0,598 {599 let v = self.1;600 self.1 += 1;601 v602 },603 v.0,604 )605 })606 .expect("list insert shouldn't fail");607 }608}609impl Drop for ListBuilder {610 fn drop(&mut self) {611 unsafe { list_builder_free(self.0) };612 }613}584614585impl Value {615impl Value {586 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {616 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {595 out625 out596 }626 }597 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {627 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {598 todo!()628 let out = Self::new_uninit();629 let mut b = ListBuilder::new(v.len());630 for v in v {631 b.push(v.into());632 }633 with_default_context(|c, _| unsafe { make_list(c, b.0, out.0) })634 .expect("list initialization should not fail");635636 out599 }637 }600 fn new_uninit() -> Self {638 fn new_uninit() -> Self {601 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })639 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })914 let nix_ctx = NixContext::new();952 let nix_ctx = NixContext::new();915 let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;953 let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;916954917 nix_raw::store_get_fs_closure(1);955 // nix_raw::store_get_fs_closure(1);918956919 Ok(())957 Ok(())920}958}