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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::convert::TryFrom;
use crate::convert::{Error, FromMrb, TryFromMrb};
use crate::sys;
use crate::value::types::{Ruby, Rust};
use crate::value::Value;
use crate::Mrb;
mod boolean;
mod bytes;
mod fixnum;
mod float;
mod string;
pub use self::boolean::*;
pub use self::bytes::*;
pub use self::fixnum::*;
pub use self::float::*;
pub use self::string::*;
impl FromMrb<Vec<Value>> for Value {
type From = Rust;
type To = Ruby;
fn from_mrb(interp: &Mrb, value: Vec<Self>) -> Self {
let mrb = interp.borrow().mrb;
let array =
unsafe { sys::mrb_ary_new_capa(mrb, i64::try_from(value.len()).unwrap_or_default()) };
for (idx, item) in value.iter().enumerate() {
unsafe {
sys::mrb_ary_set(
mrb,
array,
i64::try_from(idx).unwrap_or_default(),
item.inner(),
)
};
}
Self::new(interp, array)
}
}
impl FromMrb<Vec<Option<Value>>> for Value {
type From = Rust;
type To = Ruby;
fn from_mrb(interp: &Mrb, value: Vec<Option<Self>>) -> Self {
let mrb = interp.borrow().mrb;
let array =
unsafe { sys::mrb_ary_new_capa(mrb, i64::try_from(value.len()).unwrap_or_default()) };
for (idx, item) in value.iter().enumerate() {
if let Some(item) = item {
unsafe {
sys::mrb_ary_set(
mrb,
array,
i64::try_from(idx).unwrap_or_default(),
item.inner(),
)
};
} else {
unsafe {
sys::mrb_ary_set(
mrb,
array,
i64::try_from(idx).unwrap_or_default(),
sys::mrb_sys_nil_value(),
)
};
}
}
Self::new(interp, array)
}
}
impl TryFromMrb<Value> for Vec<Value> {
type From = Ruby;
type To = Rust;
unsafe fn try_from_mrb(
interp: &Mrb,
value: Value,
) -> Result<Self, Error<Self::From, Self::To>> {
let mrb = interp.borrow().mrb;
match value.ruby_type() {
Ruby::Array => {
let array = value.inner();
let size = sys::mrb_sys_ary_len(array);
let cap = usize::try_from(size).map_err(|_| Error {
from: Ruby::Array,
to: Rust::Vec,
})?;
let mut items = Self::with_capacity(cap);
for idx in 0..size {
let item = Value::new(interp, sys::mrb_ary_ref(mrb, array, idx));
items.push(item);
}
Ok(items)
}
type_tag => Err(Error {
from: type_tag,
to: Rust::Vec,
}),
}
}
}
impl TryFromMrb<Value> for Vec<Option<Value>> {
type From = Ruby;
type To = Rust;
unsafe fn try_from_mrb(
interp: &Mrb,
value: Value,
) -> Result<Self, Error<Self::From, Self::To>> {
let mrb = interp.borrow().mrb;
match value.ruby_type() {
Ruby::Array => {
let array = value.inner();
let size = sys::mrb_sys_ary_len(array);
let cap = usize::try_from(size).map_err(|_| Error {
from: Ruby::Array,
to: Rust::Vec,
})?;
let mut items = Self::with_capacity(cap);
for idx in 0..size {
let element = sys::mrb_ary_ref(mrb, array, idx);
if sys::mrb_sys_value_is_nil(element) {
items.push(None);
} else {
items.push(Some(Value::new(interp, element)));
}
}
Ok(items)
}
type_tag => Err(Error {
from: type_tag,
to: Rust::Vec,
}),
}
}
}