pub trait Codec:
Send
+ Sync
+ 'static
+ Debug {
// Provided methods
fn encode_notify(
&self,
method: &str,
params: &dyn Serialize,
write: &mut BytesMut,
) -> Result<(), EncodeError> { ... }
fn encode_request(
&self,
method: &str,
req_id_hint: NonZeroU64,
params: &dyn Serialize,
write: &mut BytesMut,
) -> Result<NonZeroU64, EncodeError> { ... }
fn encode_response(
&self,
req_id: ReqIdRef<'_>,
encode_as_error: bool,
response: &dyn Serialize,
write: &mut BytesMut,
) -> Result<(), EncodeError> { ... }
fn encode_response_predefined(
&self,
req_id: ReqIdRef<'_>,
response: &PredefinedResponseError,
write: &mut BytesMut,
) -> Result<(), EncodeError> { ... }
fn decode_inbound(
&self,
data: &[u8],
) -> Result<(InboundFrameType, Range<usize>), DecodeError> { ... }
fn decode_payload<'a>(
&self,
payload: &'a [u8],
decode: &mut dyn FnMut(&mut dyn Deserializer<'a>) -> Result<(), Error>,
) -> Result<(), DecodeError> { ... }
fn try_decode_predef_error<'a>(
&self,
payload: &'a [u8],
) -> Option<PredefinedResponseError> { ... }
}
Expand description
Parses/Encodes data frame.
This is a trait that encodes/decodes data frame into underlying RPC protocol, and generally responsible for any protocol-specific data frame handling.
The codec, should trivially be clone-able.
Provided Methods§
Sourcefn encode_notify(
&self,
method: &str,
params: &dyn Serialize,
write: &mut BytesMut,
) -> Result<(), EncodeError>
fn encode_notify( &self, method: &str, params: &dyn Serialize, write: &mut BytesMut, ) -> Result<(), EncodeError>
Encodes notify frame
Sourcefn encode_request(
&self,
method: &str,
req_id_hint: NonZeroU64,
params: &dyn Serialize,
write: &mut BytesMut,
) -> Result<NonZeroU64, EncodeError>
fn encode_request( &self, method: &str, req_id_hint: NonZeroU64, params: &dyn Serialize, write: &mut BytesMut, ) -> Result<NonZeroU64, EncodeError>
Encodes request frame
It should internally generate appropriate request ID, and provide deterministic hash of the
internally generated request ID. This generated ID will be fed to Codec::decode_inbound
to match the response to the request.
It is best to the output hash be deterministic for input req_id_hint
, but it is not
required.
req_id_hint
is guaranteed to be odd number.
§Returns
Should return for deterministic hash of the (internally generated) request ID.
This is used to match the response to the request.
Sourcefn encode_response(
&self,
req_id: ReqIdRef<'_>,
encode_as_error: bool,
response: &dyn Serialize,
write: &mut BytesMut,
) -> Result<(), EncodeError>
fn encode_response( &self, req_id: ReqIdRef<'_>, encode_as_error: bool, response: &dyn Serialize, write: &mut BytesMut, ) -> Result<(), EncodeError>
Encodes response frame
req_id
: The original request ID.encode_as_error
: If true, the response should be encoded as error response.response
: The response object.write
: The writer to write the response to.
Sourcefn encode_response_predefined(
&self,
req_id: ReqIdRef<'_>,
response: &PredefinedResponseError,
write: &mut BytesMut,
) -> Result<(), EncodeError>
fn encode_response_predefined( &self, req_id: ReqIdRef<'_>, response: &PredefinedResponseError, write: &mut BytesMut, ) -> Result<(), EncodeError>
Encodes predefined response error. See PredefinedResponseError
.
Sourcefn decode_inbound(
&self,
data: &[u8],
) -> Result<(InboundFrameType, Range<usize>), DecodeError>
fn decode_inbound( &self, data: &[u8], ) -> Result<(InboundFrameType, Range<usize>), DecodeError>
Decodes inbound frame, and identifies the frame type.
If Response
is received, the deterministic hash should be calculated from the request ID.
§Returns
Returns the frame type, and the range of the frame.
Sourcefn decode_payload<'a>(
&self,
payload: &'a [u8],
decode: &mut dyn FnMut(&mut dyn Deserializer<'a>) -> Result<(), Error>,
) -> Result<(), DecodeError>
fn decode_payload<'a>( &self, payload: &'a [u8], decode: &mut dyn FnMut(&mut dyn Deserializer<'a>) -> Result<(), Error>, ) -> Result<(), DecodeError>
Decodes the payload of the inbound frame.
Codec implementation should call decode
with created Deserializer
object.
Its type information can be erased using <dyn erased_serde::Deserializer>::erase
Sourcefn try_decode_predef_error<'a>(
&self,
payload: &'a [u8],
) -> Option<PredefinedResponseError>
fn try_decode_predef_error<'a>( &self, payload: &'a [u8], ) -> Option<PredefinedResponseError>
Tries decode the payload to PredefinedResponseError
.