1use crate::{FromStream, ToStream};
2
3use std::io::{Error, ErrorKind, Read, Result, Write};
4
5impl<S> ToStream<S> for bool {
6 fn to_stream<W: Write>(&self, stream: &mut W) -> Result<()> {
7 let bytes = [*self as u8];
8 stream.write_all(&bytes)?;
9 Ok(())
10 }
11}
12
13impl<S> FromStream<S> for bool {
14 fn from_stream<R: Read>(stream: &mut R) -> Result<Self> {
15 let mut bytes = [0; 1];
16
17 stream.read_exact(&mut bytes)?;
18
19 match bytes[0] {
20 0 => Ok(false),
21 1 => Ok(true),
22 _ => Err(Error::new(
23 ErrorKind::InvalidData,
24 "Invalid byte representation",
25 )),
26 }
27 }
28}