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
77
78
79
80
81
82
83
84
85
use crate::{
ty::{array_ty, Type},
value::Value,
};
use std::fmt::{Debug, Display};
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArrayValue(pub Vec<Value>);
impl ArrayValue {
pub fn new_uniform(length: usize, value: Value) -> Self {
ArrayValue(std::iter::repeat(value).take(length).collect())
}
pub fn new(values: Vec<Value>) -> Self {
ArrayValue(values)
}
pub fn zero(length: usize, ty: &Type) -> Self {
ArrayValue::new_uniform(length, Value::zero(ty))
}
pub fn ty(&self) -> Type {
array_ty(
self.0.len(),
self.0.get(0).expect("empty array has no proper type").ty(),
)
}
}
impl Display for ArrayValue {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut first = true;
write!(f, "[")?;
for v in &self.0 {
if !first {
write!(f, ", ")?;
}
write!(f, "{}", v)?;
first = false;
}
write!(f, "]")
}
}
impl Debug for ArrayValue {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl ArrayValue {
pub fn extract_field(&self, idx: usize) -> Value {
self.0[idx].clone()
}
pub fn extract_slice(&self, off: usize, len: usize) -> ArrayValue {
ArrayValue::new(self.0[off..off + len].to_vec())
}
pub fn insert_field(&mut self, idx: usize, value: Value) {
self.0[idx] = value;
}
pub fn insert_slice(&mut self, off: usize, len: usize, value: &ArrayValue) {
assert_eq!(len, value.0.len());
self.0[off..off + len].clone_from_slice(&value.0);
}
}