1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use super::{write::Writer, Encoder};
use crate::{config::Config, utils::Sealed};
/// An Encoder that writes bytes into a given writer `W`.
///
/// This struct should rarely be used.
/// In most cases, prefer any of the `encode` functions.
///
/// The ByteOrder that is chosen will impact the endianness that
/// is used to write integers to the writer.
///
/// ```
/// # use bincode::enc::{write::SliceWriter, EncoderImpl, Encode};
/// let slice: &mut [u8] = &mut [0, 0, 0, 0];
/// let config = bincode::config::legacy().with_big_endian();
///
/// let mut encoder = EncoderImpl::new(SliceWriter::new(slice), config);
/// // this u32 can be any Encodable
/// 5u32.encode(&mut encoder).unwrap();
/// assert_eq!(encoder.into_writer().bytes_written(), 4);
/// assert_eq!(slice, [0, 0, 0, 5]);
/// ```
pub struct EncoderImpl<W: Writer, C: Config> {
writer: W,
config: C,
}
impl<W: Writer, C: Config> EncoderImpl<W, C> {
/// Create a new Encoder
pub fn new(writer: W, config: C) -> EncoderImpl<W, C> {
EncoderImpl { writer, config }
}
/// Return the underlying writer
#[inline]
pub fn into_writer(self) -> W {
self.writer
}
}
impl<W: Writer, C: Config> Encoder for EncoderImpl<W, C> {
type W = W;
type C = C;
#[inline]
fn writer(&mut self) -> &mut Self::W {
&mut self.writer
}
#[inline]
fn config(&self) -> &Self::C {
&self.config
}
}
impl<W: Writer, C: Config> Sealed for EncoderImpl<W, C> {}