use alloc::borrow::Cow;
use alloc::vec::Vec;
use crate::State;
use crate::adapters::{DeserializeAs, SerializeAs};
use crate::de::{SinkHandle, atom_into_handle, borrowed_atom_into_handle, update};
use crate::error::Error;
use crate::event::{Atom, ContainerShape};
use crate::ser::{Begin, Chunk, Describe, PlainSink};
pub struct Derived;
#[doc(hidden)]
pub trait DerivedDeserialize<'de>: Sized + Send {
fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de>;
fn initial_value() -> Option<Self> {
None
}
fn deserialize_update<'out>(value: &'out mut Self, state: &mut State) -> SinkHandle<'out, 'de> {
update::replace_handle_with(
value,
<Self as DerivedDeserialize<'de>>::deserialize_into,
state,
)
}
fn __private_atom_into(
out: &mut Option<Self>,
atom: Atom,
state: &mut State,
) -> Result<(), Error> {
atom_into_handle(
<Self as DerivedDeserialize<'de>>::deserialize_into(out, state),
atom,
state,
)
}
fn __private_borrowed_atom_into(
out: &mut Option<Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error> {
borrowed_atom_into_handle(
<Self as DerivedDeserialize<'de>>::deserialize_into(out, state),
atom,
state,
)
}
fn __private_is_bytes() -> bool {
false
}
fn __private_vec_from_bytes(bytes: Vec<u8>) -> Option<Vec<Self>> {
let _ = bytes;
None
}
fn __private_array_from_bytes<const N: usize>(bytes: &[u8]) -> Option<[Self; N]> {
let _ = bytes;
None
}
}
#[doc(hidden)]
pub trait DerivedSerialize: Sync {
fn serialize(&self, state: &mut State) -> Result<Chunk<'_>, Error>;
fn finish(&self, _state: &mut State) -> Result<(), Error> {
Ok(())
}
fn is_optional(&self) -> bool {
false
}
fn describe(&self, d: &mut dyn Describe) {
let _ = d;
}
fn container_shape(&self) -> ContainerShape {
ContainerShape::new()
}
fn __private_begin(&self, state: &mut State) -> Result<Begin<'_>, Error> {
let shape = DerivedSerialize::container_shape(self);
Ok(Begin::chunk(
DerivedSerialize::serialize(self, state)?,
shape,
true,
))
}
fn __private_is_plain() -> bool
where
Self: Sized,
{
false
}
fn __private_is_plain_value(&self) -> bool
where
Self: Sized,
{
<Self as DerivedSerialize>::__private_is_plain()
}
fn __private_emit_plain(&self, sink: &mut dyn PlainSink) -> Result<(), Error> {
let _ = sink;
unreachable!("not a plain value")
}
fn __private_slice_as_bytes(_val: &[Self]) -> Option<Cow<'_, [u8]>>
where
Self: Sized,
{
None
}
}
impl<'de, T: DerivedDeserialize<'de>> DeserializeAs<'de, T> for Derived {
#[inline]
fn deserialize_into_as<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
T::deserialize_into(out, state)
}
#[inline]
fn initial_value_as() -> Option<T> {
T::initial_value()
}
#[inline]
fn deserialize_update_as<'out>(value: &'out mut T, state: &mut State) -> SinkHandle<'out, 'de>
where
T: Send,
{
T::deserialize_update(value, state)
}
#[inline]
fn __private_atom_into_as(
out: &mut Option<T>,
atom: Atom,
state: &mut State,
) -> Result<(), Error> {
T::__private_atom_into(out, atom, state)
}
#[inline]
fn __private_borrowed_atom_into_as(
out: &mut Option<T>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error> {
T::__private_borrowed_atom_into(out, atom, state)
}
#[inline]
fn __private_is_bytes_as() -> bool {
T::__private_is_bytes()
}
#[inline]
fn __private_vec_from_bytes_as(bytes: Vec<u8>) -> Option<Vec<T>> {
T::__private_vec_from_bytes(bytes)
}
#[inline]
fn __private_array_from_bytes_as<const N: usize>(bytes: &[u8]) -> Option<[T; N]> {
T::__private_array_from_bytes(bytes)
}
}
impl<T: DerivedSerialize + ?Sized> SerializeAs<T> for Derived {
#[inline]
fn serialize_as<'a>(value: &'a T, state: &mut State) -> Result<Chunk<'a>, Error> {
value.serialize(state)
}
#[inline]
fn finish_as(value: &T, state: &mut State) -> Result<(), Error> {
value.finish(state)
}
#[inline]
fn is_optional_as(value: &T) -> bool {
value.is_optional()
}
#[inline]
fn container_shape_as(value: &T) -> ContainerShape {
value.container_shape()
}
fn describe_as(value: &T, d: &mut dyn Describe) {
value.describe(d)
}
#[inline]
fn __private_begin_as<'a>(value: &'a T, state: &mut State) -> Result<Begin<'a>, Error> {
value.__private_begin(state)
}
#[inline]
fn __private_slice_as_bytes_as(val: &[T]) -> Option<Cow<'_, [u8]>>
where
T: Sized,
{
T::__private_slice_as_bytes(val)
}
}