dangerous/reader/
peek.rs

1use core::marker::PhantomData;
2use core::ops::Deref;
3
4use crate::input::Input;
5
6/// Peek of [`Input`].
7///
8/// Below is an example of what this structure prevents:
9///
10/// ```compile_fail
11/// use dangerous::{BytesReader, Error};
12///
13/// fn parse<'i, E>(r: &mut BytesReader<'i, E>) -> Result<&'i [u8], E>
14/// where
15///    E: Error<'i>
16/// {
17///     let peeked = r.peek(2)?;
18///     Ok(peeked.as_dangerous())
19/// }
20/// ```
21pub struct Peek<'p, I> {
22    input: I,
23    lifetime: PhantomData<&'p ()>,
24}
25
26impl<'p, I> Peek<'p, I> {
27    #[inline(always)]
28    pub(super) fn new(input: I) -> Self {
29        Self {
30            input,
31            lifetime: PhantomData,
32        }
33    }
34}
35
36impl<'p, I> AsRef<I> for Peek<'p, I>
37where
38    I: Input<'p>,
39{
40    #[inline(always)]
41    fn as_ref(&self) -> &I {
42        &self.input
43    }
44}
45
46impl<'p, I> Deref for Peek<'p, I>
47where
48    I: Input<'p>,
49{
50    type Target = I;
51
52    #[inline(always)]
53    fn deref(&self) -> &Self::Target {
54        self.as_ref()
55    }
56}