Skip to content

Commit 0e8fa6f

Browse files
committed
fix(data): box TData since we don't know its type when fetching with uuid
1 parent 4bee284 commit 0e8fa6f

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

crates/data/src/lib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
pub mod registry;
44

5+
use std::ops::Deref;
56
use bincode::{Decode, Encode};
7+
use bincode::config::Configuration;
68
use bincode::error::{DecodeError, EncodeError};
79

810
use bytes::{Bytes, BytesMut};
@@ -81,13 +83,13 @@ where
8183
}
8284

8385
/// Decode a [TData] off a buffer
84-
pub fn decode_from_bytes(&self, buf: &Bytes) -> Result<(TData, usize), DecodeError>
86+
pub fn decode_from_bytes(&self, buf: &Bytes) -> Result<(Box<TData>, usize), DecodeError>
8587
where
86-
TData : Decode + Sized,
88+
TData : Decode,
8789
{
8890
let config = bincode::config::standard();
89-
let res = bincode::decode_from_slice(buf, config)?;
90-
Ok(res)
91+
let res = bincode::decode_from_slice::<TData, Configuration>(buf, config)?;
92+
Ok((Box::new(res.0), res.1))
9193
}
9294

9395
/// Encode a [TData] onto a buffer
@@ -100,9 +102,7 @@ where
100102
Ok(len)
101103
}
102104

103-
pub fn handle(&self, obj: &TData)
104-
where
105-
TData : Sized,
105+
pub fn handle(&self, obj: Box<&TData>)
106106
{
107107
for handler in &self.handlers {
108108
handler.handle(obj)
@@ -112,15 +112,15 @@ where
112112

113113
pub trait DataHandler<TData> : DowncastSync + Send + Sync
114114
where
115-
TData : Data + 'static
115+
TData : Data + 'static + ?Sized
116116
{
117-
fn handle(&self, obj: &TData);
117+
fn handle(&self, obj: Box<&TData>);
118118
}
119119

120120
impl_downcast!(sync DataHandler<TData> where TData : Data + 'static);
121121

122-
impl<TData : Data, F: Fn(&TData) + Send + Sync + 'static> DataHandler<TData> for F {
123-
fn handle(&self, obj: &TData) {
122+
impl<TData : Data + ?Sized, F: Fn(Box<&TData>) + Send + Sync + 'static> DataHandler<TData> for F {
123+
fn handle(&self, obj: Box<&TData>) {
124124
self(obj)
125125
}
126126
}

crates/data/src/registry.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ impl DataTypeRegistry {
5151
}
5252

5353
/// Get the marshaller for [TData] based on a type [Uuid]
54-
pub fn by_uuid<TData>(&self, uuid: &Uuid) -> Option<&DataType<TData>>
54+
pub fn by_uuid(&self, uuid: &Uuid) -> Option<&DataType<dyn Data>>
5555
where
56-
TData : Data + 'static,
5756
{
5857
let type_id = self.id_map.get(uuid)?;
59-
unsafe { std::mem::transmute(self.types.get(type_id)) }
58+
self.types.get(type_id)
6059
}
6160

6261
// pub fn get_handler<TData, THandler>(&self, uuid: &Uuid) -> Option<&THandler>
@@ -90,7 +89,7 @@ mod tests {
9089
impl_data!(MyData, "9eddbf56-8cba-4962-9769-dcc84f1eefae");
9190

9291
#[test]
93-
fn test_registry() {
92+
fn test_registry_fetch_by_type() {
9493
let mut registry = DataTypeRegistry::new();
9594

9695
registry.register::<MyData>();
@@ -99,4 +98,14 @@ mod tests {
9998
assert!(got.is_some());
10099
assert_eq!(got.unwrap().get_id(), MyData::get_id_static());
101100
}
101+
102+
#[test]
103+
fn test_registry_fetch_by_uuid() {
104+
let mut registry = DataTypeRegistry::new();
105+
106+
registry.register::<MyData>();
107+
108+
let got = registry.by_uuid(&MyData::get_id_static());
109+
assert!(got.is_some());
110+
}
102111
}

0 commit comments

Comments
 (0)