use std::marker::PhantomData;
use tonic::{
codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder},
Status,
};
#[derive(Debug)]
pub struct AnyEncoder<T>(PhantomData<T>);
impl<T> Encoder for AnyEncoder<T> {
type Error = Status;
type Item = T;
fn encode(
&mut self,
_item: Self::Item,
_buf: &mut EncodeBuf<'_>,
) -> Result<(), Self::Error> {
Err(Status::unavailable("invalid codec"))
}
}
#[derive(Debug)]
pub struct AnyDecoder<U>(PhantomData<U>);
impl<U> Decoder for AnyDecoder<U> {
type Error = Status;
type Item = U;
fn decode(
&mut self,
_buf: &mut DecodeBuf<'_>,
) -> Result<Option<Self::Item>, Self::Error> {
Err(Status::unavailable("invalid codec"))
}
}
#[derive(Debug, Clone)]
pub struct AnyCodec<T, U>(PhantomData<(T, U)>);
impl<T, U> Default for AnyCodec<T, U> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<T, U> Codec for AnyCodec<T, U>
where
T: Send + 'static,
U: Send + 'static,
{
type Encode = T;
type Decode = U;
type Encoder = AnyEncoder<T>;
type Decoder = AnyDecoder<U>;
fn encoder(&mut self) -> Self::Encoder {
AnyEncoder(PhantomData)
}
fn decoder(&mut self) -> Self::Decoder {
AnyDecoder(PhantomData)
}
}