about summary refs log tree commit diff
path: root/tvix/eval/src/value/list.rs
blob: 19415c0d557fbc333eb280f1ce1f9b74d5068bc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! This module implements Nix lists.
use std::fmt::Display;

use super::Value;

#[repr(transparent)]
#[derive(Clone, Debug, PartialEq)]
pub struct NixList(Vec<Value>);

impl Display for NixList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("[ ")?;

        for v in &self.0 {
            v.fmt(f)?;
            f.write_str(" ")?;
        }

        f.write_str("]")
    }
}

#[cfg(feature = "arbitrary")]
mod arbitrary {
    use proptest::{
        prelude::{any_with, Arbitrary},
        strategy::{BoxedStrategy, Strategy},
    };

    use super::*;

    impl Arbitrary for NixList {
        type Parameters = <Vec<Value> as Arbitrary>::Parameters;
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
            any_with::<Vec<Value>>(args).prop_map(Self).boxed()
        }
    }
}

impl NixList {
    pub fn concat(&self, other: &Self) -> Self {
        let mut lhs = self.clone();
        let mut rhs = other.clone();
        lhs.0.append(&mut rhs.0);
        lhs
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get(&self, i: usize) -> Option<&Value> {
        self.0.get(i)
    }

    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(),
        );

        NixList(stack_slice)
    }

    pub fn iter(&self) -> std::slice::Iter<Value> {
        self.0.iter()
    }

    pub fn into_iter(self) -> std::vec::IntoIter<Value> {
        self.0.into_iter()
    }
}