use std::marker::PhantomData; use prost::Message; use crate::error::Error; use super::{Buf, ConnectCodec, ConnectDecoder, ConnectEncoder, EncodeBuf, MessageCodec}; pub struct ProtoConnectCodec { _phantom: PhantomData U>, } impl ProtoConnectCodec { pub fn new() -> Self { Self { _phantom: PhantomData, } } } impl Default for ProtoConnectCodec { fn default() -> Self { Self::new() } } impl ConnectCodec for ProtoConnectCodec { type Encode = T; type Decode = U; type Encoder = ProtoConnectEncoder; type Decoder = ProtoConnectDecoder; fn message_codec(&self) -> MessageCodec { MessageCodec::Proto } fn encoder(&mut self) -> Self::Encoder { ProtoConnectEncoder { _phantom: PhantomData, } } fn decoder(&mut self) -> Self::Decoder { ProtoConnectDecoder { _phantom: PhantomData, } } } pub struct ProtoConnectEncoder { _phantom: PhantomData, } impl ConnectEncoder for ProtoConnectEncoder { fn encode(&mut self, message: T, buf: &mut EncodeBuf) -> Result<(), Error> { message.encode(buf.as_buf_mut())?; Ok(()) } fn size_hint(&mut self, message: &T) -> usize { message.encoded_len() } } pub struct ProtoConnectDecoder { _phantom: PhantomData T>, } impl ConnectDecoder for ProtoConnectDecoder { fn decode(&mut self, buf: impl Buf) -> Result { Ok(T::decode(buf)?) } }