Skip to main content

Serializer

Trait Serializer 

Source
pub trait Serializer<T: ?Sized>: Send + Sync {
    // Required method
    fn serialize<'a>(
        &'a self,
        topic: &'a str,
        data: Option<&'a T>,
    ) -> impl Future<Output = Result<Option<Cow<'a, [u8]>>, Error>> + Send + 'a;

    // Provided methods
    fn serialize_with_headers<'a, 'h>(
        &'a self,
        topic: &'a str,
        headers: &'h mut RecordHeaders,
        data: Option<&'a T>,
    ) -> impl Future<Output = Result<Option<Cow<'a, [u8]>>, Error>> + Send + use<'a, 'h, Self, T>
       where 'a: 'h { ... }
    fn configure<'a>(
        &'a mut self,
        configs: &'a HashMap<String, String>,
        is_key: bool,
    ) -> impl Future<Output = Result<(), Error>> + Send + 'a { ... }
    fn close(&self) -> impl Future<Output = Result<(), Error>> + Send + '_ { ... }
}
Expand description

An interface for converting objects to bytes.

This interface can be combined with ClusterResourceListener to receive cluster metadata once it’s available, as well as Monitorable to enable the serializer to register metrics. For the latter, the following tags are automatically added to all metrics registered: config set to either key.serializer or value.serializer, and class set to the serializer class name.

§Type Parameters

  • T - Type to be serialized from. It may be unsized (Serializer<str>).

§Translation notes

WARNING: combining with ClusterResourceListener or Monitorable has no effect: no client registers either.

Methods return a Send future, which the producer stores inline: a serializer awaiting a large operation only on a cache miss should Box::pin it alone.

Required Methods§

Source

fn serialize<'a>( &'a self, topic: &'a str, data: Option<&'a T>, ) -> impl Future<Output = Result<Option<Cow<'a, [u8]>>, Error>> + Send + 'a

Convert data into a byte array.

It is recommended to serialize None data to None byte array.

§Arguments
  • topic - topic associated with data
  • data - typed data; may be None
§Returns

Serialized bytes, which may borrow from data; may be None.

§Errors

Whatever error the serializer reports; the producer returns it from send, or fails the record’s future.

Provided Methods§

Source

fn serialize_with_headers<'a, 'h>( &'a self, topic: &'a str, headers: &'h mut RecordHeaders, data: Option<&'a T>, ) -> impl Future<Output = Result<Option<Cow<'a, [u8]>>, Error>> + Send + use<'a, 'h, Self, T>
where 'a: 'h,

Convert data into a byte array, with access to the record headers.

It is recommended to serialize None data to None byte array.

Note that the passed in headers may be empty. The implementation is allowed to modify them, as a side effect of serialization. It is considered best practice to not delete or modify existing headers, but rather only add new ones.

§Arguments
  • topic - topic associated with data
  • headers - headers associated with the record
  • data - typed data; may be None
§Returns

Serialized bytes, which may borrow from data; may be None.

§Errors

As serialize.

§Translation notes

The default implementation ignores the headers and delegates to serialize. The producer makes the headers read-only once both serializers ran.

Source

fn configure<'a>( &'a mut self, configs: &'a HashMap<String, String>, is_key: bool, ) -> impl Future<Output = Result<(), Error>> + Send + 'a

Configure this class. The default implementation is a no-op.

§Arguments
  • configs - configs in key/value pairs
  • is_key - whether the serializer is used for the key or the value
§Errors

Whatever error the serializer reports.

Source

fn close(&self) -> impl Future<Output = Result<(), Error>> + Send + '_

Close this serializer.

This method must be idempotent as it may be called multiple times.

§Errors

Whatever error the serializer reports; the producer still closes the other serializer, then returns it as the source of its own.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§