1use crate::CodecError;
4
5pub trait EncodeSink {
7 fn write_all(&mut self, bytes: &[u8]) -> Result<(), CodecError>;
9}
10
11pub trait CanonicalEncode {
13 fn encode<W: EncodeSink + ?Sized>(&self, writer: &mut W) -> Result<(), CodecError>;
15}
16
17#[cfg(feature = "alloc")]
18impl EncodeSink for alloc::vec::Vec<u8> {
19 fn write_all(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
20 self.extend_from_slice(bytes);
21 Ok(())
22 }
23}
24
25#[cfg(feature = "std")]
26impl<T: std::io::Write> EncodeSink for std::io::BufWriter<T> {
27 fn write_all(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
28 std::io::Write::write_all(self, bytes).map_err(|_| CodecError::write_failed())
29 }
30}