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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use webcore::try_from::{TryFrom, TryInto};
use webcore::value::{Reference, Value, ConversionError, FromReferenceUnchecked};
use webcore::serialization::{JsSerializable, deserialize_array};
#[derive(Clone, PartialEq, Debug)]
pub struct Array( Reference );
impl FromReferenceUnchecked for Array {
unsafe fn from_reference_unchecked( reference: Reference ) -> Self {
Array( reference )
}
}
impl From< Array > for Reference {
fn from( array: Array ) -> Self {
array.0.clone()
}
}
impl Array {
#[inline]
pub(crate) fn as_reference( &self ) -> &Reference {
&self.0
}
pub fn len( &self ) -> usize {
js!(
return @{self}.length;
).try_into().unwrap()
}
}
impl From< Array > for Vec< Value > {
fn from( array: Array ) -> Self {
deserialize_array( &array.0, |iter| iter.collect() )
}
}
impl< 'a > From< &'a Array > for Vec< Value > {
fn from( array: &'a Array ) -> Self {
deserialize_array( &array.0, |iter| iter.collect() )
}
}
impl< 'a > From< &'a mut Array > for Vec< Value > {
fn from( array: &'a mut Array ) -> Self {
deserialize_array( &array.0, |iter| iter.collect() )
}
}
impl< V > From< Vec< V > > for Array where V: JsSerializable {
#[inline]
fn from( value: Vec< V > ) -> Self {
let value: &[V] = &value;
value.into()
}
}
impl< 'a, V > From< &'a Vec< V > > for Array where V: JsSerializable {
#[inline]
fn from( value: &'a Vec< V > ) -> Self {
let value: &[V] = &value;
value.into()
}
}
impl< 'a, V > From< &'a mut Vec< V > > for Array where V: JsSerializable {
#[inline]
fn from( value: &'a mut Vec< V > ) -> Self {
let value: &[V] = &value;
value.into()
}
}
impl< 'a, V > From< &'a [V] > for Array where V: JsSerializable {
#[inline]
fn from( value: &'a [V] ) -> Self {
let value = js! {
return @{value};
};
match value {
Value::Array( array ) => return array,
_ => unreachable!()
}
}
}
impl< 'a, V > From< &'a mut [V] > for Array where V: JsSerializable {
#[inline]
fn from( value: &'a mut [V] ) -> Self {
let value: &[V] = value;
value.into()
}
}
impl< E: Into< ConversionError >, V: TryFrom< Value, Error = E > > TryFrom< Array > for Vec< V > {
type Error = ConversionError;
fn try_from( array: Array ) -> Result< Self, Self::Error > {
deserialize_array( array.as_reference(), |deserializer| {
let mut output = Vec::with_capacity( deserializer.len() );
for value in deserializer {
let result: Result< _, E > = value.try_into();
let value = match result {
Ok( value ) => value,
Err( error ) => {
return Err( ConversionError::value_conversion_error( error.into() ) );
}
};
output.push( value );
}
Ok( output )
})
}
}