-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ivan770/codec
Bincode framed codec
- Loading branch information
Showing
7 changed files
with
100 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::node::replication::message::Request; | ||
use actix_web::web::BytesMut; | ||
use bincode::{deserialize, serialize, Error}; | ||
use serde::Serialize; | ||
use tokio_util::codec::{Decoder, Encoder}; | ||
|
||
#[derive(Default)] | ||
pub struct BincodeCodec; | ||
|
||
impl<I> Encoder<I> for BincodeCodec | ||
where | ||
I: Serialize, | ||
{ | ||
type Error = Error; | ||
|
||
fn encode(&mut self, item: I, dst: &mut BytesMut) -> Result<(), Self::Error> { | ||
dst.extend_from_slice(&serialize(&item)?); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Decoder for BincodeCodec { | ||
// GAT's required to have generic impl | ||
type Item = Request<'static>; | ||
|
||
type Error = Error; | ||
|
||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||
if !src.is_empty() { | ||
Ok(Some(deserialize(&src.split_to(src.len()))?)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
#[cfg(test)] | ||
pub mod testing; | ||
|
||
pub mod codec; |