willow_encoding/
bytes.rs

1use syncify::syncify;
2
3#[syncify(encoding_sync)]
4pub mod encoding {
5
6    use crate::error::DecodeError;
7
8    use either::Either;
9    use syncify::syncify_replace;
10
11    #[syncify_replace(use ufotofu::sync::{ BulkProducer};)]
12    use ufotofu::local_nb::BulkProducer;
13
14    /// Have `Producer` produce a single byte, or return an error if the final value was produced or the producer experienced an error.
15    pub async fn produce_byte<Producer>(
16        producer: &mut Producer,
17    ) -> Result<u8, DecodeError<Producer::Error>>
18    where
19        Producer: BulkProducer<Item = u8>,
20    {
21        match producer.produce().await {
22            Ok(Either::Left(item)) => Ok(item),
23            Ok(Either::Right(_)) => Err(DecodeError::InvalidInput),
24            Err(err) => Err(DecodeError::Producer(err)),
25        }
26    }
27}
28
29/// Return whether a bit at the given position is `1` or not.
30pub fn is_bitflagged(byte: u8, position: u8) -> bool {
31    let mask = match position {
32        0 => 0b1000_0000,
33        1 => 0b0100_0000,
34        2 => 0b0010_0000,
35        3 => 0b0001_0000,
36        4 => 0b0000_1000,
37        5 => 0b0000_0100,
38        6 => 0b0000_0010,
39        7 => 0b0000_0001,
40        _ => panic!("Can't check for a bitflag at a position greater than 7"),
41    };
42
43    byte & mask == mask
44}