pub struct GenericCodec<T: Serialize + DeserializeOwned> { /* private fields */ }
Expand description
Codec to pass over any struct that implements Serialize
and DeserializeOwned
traits.
§Examples
use cs_utils::{random_number, random_str, random_bool, futures::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 \"async\" 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§
Source§impl<T: Serialize + DeserializeOwned> GenericCodec<T>
impl<T: Serialize + DeserializeOwned> GenericCodec<T>
Trait Implementations§
Source§impl<T: Clone + Serialize + DeserializeOwned> Clone for GenericCodec<T>
impl<T: Clone + Serialize + DeserializeOwned> Clone for GenericCodec<T>
Source§fn clone(&self) -> GenericCodec<T>
fn clone(&self) -> GenericCodec<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<T: Debug + Serialize + DeserializeOwned> Debug for GenericCodec<T>
impl<T: Debug + Serialize + DeserializeOwned> Debug for GenericCodec<T>
Source§impl<T: Serialize + DeserializeOwned> Decoder for GenericCodec<T>
impl<T: Serialize + DeserializeOwned> Decoder for GenericCodec<T>
Source§fn decode(
&mut self,
buf: &mut BytesMut,
) -> Result<Option<Self::Item>, Self::Error>
fn decode( &mut self, buf: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>
Attempts to decode a frame from the provided buffer of bytes. Read more
Source§impl<T: Serialize + DeserializeOwned> Default for GenericCodec<T>
impl<T: Serialize + DeserializeOwned> Default for GenericCodec<T>
Auto Trait Implementations§
impl<T> Freeze for GenericCodec<T>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more