serde_scale/
write.rs

1// Copyright (C) 2020 Stephane Raux. Distributed under the zlib license.
2
3#[cfg(feature = "alloc")]
4use alloc::vec::Vec;
5use core::fmt::{Debug, Display};
6
7/// Interface to write bytes
8pub trait Write {
9    type Error: Debug + Display;
10
11    /// Writes bytes
12    fn write(&mut self, data: &[u8]) -> Result<(), Self::Error>;
13}
14
15impl<W: Write + ?Sized> Write for &'_ mut W {
16    type Error = W::Error;
17
18    fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
19        (**self).write(data)
20    }
21}
22
23#[cfg(feature = "alloc")]
24impl Write for Vec<u8> {
25    type Error = core::convert::Infallible;
26
27    fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
28        self.extend(data);
29        Ok(())
30    }
31}