Struct cs_utils::asyn::GenericCodec
source · [−]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 detailsImplementations
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for GenericCodec<T> where
T: RefUnwindSafe,
impl<T> Send for GenericCodec<T> where
T: Send,
impl<T> Sync for GenericCodec<T> where
T: Sync,
impl<T> Unpin for GenericCodec<T> where
T: Unpin,
impl<T> UnwindSafe for GenericCodec<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more