use core::borrow::Borrow;
use core::fmt;
use core::marker::PhantomData;
use crate::alloc::ToOwned;
use crate::expecting::{self, Expecting};
use crate::{Allocator, Context};
#[allow(unused_variables)]
pub trait UnsizedVisitor<'de, C, T>: Sized
where
C: Context<Error = Self::Error, Allocator = Self::Allocator>,
T: ?Sized + ToOwned,
{
type Ok;
type Error;
type Allocator: Allocator;
#[doc(hidden)]
type __UseMusliUnsizedVisitorAttributeMacro;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
#[inline]
fn visit_owned(self, cx: C, value: T::Owned<Self::Allocator>) -> Result<Self::Ok, Self::Error> {
self.visit_ref(cx, value.borrow())
}
#[inline]
fn visit_borrowed(self, cx: C, value: &'de T) -> Result<Self::Ok, Self::Error> {
self.visit_ref(cx, value)
}
#[inline]
fn visit_ref(self, cx: C, value: &T) -> Result<Self::Ok, Self::Error> {
Err(cx.message(expecting::bad_visitor_type(
&expecting::AnyValue,
ExpectingWrapper::new(&self),
)))
}
}
#[repr(transparent)]
struct ExpectingWrapper<'a, T, C, I>
where
I: ?Sized,
{
inner: T,
_marker: PhantomData<(C, &'a I)>,
}
impl<T, C, U> ExpectingWrapper<'_, T, C, U>
where
U: ?Sized,
{
#[inline]
fn new(value: &T) -> &Self {
unsafe { &*(value as *const T as *const Self) }
}
}
impl<'de, T, C, U> Expecting for ExpectingWrapper<'_, T, C, U>
where
T: UnsizedVisitor<'de, C, U, Error = C::Error, Allocator = C::Allocator>,
C: Context,
U: ?Sized + ToOwned,
{
#[inline]
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.expecting(f)
}
}