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
use crate::{
FromValue, OwnedMut, OwnedRef, RawOwnedMut, RawOwnedRef, Shared, ToValue, UnsafeFromValue,
Value, VmError,
};
impl<T> ToValue for Option<T>
where
T: ToValue,
{
fn to_value(self) -> Result<Value, VmError> {
Ok(Value::Option(Shared::new(match self {
Some(some) => {
let value = some.to_value()?;
Some(value)
}
None => None,
})))
}
}
impl<T> FromValue for Option<T>
where
T: FromValue,
{
fn from_value(value: Value) -> Result<Self, VmError> {
Ok(match value.into_option()?.take()? {
Some(some) => Some(T::from_value(some)?),
None => None,
})
}
}
impl UnsafeFromValue for &Option<Value> {
type Output = *const Option<Value>;
type Guard = RawOwnedRef;
unsafe fn unsafe_from_value(value: Value) -> Result<(Self::Output, Self::Guard), VmError> {
let option = value.into_option()?;
Ok(OwnedRef::into_raw(option.owned_ref()?))
}
unsafe fn to_arg(output: Self::Output) -> Self {
&*output
}
}
impl UnsafeFromValue for &mut Option<Value> {
type Output = *mut Option<Value>;
type Guard = RawOwnedMut;
unsafe fn unsafe_from_value(value: Value) -> Result<(Self::Output, Self::Guard), VmError> {
let option = value.into_option()?;
Ok(OwnedMut::into_raw(option.owned_mut()?))
}
unsafe fn to_arg(output: Self::Output) -> Self {
&mut *output
}
}