-
Notifications
You must be signed in to change notification settings - Fork 2
/
array.rs
32 lines (30 loc) · 1.01 KB
/
array.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
use super::*;
impl<T: TryFromMrb> TryFromMrb for Vec<T> {
fn try_from_mrb(value: MrbValue) -> MrbResult<Self> {
unsafe {
if minu_array_p(value.val) {
let len = minu_rarray_len(value.val);
let mut vec = Vec::with_capacity(len as _);
for i in 0..len {
let minu_val = minu_ary_ref(value.val, i);
let val = T::try_from_mrb(MrbValue::new(value.mrb, minu_val))?;
vec.push(val);
}
Ok(vec)
} else {
Err(MrbConversionError::new("Vec<T>"))
}
}
}
}
impl<T: TryIntoMrb> TryIntoMrb for Vec<T> {
fn try_into_mrb(self, mrb: *mut minu_state) -> MrbResult<MrbValue> {
unsafe {
let ary = minu_ary_new_capa(mrb, self.len() as _);
for v in self.into_iter() {
minu_ary_push(mrb, ary, v.try_into_mrb(mrb)?.val)
}
return Ok(MrbValue::new(mrb, ary));
}
}
}