2022-08-14 02:08:11 +02:00
|
|
|
//! This module implements Nix lists.
|
2022-12-29 12:44:09 +01:00
|
|
|
use std::ops::Index;
|
|
|
|
|
2022-12-29 15:08:14 +01:00
|
|
|
use imbl::{vector, Vector};
|
2022-10-23 19:37:11 +02:00
|
|
|
|
2023-01-10 12:52:59 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-12-24 18:18:26 +01:00
|
|
|
|
2022-09-18 20:04:40 +02:00
|
|
|
use crate::errors::ErrorKind;
|
2022-09-18 21:13:20 +02:00
|
|
|
use crate::vm::VM;
|
2022-09-18 20:04:40 +02:00
|
|
|
|
2022-10-22 15:51:29 +02:00
|
|
|
use super::thunk::ThunkSet;
|
|
|
|
use super::TotalDisplay;
|
2022-08-09 16:08:10 +02:00
|
|
|
use super::Value;
|
|
|
|
|
2022-08-12 16:21:24 +02:00
|
|
|
#[repr(transparent)]
|
2023-01-10 12:52:59 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2022-12-29 12:44:09 +01:00
|
|
|
pub struct NixList(Vector<Value>);
|
2022-08-09 16:08:10 +02:00
|
|
|
|
2022-10-22 15:51:29 +02:00
|
|
|
impl TotalDisplay for NixList {
|
|
|
|
fn total_fmt(&self, f: &mut std::fmt::Formatter<'_>, set: &mut ThunkSet) -> std::fmt::Result {
|
2022-08-09 16:50:27 +02:00
|
|
|
f.write_str("[ ")?;
|
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
for v in self {
|
2022-10-22 15:51:29 +02:00
|
|
|
v.total_fmt(f, set)?;
|
2022-08-09 16:50:27 +02:00
|
|
|
f.write_str(" ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
f.write_str("]")
|
2022-08-09 16:08:10 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-11 10:50:38 +02:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
impl From<Vector<Value>> for NixList {
|
|
|
|
fn from(vs: Vector<Value>) -> Self {
|
|
|
|
Self(vs)
|
2022-09-18 20:04:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:50:38 +02:00
|
|
|
impl NixList {
|
2022-08-30 02:33:30 +02:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
|
2022-09-05 20:41:50 +02:00
|
|
|
pub fn get(&self, i: usize) -> Option<&Value> {
|
|
|
|
self.0.get(i)
|
|
|
|
}
|
|
|
|
|
2023-01-10 20:33:27 +01:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:55:59 +02:00
|
|
|
pub fn construct(count: usize, stack_slice: Vec<Value>) -> Self {
|
|
|
|
debug_assert!(
|
|
|
|
count == stack_slice.len(),
|
|
|
|
"NixList::construct called with count == {}, but slice.len() == {}",
|
|
|
|
count,
|
|
|
|
stack_slice.len(),
|
|
|
|
);
|
|
|
|
|
2022-12-29 13:50:19 +01:00
|
|
|
NixList(Vector::from_iter(stack_slice.into_iter()))
|
2022-08-11 10:55:59 +02:00
|
|
|
}
|
2022-08-24 16:41:23 +02:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
pub fn iter(&self) -> vector::Iter<Value> {
|
2022-08-31 03:47:20 +02:00
|
|
|
self.0.iter()
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:34:09 +01:00
|
|
|
pub fn ptr_eq(&self, other: &Self) -> bool {
|
2022-12-29 12:44:09 +01:00
|
|
|
self.0.ptr_eq(&other.0)
|
2022-11-23 09:34:09 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 20:04:40 +02:00
|
|
|
/// Compare `self` against `other` for equality using Nix equality semantics
|
2022-09-18 21:13:20 +02:00
|
|
|
pub fn nix_eq(&self, other: &Self, vm: &mut VM) -> Result<bool, ErrorKind> {
|
2022-11-23 09:34:09 +01:00
|
|
|
if self.ptr_eq(other) {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
2022-09-18 20:04:40 +02:00
|
|
|
if self.len() != other.len() {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (v1, v2) in self.iter().zip(other.iter()) {
|
2022-09-18 21:13:20 +02:00
|
|
|
if !v1.nix_eq(v2, vm)? {
|
2022-09-18 20:04:40 +02:00
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|
2022-10-26 11:02:09 +02:00
|
|
|
|
|
|
|
/// force each element of the list (shallowly), making it safe to call .get().value()
|
|
|
|
pub fn force_elements(&self, vm: &mut VM) -> Result<(), ErrorKind> {
|
|
|
|
self.iter().try_for_each(|v| v.force(vm).map(|_| ()))
|
|
|
|
}
|
2022-11-23 09:34:09 +01:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
pub fn into_inner(self) -> Vector<Value> {
|
|
|
|
self.0
|
2022-11-23 09:34:09 +01:00
|
|
|
}
|
2022-12-29 13:50:19 +01:00
|
|
|
|
|
|
|
#[deprecated(note = "callers should avoid constructing from Vec")]
|
|
|
|
pub fn from_vec(vs: Vec<Value>) -> Self {
|
|
|
|
Self(Vector::from_iter(vs.into_iter()))
|
|
|
|
}
|
2022-08-11 10:50:38 +02:00
|
|
|
}
|
2022-10-02 18:19:13 +02:00
|
|
|
|
|
|
|
impl IntoIterator for NixList {
|
|
|
|
type Item = Value;
|
2022-12-29 15:08:14 +01:00
|
|
|
type IntoIter = imbl::vector::ConsumingIter<Value>;
|
2022-10-02 18:19:13 +02:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
2022-10-02 18:19:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 04:47:23 +02:00
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a NixList {
|
|
|
|
type Item = &'a Value;
|
2022-12-29 15:08:14 +01:00
|
|
|
type IntoIter = imbl::vector::Iter<'a, Value>;
|
2022-10-13 04:47:23 +02:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 19:37:11 +02:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
impl Index<usize> for NixList {
|
|
|
|
type Output = Value;
|
2022-10-23 19:37:11 +02:00
|
|
|
|
2022-12-29 12:44:09 +01:00
|
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
|
|
&self.0[index]
|
2022-10-23 19:37:11 +02:00
|
|
|
}
|
|
|
|
}
|