33use cxx;
44
55use crate :: bridge:: ffi;
6+ use crate :: DataSketchesError ;
67
78/// Specifies the target type of HLL sketch to be created. It is a target in that the actual
89/// allocation of the HLL array is deferred until sufficient number of items have been received by
@@ -61,13 +62,10 @@ impl HLLSketch {
6162 UPtrVec ( self . inner . serialize ( ) )
6263 }
6364
64- pub fn deserialize ( buf : & [ u8 ] ) -> Self {
65- // TODO: this could be friendlier, it currently terminates
66- // the program no bad deserialization, and instead can be a
67- // Result.
68- Self {
69- inner : ffi:: deserialize_opaque_hll_sketch ( buf) ,
70- }
65+ pub fn deserialize ( buf : & [ u8 ] ) -> Result < Self , DataSketchesError > {
66+ Ok ( Self {
67+ inner : ffi:: deserialize_opaque_hll_sketch ( buf) ?,
68+ } )
7169 }
7270}
7371
@@ -107,9 +105,9 @@ mod tests {
107105 fn check_cycle ( s : & HLLSketch ) {
108106 let est = s. estimate ( ) ;
109107 let bytes = s. serialize ( ) ;
110- let cpy = HLLSketch :: deserialize ( bytes. as_ref ( ) ) ;
111- let cpy2 = HLLSketch :: deserialize ( bytes. as_ref ( ) ) ;
112- let cpy3 = HLLSketch :: deserialize ( bytes. as_ref ( ) ) ;
108+ let cpy = HLLSketch :: deserialize ( bytes. as_ref ( ) ) . unwrap ( ) ;
109+ let cpy2 = HLLSketch :: deserialize ( bytes. as_ref ( ) ) . unwrap ( ) ;
110+ let cpy3 = HLLSketch :: deserialize ( bytes. as_ref ( ) ) . unwrap ( ) ;
113111 assert_eq ! ( est, cpy. estimate( ) ) ;
114112 assert_eq ! ( est, cpy2. estimate( ) ) ;
115113 assert_eq ! ( est, cpy3. estimate( ) ) ;
@@ -222,7 +220,7 @@ mod tests {
222220 base64:: STANDARD_NO_PAD ,
223221 )
224222 . unwrap ( ) ;
225- let hh = HLLSketch :: deserialize ( & bytes) ;
223+ let hh = HLLSketch :: deserialize ( & bytes) . unwrap ( ) ;
226224
227225 assert_eq ! ( hh. estimate( ) , 4.000000029802323 ) ;
228226 }
@@ -234,16 +232,24 @@ mod tests {
234232 base64:: STANDARD_NO_PAD ,
235233 )
236234 . unwrap ( ) ;
237- let hh1 = HLLSketch :: deserialize ( & bytes) ;
235+ let hh1 = HLLSketch :: deserialize ( & bytes) . unwrap ( ) ;
238236
239237 let bytes = base64:: decode_config (
240238 "AgEHDAMABAgGc2UEe2XmCNsXmgrDsDgEAAAAAAAAAAAAAAAAAAAAAA==" ,
241239 base64:: STANDARD_NO_PAD ,
242240 )
243241 . unwrap ( ) ;
244- let hh2 = HLLSketch :: deserialize ( & bytes) ;
242+ let hh2 = HLLSketch :: deserialize ( & bytes) . unwrap ( ) ;
245243
246244 assert_eq ! ( hh1. estimate( ) , 4.000000029802323 ) ;
247245 assert_eq ! ( hh2. estimate( ) , 4.000000029802323 ) ;
248246 }
247+
248+ #[ test]
249+ fn hll_deserialization_error ( ) {
250+ assert ! ( matches!(
251+ HLLSketch :: deserialize( & [ 9 , 9 , 9 , 9 ] ) ,
252+ Err ( DataSketchesError :: CXXError ( _) )
253+ ) ) ;
254+ }
249255}
0 commit comments