2
2
3
3
pub mod registry;
4
4
5
+ use std:: ops:: Deref ;
5
6
use bincode:: { Decode , Encode } ;
7
+ use bincode:: config:: Configuration ;
6
8
use bincode:: error:: { DecodeError , EncodeError } ;
7
9
8
10
use bytes:: { Bytes , BytesMut } ;
@@ -81,13 +83,13 @@ where
81
83
}
82
84
83
85
/// 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 >
85
87
where
86
- TData : Decode + Sized ,
88
+ TData : Decode ,
87
89
{
88
90
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 ) )
91
93
}
92
94
93
95
/// Encode a [TData] onto a buffer
@@ -100,9 +102,7 @@ where
100
102
Ok ( len)
101
103
}
102
104
103
- pub fn handle ( & self , obj : & TData )
104
- where
105
- TData : Sized ,
105
+ pub fn handle ( & self , obj : Box < & TData > )
106
106
{
107
107
for handler in & self . handlers {
108
108
handler. handle ( obj)
@@ -112,15 +112,15 @@ where
112
112
113
113
pub trait DataHandler < TData > : DowncastSync + Send + Sync
114
114
where
115
- TData : Data + ' static
115
+ TData : Data + ' static + ? Sized
116
116
{
117
- fn handle ( & self , obj : & TData ) ;
117
+ fn handle ( & self , obj : Box < & TData > ) ;
118
118
}
119
119
120
120
impl_downcast ! ( sync DataHandler <TData > where TData : Data + ' static ) ;
121
121
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 > ) {
124
124
self ( obj)
125
125
}
126
126
}
0 commit comments