Skip to main content

slice_codec/
decoder.rs

1// Copyright (c) ZeroC, Inc.
2
3use crate::buffer::InputSource;
4use crate::decode_from::DecodeFrom;
5use crate::Result;
6use core::ops::{Deref, DerefMut};
7
8/// TODO
9pub struct Decoder<I: InputSource> {
10    /// The underlying input-source that this decoder will read bytes from.
11    input: I,
12}
13
14impl<I: InputSource> Decoder<I> {
15    /// TODO
16    pub fn new(underlying: I) -> Self {
17        Self { input: underlying }
18    }
19
20    /// Attempts to decode a value of the specified type from this decoder's underlying input-source.
21    pub fn decode<T: DecodeFrom>(&mut self) -> Result<T> {
22        T::decode_from(self)
23    }
24}
25
26// Allows users to call functions on the underlying input-source through this decoder.
27impl<I: InputSource> Deref for Decoder<I> {
28    type Target = I;
29
30    fn deref(&self) -> &Self::Target {
31        &self.input
32    }
33}
34
35// Allows users to call functions on the underlying input-source through this decoder.
36impl<I: InputSource> DerefMut for Decoder<I> {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.input
39    }
40}