-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Postcard Codec gated by feature codec-postcard.
- Loading branch information
Showing
7 changed files
with
66 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,7 @@ jobs: | |
- bincode | ||
- ciborium | ||
- message-pack | ||
- postcard | ||
|
||
steps: | ||
- name: Checkout sources | ||
|
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,36 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{Codec, DeserializationError, SerializationError}; | ||
|
||
/// Postcard codec. | ||
/// | ||
/// See [postcard] for details. | ||
/// This uses the default function configuration. | ||
#[cfg_attr(docsrs, doc(cfg(feature = "codec-postcard")))] | ||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct Postcard; | ||
|
||
impl Codec for Postcard { | ||
#[inline] | ||
fn serialize<Writer, Item>(writer: Writer, item: &Item) -> Result<(), super::SerializationError> | ||
where | ||
Writer: std::io::Write, | ||
Item: serde::Serialize, | ||
{ | ||
postcard::to_io(item, writer).map_err(SerializationError::new)?; | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn deserialize<Reader, Item>(mut reader: Reader) -> Result<Item, super::DeserializationError> | ||
where | ||
Reader: std::io::Read, | ||
Item: serde::de::DeserializeOwned, | ||
{ | ||
let mut bytes = vec![]; | ||
reader.read_to_end(&mut bytes).map_err(DeserializationError::new)?; | ||
|
||
let item = postcard::from_bytes(&bytes).map_err(DeserializationError::new)?; | ||
Ok(item) | ||
} | ||
} |
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