1use crate::Result;
4
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7
8#[cfg(feature = "sha2")]
9use sha2::{Digest, Sha256, Sha512};
10
11#[cfg(feature = "base64")]
13pub type Base64Writer<'o> = base64::Encoder<'o, base64::Base64>;
14
15pub trait Writer: Sized {
18 fn write(&mut self, bytes: &[u8]) -> Result<()>;
20}
21
22#[cfg(feature = "alloc")]
23impl Writer for Vec<u8> {
24 fn write(&mut self, bytes: &[u8]) -> Result<()> {
25 self.extend_from_slice(bytes);
26 Ok(())
27 }
28}
29
30#[cfg(feature = "base64")]
31impl Writer for Base64Writer<'_> {
32 fn write(&mut self, bytes: &[u8]) -> Result<()> {
33 Ok(self.encode(bytes)?)
34 }
35}
36
37#[cfg(feature = "pem")]
38impl Writer for pem::Encoder<'_, '_> {
39 fn write(&mut self, bytes: &[u8]) -> Result<()> {
40 Ok(self.encode(bytes)?)
41 }
42}
43
44#[cfg(feature = "sha2")]
45impl Writer for Sha256 {
46 fn write(&mut self, bytes: &[u8]) -> Result<()> {
47 self.update(bytes);
48 Ok(())
49 }
50}
51
52#[cfg(feature = "sha2")]
53impl Writer for Sha512 {
54 fn write(&mut self, bytes: &[u8]) -> Result<()> {
55 self.update(bytes);
56 Ok(())
57 }
58}