aboutsummaryrefslogtreecommitdiffstats
path: root/src/endable.rs
blob: 60060809ec518bd09ca51cefed082c251c2b06e3 (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
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)
        }
    }
}