minicbor-adapters 0.0.8

Adapters between `minicbor` and other crates such as `heapless` and `cboritem`
Documentation
/// Wrapper around an [`embedded_io::Write`] implementation that can be written to by [minicbor].
///
/// # Usage example
///
/// ```
/// use minicbor_adapters::WriteToEmbeddedIo;
///
/// fn serialize(data: &[u8], into: impl embedded_io::Write) {
///     minicbor::encode(data, WriteToEmbeddedIo(into)).unwrap();
/// }
/// ```
#[derive(Debug)]
pub struct WriteToEmbeddedIo<W: embedded_io::Write>(pub W);

impl<W: embedded_io::Write> minicbor::encode::Write for WriteToEmbeddedIo<W> {
    type Error = W::Error;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.0.write_all(buf)
    }
}

// This'd make a bad doctest, because most lines are really about using the only easily provided
// impl of the trait.
#[test]
fn test_write() {
    const BUF_LEN: usize = 10;
    let mut buf = [0xff; BUF_LEN];
    let mut remaining_buf = &mut buf[..];
    minicbor::encode([1, 2, 3], WriteToEmbeddedIo(&mut remaining_buf)).unwrap();
    let written = BUF_LEN - remaining_buf.len();
    assert_eq!(&[0x83, 1, 2, 3], &buf[..written]);
}