use crate::State;
use crate::de::{OwnedSink, Sink, SinkHandle};
use crate::error::Error;
use crate::event::Atom;
pub(crate) struct MappedSink<'a, 'de, T, U> {
out: &'a mut Option<U>,
sink: OwnedSink<'de, T>,
convert: fn(T) -> Result<U, Error>,
}
impl<'a, 'de, T: Send + 'a, U: Send + 'a> MappedSink<'a, 'de, T, U> {
pub(crate) fn handle(
out: &'a mut Option<U>,
sink: OwnedSink<'de, T>,
convert: fn(T) -> Result<U, Error>,
state: &mut State,
) -> SinkHandle<'a, 'de> {
SinkHandle::arena(MappedSink { out, sink, convert }, state)
}
}
#[cfg(feature = "derive")]
pub fn mapped<'a, 'de, T: Send + 'a, U: Send + 'a>(
out: &'a mut Option<U>,
sink: OwnedSink<'de, T>,
convert: fn(T) -> Result<U, Error>,
state: &mut State,
) -> SinkHandle<'a, 'de> {
MappedSink::handle(out, sink, convert, state)
}
impl<'a, 'de, T: Send, U: Send> Sink<'de> for MappedSink<'a, 'de, T, U> {
fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().atom(atom, state)
}
fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().borrowed_atom(atom, state)
}
fn map(&mut self, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().map(state)
}
fn seq(&mut self, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().seq(state)
}
forward_to_owned!(sink);
fn finish(&mut self, state: &mut State) -> Result<(), Error> {
self.sink.get_mut().finish(state)?;
if let Some(value) = self.sink.take() {
*self.out = Some((self.convert)(value)?);
}
Ok(())
}
}