use std::ops::{Deref, DerefMut};
use crate::{Error, frame, group};
pub(crate) struct Group(Option<group::Producer>);
impl Group {
pub fn new(producer: group::Producer) -> Self {
Self(Some(producer))
}
pub fn into_inner(mut self) -> group::Producer {
self.0.take().expect("group owned until consumed")
}
pub fn finish(mut self) -> Result<(), Error> {
self.deref_mut().finish()?;
self.0.take();
Ok(())
}
pub fn abort(self, err: Error) -> Result<(), Error> {
self.into_inner().abort(err)
}
}
impl Deref for Group {
type Target = group::Producer;
fn deref(&self) -> &Self::Target {
self.0.as_ref().expect("group owned until consumed")
}
}
impl DerefMut for Group {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.as_mut().expect("group owned until consumed")
}
}
impl Drop for Group {
fn drop(&mut self) {
if let Some(producer) = self.0.take() {
let _ = producer.abort(Error::Cancel);
}
}
}
pub(crate) struct Frame<'a>(Option<frame::Producer<'a>>);
impl<'a> Frame<'a> {
pub fn new(producer: frame::Producer<'a>) -> Self {
Self(Some(producer))
}
pub fn finish(mut self) -> Result<(), Error> {
self.0.take().expect("frame owned until consumed").finish()
}
pub fn abort(mut self, err: Error) -> Result<(), Error> {
self.0.take().expect("frame owned until consumed").abort(err)
}
}
impl<'a> Deref for Frame<'a> {
type Target = frame::Producer<'a>;
fn deref(&self) -> &Self::Target {
self.0.as_ref().expect("frame owned until consumed")
}
}
impl DerefMut for Frame<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.as_mut().expect("frame owned until consumed")
}
}
impl Drop for Frame<'_> {
fn drop(&mut self) {
if let Some(producer) = self.0.take() {
let _ = producer.abort(Error::Cancel);
}
}
}