-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for msgpack deserialization
- Loading branch information
1 parent
d13ba63
commit 052fdef
Showing
24 changed files
with
1,036 additions
and
169 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 +1,12 @@ | ||
[workspace] | ||
resolver = "2" | ||
members = ["merde", "merde_json", "merde_time", "merde_core", "merde_yaml"] | ||
exclude = ["zerodeps-example"] | ||
members = [ | ||
"merde", | ||
"merde_json", | ||
"merde_time", | ||
"merde_core", | ||
"merde_yaml", | ||
"merde_msgpack", | ||
"merde_loggingserializer", | ||
] | ||
exclude = ["zerodeps-example", "merde_msgpack/testdata-maker"] |
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,154 @@ | ||
use std::{ | ||
borrow::Cow, | ||
fmt, | ||
hash::{Hash, Hasher}, | ||
ops::Deref, | ||
}; | ||
|
||
#[cfg(feature = "compact_bytes")] | ||
use compact_bytes::CompactBytes; | ||
|
||
use crate::IntoStatic; | ||
|
||
/// A copy-on-write bytes type that uses [`CompactBytes`] for | ||
/// the "owned" variant. | ||
#[derive(Clone)] | ||
pub enum CowBytes<'a> { | ||
Borrowed(&'a [u8]), | ||
#[cfg(feature = "compact_bytes")] | ||
Owned(CompactBytes), | ||
#[cfg(not(feature = "compact_bytes"))] | ||
Owned(Vec<u8>), | ||
} | ||
|
||
impl<'a> CowBytes<'a> { | ||
pub fn new(bytes: &'a [u8]) -> Self { | ||
CowBytes::Borrowed(bytes) | ||
} | ||
|
||
pub fn into_owned(self) -> Vec<u8> { | ||
match self { | ||
CowBytes::Borrowed(b) => b.to_vec(), | ||
#[cfg(feature = "compact_bytes")] | ||
CowBytes::Owned(b) => b.to_vec(), | ||
#[cfg(not(feature = "compact_bytes"))] | ||
CowBytes::Owned(b) => b, | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for CowBytes<'_> { | ||
fn as_ref(&self) -> &[u8] { | ||
match self { | ||
CowBytes::Borrowed(b) => b, | ||
CowBytes::Owned(b) => b.as_ref(), | ||
} | ||
} | ||
} | ||
|
||
impl Deref for CowBytes<'_> { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl<'a> From<&'a [u8]> for CowBytes<'a> { | ||
fn from(b: &'a [u8]) -> Self { | ||
CowBytes::Borrowed(b) | ||
} | ||
} | ||
|
||
impl<'a> From<Vec<u8>> for CowBytes<'a> { | ||
fn from(v: Vec<u8>) -> Self { | ||
#[cfg(feature = "compact_bytes")] | ||
{ | ||
CowBytes::Owned(CompactBytes::new(v)) | ||
} | ||
#[cfg(not(feature = "compact_bytes"))] | ||
{ | ||
CowBytes::Owned(v) | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<Cow<'a, [u8]>> for CowBytes<'a> { | ||
fn from(cow: Cow<'a, [u8]>) -> Self { | ||
match cow { | ||
Cow::Borrowed(b) => CowBytes::Borrowed(b), | ||
Cow::Owned(v) => v.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'b> PartialEq<CowBytes<'a>> for CowBytes<'b> { | ||
fn eq(&self, other: &CowBytes<'a>) -> bool { | ||
self.deref() == other.deref() | ||
} | ||
} | ||
|
||
impl PartialEq<[u8]> for CowBytes<'_> { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.deref() == other | ||
} | ||
} | ||
|
||
impl PartialEq<CowBytes<'_>> for [u8] { | ||
fn eq(&self, other: &CowBytes<'_>) -> bool { | ||
self == other.deref() | ||
} | ||
} | ||
|
||
impl Eq for CowBytes<'_> {} | ||
|
||
impl Hash for CowBytes<'_> { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.deref().hash(state) | ||
} | ||
} | ||
|
||
impl fmt::Debug for CowBytes<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(self.deref(), f) | ||
} | ||
} | ||
|
||
impl IntoStatic for CowBytes<'_> { | ||
type Output = CowBytes<'static>; | ||
|
||
fn into_static(self) -> Self::Output { | ||
match self { | ||
#[cfg(feature = "compact_bytes")] | ||
CowBytes::Borrowed(b) => CowBytes::Owned(CompactBytes::new(b)), | ||
#[cfg(not(feature = "compact_bytes"))] | ||
CowBytes::Borrowed(b) => CowBytes::Owned(b.to_vec()), | ||
CowBytes::Owned(b) => CowBytes::Owned(b), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
mod serde_impls { | ||
use super::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
impl Serialize for CowBytes<'_> { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_bytes(self) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for CowBytes<'_> { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let bytes = Vec::<u8>::deserialize(deserializer)?; | ||
Ok(CowBytes::from(bytes)) | ||
} | ||
} | ||
} |
Oops, something went wrong.