Skip to main content

slice_codec/
encoder.rs

1// Copyright (c) ZeroC, Inc.
2
3use crate::buffer::OutputTarget;
4use crate::encode_into::EncodeInto;
5use crate::Result;
6use core::ops::{Deref, DerefMut};
7
8/// TODO
9pub struct Encoder<O: OutputTarget> {
10    /// The underlying output-target that this encoder will write bytes to.
11    output: O,
12}
13
14impl<O: OutputTarget> Encoder<O> {
15    /// TODO
16    pub fn new(underlying: O) -> Self {
17        Self { output: underlying }
18    }
19
20    /// Attempts to encode the provided value into this encoder's underlying output-target.
21    pub fn encode<T: EncodeInto>(&mut self, value: T) -> Result<()> {
22        value.encode_into(self)
23    }
24}
25
26// Allows users to call functions on the underlying output-target through this encoder.
27impl<O: OutputTarget> Deref for Encoder<O> {
28    type Target = O;
29
30    fn deref(&self) -> &Self::Target {
31        &self.output
32    }
33}
34
35// Allows users to call functions on the underlying output-target through this encoder.
36impl<O: OutputTarget> DerefMut for Encoder<O> {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.output
39    }
40}