Skip to content

Commit 9317aca

Browse files
add serde ByteSerializer
1 parent 0797b72 commit 9317aca

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ edition = "2018"
88
protocol = { git = "https://github.com/noocene/protocol" }
99
serde = { version = "1.0.104", default-features = false }
1010
futures = { version = "0.3.1", default-features = false }
11+
genio = { version = "0.2.1", default-features = false }
12+
core-futures-io = { git = "https://github.com/noocene/core-futures-io", default-features = false }
1113

1214
[features]
13-
alloc = ["serde/alloc"]
14-
std = []
15-
default = ["std", "alloc"]
15+
alloc = ["serde/alloc", "protocol/alloc", "core-futures-io/alloc"]
16+
std = ["protocol/std", "genio/std", "core-futures-io/std"]
17+
default = ["std", "alloc"]

src/lib.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ use core::{
1414
},
1515
time::Duration,
1616
};
17+
use core_futures_io::{AsyncRead, AsyncWrite};
1718
use futures::{
1819
future::{ready, Map, Ready},
1920
stream::{once, Forward, Once, StreamFuture},
2021
FutureExt, Sink, StreamExt,
2122
};
22-
use protocol::{Bottom, Channels, Format, Protocol};
23+
use protocol::{
24+
format::{ByteFormat, ItemFormat},
25+
Bottom, Channels, Format, Protocol,
26+
};
2327
use serde::{de::DeserializeOwned, Serialize};
2428

2529
#[macro_export]
@@ -49,9 +53,27 @@ pub trait Serializer {
4953
) -> Result<T, Self::DeserializeError>;
5054
}
5155

56+
pub trait ByteSerializer: Serializer {
57+
type SerializeError;
58+
type DeserializeError;
59+
60+
fn serialize<T: Serialize + DeserializeOwned, W: AsyncWrite>(
61+
&mut self,
62+
writer: &mut W,
63+
item: T,
64+
) -> Result<(), <Self as ByteSerializer>::SerializeError>;
65+
66+
fn deserialize<T: Serialize + DeserializeOwned, R: AsyncRead>(
67+
&mut self,
68+
reader: &mut R,
69+
) -> Result<T, <Self as ByteSerializer>::DeserializeError>;
70+
}
71+
5272
pub struct Serde<T: Serializer>(T);
5373

54-
impl<T: Serializer, U: Serialize + DeserializeOwned> Format<U> for Serde<T> {
74+
impl<T: Serializer, U: Serialize + DeserializeOwned> Format<U> for Serde<T> {}
75+
76+
impl<T: Serializer, U: Serialize + DeserializeOwned> ItemFormat<U> for Serde<T> {
5577
type Representation = T::Representation;
5678
type SerializeError = T::SerializeError;
5779
type Serialize = Ready<Result<T::Representation, T::SerializeError>>;
@@ -67,6 +89,21 @@ impl<T: Serializer, U: Serialize + DeserializeOwned> Format<U> for Serde<T> {
6789
}
6890
}
6991

92+
impl<T: ByteSerializer, U: Serialize + DeserializeOwned> ByteFormat<U> for Serde<T> {
93+
type SerializeError = <T as ByteSerializer>::SerializeError;
94+
type Serialize = Ready<Result<(), <T as ByteSerializer>::SerializeError>>;
95+
type DeserializeError = <T as ByteSerializer>::DeserializeError;
96+
type Deserialize = Ready<Result<U, <T as ByteSerializer>::DeserializeError>>;
97+
98+
fn serialize<W: AsyncWrite>(&mut self, writer: &mut W, item: U) -> Self::Serialize {
99+
ready(ByteSerializer::serialize(&mut self.0, writer, item))
100+
}
101+
102+
fn deserialize<R: AsyncRead>(&mut self, reader: &mut R) -> Self::Deserialize {
103+
ready(ByteSerializer::deserialize(&mut self.0, reader))
104+
}
105+
}
106+
70107
// impl<T: Serializer> Format<Bottom> for Serde<T> {
71108
// type Representation = T::Representation;
72109
// type SerializeError = T::SerializeError;

0 commit comments

Comments
 (0)