aboutsummaryrefslogtreecommitdiffstats
path: root/src/endable.rs
blob: 6d842f36250a02a9e744febf8a7eab7c3f60afb0 (plain) (blame)
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
use crate::Error;

#[derive(Debug)]
pub struct Endable<T> {
    inner: T,
    ended: bool,
}

impl<T> Endable<T> {
    pub fn new(inner: T) -> Self {
        Self {
            inner,
            ended: false,
        }
    }

    pub fn end(&mut self) {
        self.ended = true;
    }

    pub fn into_inner(self) -> T {
        self.inner
    }

    #[inline(always)]
    pub fn try_as_mut(&mut self) -> Result<&mut T, Error> {
        if self.ended {
            Err(Error::RootElementEnded)
        } else {
            Ok(&mut self.inner)
        }
    }

    pub fn ignore_end(&mut self) -> &mut T {
        &mut self.inner
    }
}