use core::marker::PhantomData;
use crate::{
munge, Decode, DecodeError, Encodable, EncodableOption, Encode, EncodeError, EncodeOption,
Slot, TakeFrom,
};
#[derive(Debug)]
pub struct ClientEndpoint;
#[derive(Debug)]
pub struct ServerEndpoint;
#[derive(Debug)]
#[repr(transparent)]
pub struct EndpointResource<T, P> {
resource: T,
_endpoint: PhantomData<P>,
}
impl<T, P> EndpointResource<T, P> {
pub fn new(resource: T) -> Self {
Self { resource, _endpoint: PhantomData }
}
pub fn into_inner(self) -> T {
self.resource
}
}
unsafe impl<D: ?Sized, T, P> Decode<D> for EndpointResource<T, P>
where
T: Decode<D>,
{
fn decode(slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
munge!(let Self { resource, _endpoint: _ } = slot);
T::decode(resource, decoder)
}
}
impl<T, P> Encodable for EndpointResource<T, P>
where
T: Encodable,
{
type Encoded<'buf> = EndpointResource<T::Encoded<'buf>, P>;
}
impl<T, P> EncodableOption for EndpointResource<T, P>
where
T: EncodableOption,
{
type EncodedOption<'buf> = EndpointResource<T::EncodedOption<'buf>, P>;
}
impl<E, T, P> Encode<E> for EndpointResource<T, P>
where
E: ?Sized,
T: Encode<E>,
{
fn encode(
&mut self,
encoder: &mut E,
slot: Slot<'_, Self::Encoded<'_>>,
) -> Result<(), EncodeError> {
munge!(let Self::Encoded { resource, _endpoint: _ } = slot);
self.resource.encode(encoder, resource)
}
}
impl<E, T, P> EncodeOption<E> for EndpointResource<T, P>
where
E: ?Sized,
T: EncodeOption<E>,
{
fn encode_option(
this: Option<&mut Self>,
encoder: &mut E,
slot: Slot<'_, Self::EncodedOption<'_>>,
) -> Result<(), EncodeError> {
munge!(let Self::EncodedOption { resource, _endpoint: _ } = slot);
T::encode_option(this.map(|this| &mut this.resource), encoder, resource)
}
}
impl<T, P, U> TakeFrom<EndpointResource<U, P>> for EndpointResource<T, P>
where
T: TakeFrom<U>,
{
fn take_from(from: &mut EndpointResource<U, P>) -> Self {
Self { resource: T::take_from(&mut from.resource), _endpoint: PhantomData }
}
}