deser-core 0.9.0

Core traits and types of deser, use the deser crate instead
Documentation
/// Creates a newtype wrapper around `Option<T>`.
///
/// Slot wrappers are useful to implement deserialization when stateless
/// deserialization is an option.  This way an allocation is avoided.
/// For more information see [`de`](crate::de).  To see the generated
/// slot wrapper API see [`SlotWrapper`](crate::de::SlotWrapper).
///
/// ## Example
///
/// ```rust
/// deser::make_slot_wrapper!(SlotWrapper);
/// ```
#[macro_export]
macro_rules! make_slot_wrapper {
    ($name:ident) => {
        $crate::__make_slot_wrapper!((pub(crate)), $name);
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! __make_slot_wrapper {
    (($($vis:tt)*), $name:ident) => {
        /// The generated slot wrapper.
        ///
        /// Note that you need to generate your own slot wrapper by using the
        /// [`make_slot_wrapper`] macro so you're able to implement a sink
        /// for it.
        #[repr(transparent)]
        $($vis)* struct $name<T>(Option<T>);

        impl<T> $name<T> {
            /// Wraps a slot transparently.
            ///
            /// This wraps a slot (an `Option<T>`) in a slot wrapper.  Typically
            /// the [`make_handle`](Self::make_handle) shortcut is preferred.
            $($vis)* fn wrap(out: &mut Option<T>) -> &mut Self {
                unsafe { &mut *(out as *mut Option<T> as *mut $name<T>) }
            }

            /// Wraps a slot transparently and returns a handle.
            ///
            /// This wraps a slot (an `Option<T>`) in a slot wrapper and then
            /// returns a [`SinkHandle`] to it.
            ///
            /// Equivalent to `SinkHandle::to(SlotWrapper::wrap(...))`.
            $($vis)* fn make_handle<'de>(out: &mut Option<T>) -> $crate::de::SinkHandle<'_, 'de> where $name<T>: $crate::de::Sink<'de> {
                $crate::de::SinkHandle::to(Self::wrap(out))
            }
        }

        impl<T> core::ops::Deref for $name<T> {
            type Target = Option<T>;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl<T> core::ops::DerefMut for $name<T> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }
    };
}

/// Implements `__private_begin` for types which do not implement `finish`.
///
/// The derive generates the same code.
macro_rules! begin_without_finish {
    () => {
        #[inline]
        fn __private_begin(
            &self,
            state: &mut crate::State,
        ) -> Result<crate::ser::Begin<'_>, crate::Error> {
            let shape = crate::ser::Serialize::container_shape(self);
            Ok(crate::ser::Begin::chunk(
                crate::ser::Serialize::serialize(self, state)?,
                shape,
                false,
            ))
        }
    };
}