ssh_encoding/
writer.rs

1//! Writer trait and associated implementations.
2
3use crate::Result;
4
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7
8#[cfg(feature = "bytes")]
9use bytes::{BufMut, BytesMut};
10
11#[cfg(feature = "digest")]
12use digest::Digest;
13
14/// Writer trait which encodes the SSH binary format to various output
15/// encodings.
16pub trait Writer: Sized {
17    /// Write the given bytes to the writer.
18    fn write(&mut self, bytes: &[u8]) -> Result<()>;
19}
20
21#[cfg(feature = "alloc")]
22impl Writer for Vec<u8> {
23    fn write(&mut self, bytes: &[u8]) -> Result<()> {
24        self.extend_from_slice(bytes);
25        Ok(())
26    }
27}
28
29#[cfg(feature = "bytes")]
30impl Writer for BytesMut {
31    fn write(&mut self, bytes: &[u8]) -> Result<()> {
32        self.put(bytes);
33        Ok(())
34    }
35}
36
37/// Wrapper for digests.
38///
39/// This allows to update digests from the serializer directly.
40#[cfg(feature = "digest")]
41#[derive(Debug)]
42pub struct DigestWriter<'d, D>(pub &'d mut D);
43
44#[cfg(feature = "digest")]
45impl<D> Writer for DigestWriter<'_, D>
46where
47    D: Digest,
48{
49    fn write(&mut self, bytes: &[u8]) -> Result<()> {
50        self.0.update(bytes);
51        Ok(())
52    }
53}