Skip to content

Commit a1689ae

Browse files
committed
feat(socketioxide): wip: unify non-binary and binary data
This allows users to create a payload struct (that implements Serialize/Deserialize) that has any binary payload data embedded in the struct directly, rather than needing to look in a "side" Vec of payload data, and have to guess what order the binary payloads fit into their data model. To accomplish this, there are new Serializer and Deserializer implementations that mostly wrap the Ser/Deser impls provided for serde_json::Value. Unfortunately serde_json doesn't expose everything necessary to do this in a truly simple way; some duplication of serde_json's functionality is needed. NB: due to difficulties with lifetimes, the deserializer has to currently clone the Vec of binary payloads as they get passed around to sub-deserializers. While this isn't the worst thing (cloning Bytes is cheap), it's not free, and this needs to be revisited and fixed. Closes #276.
1 parent 1702bdb commit a1689ae

File tree

6 files changed

+1567
-2
lines changed

6 files changed

+1567
-2
lines changed

socketioxide/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ tracing = { workspace = true, optional = true }
3636

3737
# State
3838
state = { version = "0.6.0", optional = true }
39+
pretty_assertions = "1.4.0"
3940

4041
[features]
4142
v4 = ["engineioxide/v3"]

socketioxide/src/handler/extract.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ where
140140
fn from_message_parts(
141141
_: &Arc<Socket<A>>,
142142
v: &mut serde_json::Value,
143-
_: &mut Vec<Bytes>,
143+
b: &mut Vec<Bytes>,
144144
_: &Option<i64>,
145145
) -> Result<Self, Self::Error> {
146146
upwrap_array(v);
147-
serde_json::from_value(v.clone()).map(Data)
147+
crate::value::de::from_value::<T>(v.clone(), b).map(Data)
148148
}
149149
}
150150

socketioxide/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,13 @@ pub use engineioxide::TransportType;
275275
pub use errors::{AckError, AdapterError, BroadcastError, DisconnectError, SendError, SocketError};
276276
pub use handler::extract;
277277
pub use io::{SocketIo, SocketIoBuilder, SocketIoConfig};
278+
pub use value::{de::from_value, ser::to_value};
278279

279280
mod client;
280281
mod errors;
281282
mod io;
282283
mod ns;
284+
mod value;
283285

284286
/// Socket.IO protocol version.
285287
/// It is accessible with the [`Socket::protocol`](socket::Socket) method or as an extractor

0 commit comments

Comments
 (0)