Struct cs_utils::futures::GenericCodec
source · [−]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 detailsImplementations
sourceimpl<T: Serialize + DeserializeOwned> GenericCodec<T>
impl<T: Serialize + DeserializeOwned> GenericCodec<T>
Trait Implementations
sourceimpl<T: Clone + Serialize + DeserializeOwned> Clone for GenericCodec<T>
impl<T: Clone + Serialize + DeserializeOwned> Clone for GenericCodec<T>
sourcefn clone(&self) -> GenericCodec<T>
fn clone(&self) -> GenericCodec<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
sourceimpl<T: Debug + Serialize + DeserializeOwned> Debug for GenericCodec<T>
impl<T: Debug + Serialize + DeserializeOwned> Debug for GenericCodec<T>
sourceimpl<T: Serialize + DeserializeOwned> Decoder for GenericCodec<T>
impl<T: Serialize + DeserializeOwned> Decoder for GenericCodec<T>
type Item = T
type Item = T
The type of decoded frames.
sourcefn 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
sourceimpl<T: Serialize + DeserializeOwned> Default for GenericCodec<T>
impl<T: Serialize + DeserializeOwned> Default for GenericCodec<T>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more