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
use super::{FromForeign, InputType, ReturnType, ToForeign};
use std::convert::Infallible;

pub struct BoolMarshaler;

impl InputType for BoolMarshaler {
    type Foreign = u8;
    type ForeignTraitObject = ();
}

impl ReturnType for BoolMarshaler {
    type Foreign = u8;
    type ForeignTraitObject = ();

    fn foreign_default() -> u8 {
        0
    }
}

impl FromForeign<u8, bool> for BoolMarshaler {
    type Error = Infallible;

    #[inline(always)]
    unsafe fn from_foreign(i: u8) -> Result<bool, Self::Error> {
        Ok(i != 0)
    }
}

impl ToForeign<bool, u8> for BoolMarshaler {
    type Error = std::convert::Infallible;

    #[inline(always)]
    fn to_foreign(b: bool) -> Result<u8, Self::Error> {
        Ok(if b { 1 } else { 0 })
    }
}