Skip to main content

ExternalValueHandle

Struct ExternalValueHandle 

Source
pub struct ExternalValueHandle<'s> { /* private fields */ }
Expand description

A handle to a Nix external value that retains the ExternalValue*.

nix_get_external is broken in Nix 2.32.7; see the module-level note. This type stores the ExternalValue* from nix_create_external_value directly and uses nix_get_external_value_content for downcasting.

Derefs to Value.

Implementations§

Source§

impl ExternalValueHandle<'_>

Source

pub fn as_external<T: NixExternal>(&self) -> Result<&T>

Downcast to a concrete Rust type T.

§Errors

Returns Error::InvalidType if the stored TypeId does not match T.

Methods from Deref<Target = Value<'s>>§

Source

pub fn get_attr(&self, key: &str) -> Result<Value<'_>>

Get an attribute by name.

Returns the value associated with the given attribute name.

§Errors

Returns an error if the value is not an attribute set or the key does not exist.

Source

pub fn attr_keys(&self) -> Result<Vec<String>>

Get all attribute keys.

Returns a vector of all attribute names in this attribute set.

§Errors

Returns an error if the value is not an attribute set.

Source

pub fn has_attr(&self, key: &str) -> Result<bool>

Check if an attribute exists.

Returns true if the attribute set contains the given key.

§Errors

Returns an error if the value is not an attribute set.

Source

pub fn attrs(&self) -> Result<AttrIterator<'_>>

Create an iterator over key-value pairs.

§Returns

An iterator over all key-value pairs in the attribute set.

§Errors

Returns an error if the value is not an attribute set.

Source

pub fn is_list(&self) -> bool

Check if this value is a list.

§Example
use std::sync::Arc;

use nix_bindings::{Context, EvalStateBuilder, Store};
fn main() -> Result<(), Box<dyn std::error::Error>> {
  let ctx = Arc::new(Context::new()?);
  let store = Arc::new(Store::open(&ctx, None)?);
  let state = EvalStateBuilder::new(&store)?.build()?;
  let list = state.eval_from_string("[1 2 3]", "<eval>")?;
  assert!(list.is_list());
  Ok(())
}
Source

pub fn list_len(&self) -> Result<usize>

Get the length of this list.

§Errors

Returns an error if this value is not a list.

§Example
let list = state.eval_from_string("[1 2 3]", "<eval>")?;
assert_eq!(list.list_len()?, 3);
Source

pub fn list_get(&self, idx: usize) -> Result<Value<'_>>

Get an element from this list by index.

§Arguments
  • idx - The index of the element to retrieve (0-based)
§Errors

Returns an error if this value is not a list or the index is out of bounds.

§Example
let list = state.eval_from_string("[1 2 3]", "<eval>")?;
let first = list.list_get(0)?;
assert_eq!(first.as_int()?, 1);
Source

pub fn list_iter(&self) -> Result<ListIterator<'_>>

Create an iterator over the elements of this list.

§Errors

Returns an error if this value is not a list.

Source

pub fn value_type(&self) -> ValueType

Get the type of this value.

Does not force; a lazy attribute or list element reports as ValueType::Thunk until forced. Use force or any as_* accessor (which force implicitly) first.

Source

pub fn type_name(&self) -> String

Get the Nix type name of this value as reported by the C API.

Wraps nix_get_typename. Returns Nix’s source-of-truth string (e.g. "int", "thunk", "lambda"). Falls back to the local [ValueType::to_string] if the C call returns null.

Source

pub fn as_int(&self) -> Result<i64>

Convert this value to an integer. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not an integer.

Source

pub fn as_float(&self) -> Result<f64>

Convert this value to a float. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not a float.

Source

pub fn as_bool(&self) -> Result<bool>

Convert this value to a boolean. Forces the value first.

§Errors

Returns an error if forcing fails or the resolved value is not a boolean.

Source

pub fn as_string(&self) -> Result<String>

Convert this value to a string.

Realises any string context. Forces the value first.

§Errors

Returns an error if forcing fails or the value is not a string.

Source

pub fn as_string_with_context(&self) -> Result<(String, Vec<StorePath>)>

Convert this value to a string and return its store-path context.

Extended form of as_string returning both content and any store paths embedded in the string’s context. For ordinary strings the context vector is empty.

§Errors

Returns an error if the value is not a string.

Source

pub fn as_path(&self) -> Result<PathBuf>

Convert this value to a filesystem path. Forces the value first.

§Errors

Returns an error if forcing fails or the value is not a path.

Source

pub fn call(&self, arg: &Value<'_>) -> Result<Value<'_>>

Call this value as a function with a single argument.

§Errors

Returns an error if this value is not a function or the call fails.

Source

pub fn call_multi(&self, args: &[&Value<'_>]) -> Result<Value<'_>>

Call this value as a curried function with multiple arguments.

§Errors

Returns an error if this value is not a function or the call fails.

Source

pub fn copy(&self) -> Result<Value<'_>>

Copy this value into a new owned value slot.

§Errors

Returns an error if the copy fails.

Source

pub fn to_nix_string(&self) -> Result<String>

Format this value as Nix syntax.

Forces the value first and recursively renders attribute sets and lists. Functions, thunks, and external values render as opaque placeholders (<lambda>, <thunk>, <external>) the same way nix-instantiate --eval does.

§Errors

Returns an error if forcing fails or any nested value cannot be rendered.

Trait Implementations§

Source§

impl<'s> Deref for ExternalValueHandle<'s>

Source§

type Target = Value<'s>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Send for ExternalValueHandle<'_>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.