plonk_wasm/
wasm_vector.rs

1use core::{convert::From, ops::Deref};
2use paste::paste;
3use wasm_bindgen::{
4    convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi},
5    prelude::*,
6};
7use wasm_types::FlatVector as WasmFlatVector;
8
9#[derive(Clone, Debug)]
10pub struct WasmVector<T>(Vec<T>);
11
12impl<T> Deref for WasmVector<T> {
13    type Target = Vec<T>;
14
15    fn deref(&self) -> &Self::Target {
16        &self.0
17    }
18}
19
20impl<T> From<Vec<T>> for WasmVector<T> {
21    fn from(x: Vec<T>) -> Self {
22        WasmVector(x)
23    }
24}
25
26impl<T> From<WasmVector<T>> for Vec<T> {
27    fn from(x: WasmVector<T>) -> Self {
28        x.0
29    }
30}
31
32impl<'a, T> From<&'a WasmVector<T>> for &'a Vec<T> {
33    fn from(x: &'a WasmVector<T>) -> Self {
34        &x.0
35    }
36}
37
38impl<T> core::iter::IntoIterator for WasmVector<T> {
39    type Item = <Vec<T> as core::iter::IntoIterator>::Item;
40    type IntoIter = <Vec<T> as core::iter::IntoIterator>::IntoIter;
41    fn into_iter(self) -> Self::IntoIter {
42        self.0.into_iter()
43    }
44}
45
46impl<'a, T> core::iter::IntoIterator for &'a WasmVector<T> {
47    type Item = <&'a Vec<T> as core::iter::IntoIterator>::Item;
48    type IntoIter = <&'a Vec<T> as core::iter::IntoIterator>::IntoIter;
49    fn into_iter(self) -> Self::IntoIter {
50        self.0.iter()
51    }
52}
53
54impl<T> core::iter::FromIterator<T> for WasmVector<T> {
55    fn from_iter<I>(iter: I) -> WasmVector<T>
56    where
57        I: IntoIterator<Item = T>,
58    {
59        WasmVector(core::iter::FromIterator::from_iter(iter))
60    }
61}
62
63impl<T> core::default::Default for WasmVector<T> {
64    fn default() -> Self {
65        WasmVector(core::default::Default::default())
66    }
67}
68
69impl<T> core::iter::Extend<T> for WasmVector<T> {
70    fn extend<I>(&mut self, iter: I)
71    where
72        I: IntoIterator<Item = T>,
73    {
74        self.0.extend(iter)
75    }
76}
77
78impl<T> wasm_bindgen::describe::WasmDescribe for WasmVector<T> {
79    fn describe() {
80        <Vec<u32> as wasm_bindgen::describe::WasmDescribe>::describe()
81    }
82}
83
84impl<T: FromWasmAbi<Abi = u32>> FromWasmAbi for WasmVector<T> {
85    type Abi = <Vec<u32> as FromWasmAbi>::Abi;
86    unsafe fn from_abi(js: Self::Abi) -> Self {
87        let pointers: Vec<u32> = FromWasmAbi::from_abi(js);
88        WasmVector(
89            pointers
90                .into_iter()
91                .map(|x| FromWasmAbi::from_abi(x))
92                .collect(),
93        )
94    }
95}
96
97impl<T: FromWasmAbi<Abi = u32>> OptionFromWasmAbi for WasmVector<T> {
98    fn is_none(x: &Self::Abi) -> bool {
99        <Vec<u32> as OptionFromWasmAbi>::is_none(x)
100    }
101}
102
103impl<T: IntoWasmAbi<Abi = u32>> IntoWasmAbi for WasmVector<T> {
104    type Abi = <Vec<u32> as FromWasmAbi>::Abi;
105    fn into_abi(self) -> Self::Abi {
106        let pointers: Vec<u32> = self
107            .0
108            .into_iter()
109            .map(|x| IntoWasmAbi::into_abi(x))
110            .collect();
111        IntoWasmAbi::into_abi(pointers)
112    }
113}
114
115impl<T: IntoWasmAbi<Abi = u32>> OptionIntoWasmAbi for WasmVector<T> {
116    fn none() -> Self::Abi {
117        <Vec<u32> as OptionIntoWasmAbi>::none()
118    }
119}
120
121macro_rules! impl_vec_vec_fp {
122    ( $F:ty, $WasmF:ty ) => {
123        paste! {
124            #[wasm_bindgen]
125            pub struct [<WasmVecVec $F:camel>](#[wasm_bindgen(skip)] pub Vec<Vec<$F>>);
126
127            #[wasm_bindgen]
128            impl [<WasmVecVec $F:camel>] {
129                #[wasm_bindgen(constructor)]
130                pub fn create(n: i32) -> Self {
131                    [<WasmVecVec $F:camel>](Vec::with_capacity(n as usize))
132                }
133
134                #[wasm_bindgen]
135                pub fn push(&mut self, x: WasmFlatVector<$WasmF>) {
136                    self.0.push(x.into_iter().map(Into::into).collect())
137                }
138
139                #[wasm_bindgen]
140                pub fn get(&self, i: i32) -> WasmFlatVector<$WasmF> {
141                    self.0[i as usize].clone().into_iter().map(Into::into).collect()
142                }
143
144                #[wasm_bindgen]
145                pub fn set(&mut self, i: i32, x: WasmFlatVector<$WasmF>) {
146                    self.0[i as usize] = x.into_iter().map(Into::into).collect()
147                }
148            }
149        }
150    };
151}
152
153pub mod fp {
154    use super::*;
155    use arkworks::WasmPastaFp;
156    use mina_curves::pasta::Fp;
157
158    impl_vec_vec_fp!(Fp, WasmPastaFp);
159}
160
161pub mod fq {
162    use super::*;
163    use arkworks::WasmPastaFq;
164    use mina_curves::pasta::Fq;
165
166    impl_vec_vec_fp!(Fq, WasmPastaFq);
167}