Impl bool encoding

This commit is contained in:
mars 2022-05-25 17:47:59 -06:00
parent e97e31daf6
commit 63ba378bf7
1 changed files with 13 additions and 0 deletions

View File

@ -11,6 +11,19 @@ pub trait Decode: Sized {
fn decode(reader: &mut impl Read) -> IoResult<Self>;
}
// TODO: raw bool encoding is inefficient and should be replaced with bitsets.
impl Encode for bool {
fn encode(&self, writer: &mut impl Write) -> IoResult<()> {
writer.write_u8(if *self { 1 } else { 0 })
}
}
impl Decode for bool {
fn decode(reader: &mut impl Read) -> IoResult<Self> {
Ok(reader.read_u8()? != 0)
}
}
impl Encode for u8 {
fn encode(&self, writer: &mut impl Write) -> IoResult<()> {
writer.write_u8(*self)