@@ -7,21 +7,23 @@ use std::error::Error;
77/// Merges multiple sorted inputs into a single sorted output.
88/// Time complexity is *m* \* log(*n*) in worst case where *m* is the number of items,
99/// *n* is the number of chunks (inputs).
10- pub struct BinaryHeapMerger < T , C >
10+ pub struct BinaryHeapMerger < T , E , C >
1111where
1212 T : Ord ,
13- C : IntoIterator < Item = Result < T , Box < dyn Error > > > ,
13+ E : Error ,
14+ C : IntoIterator < Item = Result < T , E > > ,
1415{
1516 // binary heap is max-heap by default so we reverse it to convert it to min-heap
1617 items : BinaryHeap < ( std:: cmp:: Reverse < T > , usize ) > ,
1718 chunks : Vec < C :: IntoIter > ,
1819 initiated : bool ,
1920}
2021
21- impl < T , C > BinaryHeapMerger < T , C >
22+ impl < T , E , C > BinaryHeapMerger < T , E , C >
2223where
2324 T : Ord ,
24- C : IntoIterator < Item = Result < T , Box < dyn Error > > > ,
25+ E : Error ,
26+ C : IntoIterator < Item = Result < T , E > > ,
2527{
2628 /// Creates an instance of binary heap merger using chunks as inputs.
2729 /// Chunk items should be sorted in ascending order otherwise the result is undefined.
@@ -40,12 +42,13 @@ where
4042 }
4143}
4244
43- impl < T , C > Iterator for BinaryHeapMerger < T , C >
45+ impl < T , E , C > Iterator for BinaryHeapMerger < T , E , C >
4446where
4547 T : Ord ,
46- C : IntoIterator < Item = Result < T , Box < dyn Error > > > ,
48+ E : Error ,
49+ C : IntoIterator < Item = Result < T , E > > ,
4750{
48- type Item = Result < T , Box < dyn Error > > ;
51+ type Item = Result < T , E > ;
4952
5053 /// Returns the next item from inputs in ascending order.
5154 fn next ( & mut self ) -> Option < Self :: Item > {
@@ -104,26 +107,26 @@ mod test {
104107 ) ]
105108 #[ case(
106109 vec![
107- vec![ Result :: < i32 , Box <dyn Error >> :: Err ( Box :: new ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ) ]
110+ vec![ Result :: Err ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ]
108111 ] ,
109112 vec![
110- Result :: < i32 , Box <dyn Error >> :: Err ( Box :: new ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) )
113+ Result :: Err ( io:: Error :: new( ErrorKind :: Other , "test error" ) )
111114 ] ,
112115 ) ]
113116 #[ case(
114117 vec![
115- vec![ Ok ( 3 ) , Result :: < i32 , Box <dyn Error >> :: Err ( Box :: new ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ) ] ,
118+ vec![ Ok ( 3 ) , Result :: Err ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ] ,
116119 vec![ Ok ( 1 ) , Ok ( 2 ) ] ,
117120 ] ,
118121 vec![
119122 Ok ( 1 ) ,
120123 Ok ( 2 ) ,
121- Result :: < i32 , Box <dyn Error >> :: Err ( Box :: new ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ) ,
124+ Result :: Err ( io:: Error :: new( ErrorKind :: Other , "test error" ) ) ,
122125 ] ,
123126 ) ]
124127 fn test_merger (
125- #[ case] chunks : Vec < Vec < Result < i32 , Box < dyn Error > > > > ,
126- #[ case] expected_result : Vec < Result < i32 , Box < dyn Error > > > ,
128+ #[ case] chunks : Vec < Vec < Result < i32 , io :: Error > > > ,
129+ #[ case] expected_result : Vec < Result < i32 , io :: Error > > ,
127130 ) {
128131 let merger = BinaryHeapMerger :: new ( chunks) ;
129132 let actual_result = merger. collect ( ) ;
@@ -136,16 +139,16 @@ mod test {
136139 }
137140
138141 fn compare_vectors_of_result < T : PartialEq , E : Error + ' static > (
139- actual : & Vec < Result < T , Box < dyn Error > > > ,
140- expected : & Vec < Result < T , Box < dyn Error > > > ,
142+ actual : & Vec < Result < T , E > > ,
143+ expected : & Vec < Result < T , E > > ,
141144 ) -> bool {
142145 actual
143146 . into_iter ( )
144147 . zip ( expected)
145148 . all (
146149 |( actual_result, expected_result) | match ( actual_result, expected_result) {
147150 ( Ok ( actual_result) , Ok ( expected_result) ) if actual_result == expected_result => true ,
148- ( Err ( actual_err) , Err ( expected_err) ) if actual_err. is :: < E > ( ) && expected_err. is :: < E > ( ) => true ,
151+ ( Err ( actual_err) , Err ( expected_err) ) => actual_err. to_string ( ) == expected_err. to_string ( ) ,
149152 _ => false ,
150153 } ,
151154 )
0 commit comments