Files
nixjit/src/ty/internal/list.rs
2025-07-17 16:34:18 +08:00

71 lines
1.3 KiB
Rust

use std::ops::Deref;
use ecow::EcoVec;
use hashbrown::HashSet;
use crate::engine::Engine;
use crate::ty::public as p;
use super::Value;
#[derive(Clone, PartialEq, Debug)]
pub struct List {
data: EcoVec<Value>,
}
impl Default for List {
fn default() -> Self {
Self::new()
}
}
impl<T: Into<EcoVec<Value>>> From<T> for List {
fn from(value: T) -> Self {
Self { data: value.into() }
}
}
impl Deref for List {
type Target = [Value];
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl List {
pub fn new() -> Self {
List {
data: EcoVec::new(),
}
}
pub fn with_capacity(cap: usize) -> Self {
List {
data: EcoVec::with_capacity(cap),
}
}
pub fn push(&mut self, elem: Value) {
self.data.push(elem);
}
pub fn concat(&mut self, other: &List) {
for elem in other.data.iter() {
self.data.push(elem.clone());
}
}
pub fn into_inner(self) -> EcoVec<Value> {
self.data
}
pub fn to_public(&self, engine: &Engine, seen: &mut HashSet<Value>) -> p::Value {
p::Value::List(p::List::new(
self.data
.iter()
.map(|value| value.clone().to_public(engine, seen))
.collect(),
))
}
}