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
use crate::convert::{Error, FromMrb, TryFromMrb};
use crate::value::types::{Ruby, Rust};
use crate::value::Value;
use crate::Mrb;

impl FromMrb<Option<Vec<u8>>> for Value {
    type From = Rust;
    type To = Ruby;

    fn from_mrb(interp: &Mrb, value: Option<Vec<u8>>) -> Self {
        if let Some(value) = value {
            Self::from_mrb(interp, value)
        } else {
            Self::from_mrb(interp, None::<Self>)
        }
    }
}

impl FromMrb<Option<&[u8]>> for Value {
    type From = Rust;
    type To = Ruby;

    fn from_mrb(interp: &Mrb, value: Option<&[u8]>) -> Self {
        if let Some(value) = value {
            Self::from_mrb(interp, value)
        } else {
            Self::from_mrb(interp, None::<Self>)
        }
    }
}

#[allow(clippy::use_self)]
// https://github.com/rust-lang/rust-clippy/issues/4143
impl TryFromMrb<Value> for Option<Vec<u8>> {
    type From = Ruby;
    type To = Rust;

    unsafe fn try_from_mrb(
        interp: &Mrb,
        value: Value,
    ) -> Result<Self, Error<Self::From, Self::To>> {
        let value = <Option<Value>>::try_from_mrb(interp, value)?;
        if let Some(item) = value {
            Ok(Some(<Vec<u8>>::try_from_mrb(interp, item)?))
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
// FromMrb<Option<Vec<u8>>> is implemented in terms of FromMrb<Option<&[u8]>> so
// only implement the tests for Vec<u8> to exercise both code paths.
mod tests {
    use quickcheck_macros::quickcheck;

    use crate::convert::{FromMrb, TryFromMrb};
    use crate::eval::MrbEval;
    use crate::sys;
    use crate::value::types::Ruby;
    use crate::value::Value;

    #[test]
    fn fail_convert() {
        let interp = crate::interpreter().expect("mrb init");
        // get a mrb_value that can't be converted to a primitive type.
        let value = interp.eval("Object.new").expect("eval");
        let result = unsafe { <Option<Vec<u8>>>::try_from_mrb(&interp, value) }.map(|_| ());
        assert_eq!(result.map_err(|e| e.from), Err(Ruby::Object));
    }

    #[allow(clippy::needless_pass_by_value)]
    #[quickcheck]
    fn convert_to_value(v: Option<Vec<u8>>) -> bool {
        let interp = crate::interpreter().expect("mrb init");
        let value = Value::from_mrb(&interp, v.clone());
        if let Some(v) = v {
            let value = unsafe { <Vec<u8>>::try_from_mrb(&interp, value) }.expect("convert");
            value == v
        } else {
            unsafe { sys::mrb_sys_value_is_nil(value.inner()) }
        }
    }

    #[allow(clippy::needless_pass_by_value)]
    #[quickcheck]
    fn roundtrip(v: Option<Vec<u8>>) -> bool {
        let interp = crate::interpreter().expect("mrb init");
        let value = Value::from_mrb(&interp, v.clone());
        let value = unsafe { <Option<Vec<u8>>>::try_from_mrb(&interp, value) }.expect("convert");
        value == v
    }
}