diff options
Diffstat (limited to 'src/endable.rs')
-rw-r--r-- | src/endable.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/endable.rs b/src/endable.rs new file mode 100644 index 0000000..6006080 --- /dev/null +++ b/src/endable.rs @@ -0,0 +1,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) + } + } +} |