pub struct GenericCodec<T: Serialize + DeserializeOwned> { /* private fields */ }
Expand description

Codec for any struct that implements Serialize and DeserializeOwned traits.

Examples

use cs_utils::{random_number, random_str, random_bool, asyn::GenericCodec};
use futures::{StreamExt, SinkExt};
use serde::{Serialize, Deserialize};
use tokio::{io::duplex, try_join};
use tokio_util::codec::Framed;
 
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct TestStruct {
    id: u16,
    name: String,
    is_flag: bool,
}
 
println!("\n🏷️  Generic codec example:");
println!("\n⚠️ Note: the \"asyn\" feature must be enabled!\n");

let (stream, _) = duplex(4096);

let message = TestStruct {
    id: random_number(0..=u16::MAX),
    name: random_str(8),
    is_flag: random_bool(),
};
 
let framed_stream = Framed::new(
    stream,
    GenericCodec::<TestStruct>::new(),
 );
 
let (mut sink, mut source) = framed_stream.split();
 
// send/receive the framed data using the sink/source
// see "generic_codec" example for more details

Implementations

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The type of decoded frames.

The type of unrecoverable frame decoding errors. Read more

Attempts to decode a frame from the provided buffer of bytes. Read more

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more

Returns the “default value” for a type. Read more

The type of encoding errors. Read more

Encodes a frame into the buffer provided. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.