1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use crate::{check_len, Result, TryRead, TryWrite};

impl<'a> TryRead<'a> for bool {
    #[inline]
    fn try_read(bytes: &'a [u8], _ctx: ()) -> Result<(Self, usize)> {
        check_len(bytes, 1)?;

        Ok((bytes[0] != 0, 1))
    }
}

impl TryWrite for bool {
    #[inline]
    fn try_write(self, bytes: &mut [u8], _ctx: ()) -> Result<usize> {
        check_len(bytes, 1)?;

        bytes[0] = if self { u8::max_value() } else { 0 };

        Ok(1)
    }
}