Skip to main content

messagepack_core/decode/
bool.rs

1use super::{DecodeBorrowed, Error};
2use crate::{formats::Format, io::IoRead};
3
4impl<'de> DecodeBorrowed<'de> for bool {
5    type Value = Self;
6
7    fn decode_borrowed_with_format<R>(
8        format: Format,
9        _reader: &mut R,
10    ) -> core::result::Result<Self::Value, Error<R::Error>>
11    where
12        R: IoRead<'de>,
13    {
14        let val = match format {
15            Format::True => Ok(true),
16            Format::False => Ok(false),
17            _ => Err(Error::UnexpectedFormat),
18        }?;
19
20        Ok(val)
21    }
22}
23
24impl<'de> DecodeBorrowed<'de> for core::sync::atomic::AtomicBool {
25    type Value = Self;
26
27    fn decode_borrowed_with_format<R>(
28        format: Format,
29        reader: &mut R,
30    ) -> core::result::Result<Self::Value, Error<R::Error>>
31    where
32        R: IoRead<'de>,
33    {
34        let val = bool::decode_borrowed_with_format(format, reader)?;
35        Ok(Self::new(val))
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::decode::Decode;
42
43    #[test]
44    fn decode_true() {
45        let buf: &[u8] = &[0xc3];
46        let mut r = crate::io::SliceReader::new(buf);
47        let decoded = bool::decode(&mut r).unwrap();
48        let expect = true;
49        assert_eq!(decoded, expect);
50        assert_eq!(r.rest().len(), 0)
51    }
52
53    #[test]
54    fn decode_false() {
55        let buf: &[u8] = &[0xc2];
56        let mut r = crate::io::SliceReader::new(buf);
57        let decoded = bool::decode(&mut r).unwrap();
58        let expect: bool = false;
59        assert_eq!(decoded, expect);
60        assert_eq!(r.rest().len(), 0)
61    }
62}