-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist_ref.rs
101 lines (87 loc) · 2.39 KB
/
list_ref.rs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::marker::PhantomData;
use crate::{
StackShared,
prim_ref::{
PrimExpr,
},
rec_ref::{
ListToRecMappable,
},
MapKV,
Ref,
};
pub trait ToListMappable {
type O;
fn do_map(self, base: String) -> Self::O;
}
pub trait RecToListMappable {
type O;
fn do_map(self, base: String) -> Self::O;
}
pub struct ListRef<T> {
pub(crate) shared: StackShared,
pub(crate) base: String,
_pd: PhantomData<T>,
}
impl<T> Ref for ListRef<T> {
fn new(shared: StackShared, base: String) -> Self {
ListRef {
shared: shared,
base: base,
_pd: Default::default(),
}
}
}
impl<T: Ref> ListRef<T> {
pub fn get(&self, index: usize) -> T {
T::new(self.shared.clone(), format!("{}[{}]", &self.base, index))
}
pub fn map<O: ToListMappable>(&self, inner: impl FnOnce(MapKV<T>) -> O) -> O::O {
let out = inner(MapKV::new(self.shared.clone()));
out.do_map(self.base.clone())
}
pub fn map_rec<O: ListToRecMappable>(&self, inner: impl FnOnce(MapKV<T>) -> (PrimExpr<String>, O)) -> O::O {
let (k, out) = inner(MapKV::new(self.shared.clone()));
out.do_map_rec(self.base.clone(), k)
}
}
pub struct MapListRef<T> {
pub(crate) shared: StackShared,
pub(crate) base: String,
pub(crate) map_base: String,
_pd: PhantomData<T>,
}
impl<T> MapListRef<T> {
pub(crate) fn new(shared: StackShared, base: String, map_base: String) -> Self {
MapListRef {
shared: shared,
base: base,
map_base: map_base,
_pd: Default::default(),
}
}
}
impl<T: Ref> MapListRef<T> {
pub fn map<O: ToListMappable>(&self, inner: impl FnOnce(T) -> O) -> O::O {
let out = inner(T::new(self.shared.clone(), self.map_base.to_string()));
out.do_map(self.base.clone())
}
}
pub struct MapListRefToRec<T> {
pub(crate) shared: StackShared,
pub(crate) base: String,
pub(crate) map_base_key: String,
pub(crate) map_base: String,
_pd: PhantomData<T>,
}
impl<T> MapListRefToRec<T> {
pub(crate) fn new(shared: StackShared, base: String, map_base_key: String, map_base: String) -> Self {
MapListRefToRec {
shared: shared,
base: base,
map_base_key: map_base_key,
map_base: map_base,
_pd: Default::default(),
}
}
}