@@ -13,12 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
- use alloc:: boxed:: Box ;
17
- use alloc:: format;
18
- use alloc:: string:: ToString ;
16
+ use anyhow:: { bail, Result } ;
19
17
use alloc:: vec:: Vec ;
20
18
use core:: any:: type_name;
21
- use core:: error:: Error ;
22
19
use core:: slice:: from_raw_parts_mut;
23
20
24
21
pub struct InputDataSection {
@@ -31,16 +28,14 @@ impl InputDataSection {
31
28
InputDataSection { ptr, len }
32
29
}
33
30
34
- pub fn try_pop_shared_input_data_into < T > ( & self ) -> Result < T , Box < dyn Error > >
31
+ pub fn try_pop_shared_input_data_into < T > ( & self ) -> Result < T >
35
32
where
36
33
T : for < ' a > TryFrom < & ' a [ u8 ] > ,
37
34
{
38
35
let input_data_buffer = unsafe { from_raw_parts_mut ( self . ptr , self . len ) } ;
39
36
40
37
if input_data_buffer. is_empty ( ) {
41
- return Err ( "Got a 0-size buffer in pop_shared_input_data_into"
42
- . to_string ( )
43
- . into ( ) ) ;
38
+ bail ! ( "Got a 0-size buffer in pop_shared_input_data_into" ) ;
44
39
}
45
40
46
41
// get relative offset to next free address
@@ -51,11 +46,7 @@ impl InputDataSection {
51
46
) ;
52
47
53
48
if stack_ptr_rel > self . len || stack_ptr_rel < 16 {
54
- return Err ( format ! (
55
- "Invalid stack pointer: {} in pop_shared_input_data_into" ,
56
- stack_ptr_rel
57
- )
58
- . into ( ) ) ;
49
+ bail ! ( "Invalid stack pointer: {} in pop_shared_input_data_into" , stack_ptr_rel) ;
59
50
}
60
51
61
52
// go back 8 bytes and read. This is the offset to the element on top of stack
@@ -71,7 +62,7 @@ impl InputDataSection {
71
62
let type_t = match T :: try_from ( buffer) {
72
63
Ok ( t) => Ok ( t) ,
73
64
Err ( _e) => {
74
- return Err ( format ! ( "Unable to convert buffer to {}" , type_name:: <T >( ) ) . into ( ) ) ;
65
+ bail ! ( "Unable to convert buffer to {}" , type_name:: <T >( ) ) ;
75
66
}
76
67
} ;
77
68
@@ -95,13 +86,11 @@ impl OutputDataSection {
95
86
OutputDataSection { ptr, len }
96
87
}
97
88
98
- pub fn push_shared_output_data ( & self , data : Vec < u8 > ) -> Result < ( ) , Box < dyn Error > > {
89
+ pub fn push_shared_output_data ( & self , data : Vec < u8 > ) -> Result < ( ) > {
99
90
let output_data_buffer = unsafe { from_raw_parts_mut ( self . ptr , self . len ) } ;
100
91
101
92
if output_data_buffer. is_empty ( ) {
102
- return Err ( "Got a 0-size buffer in push_shared_output_data"
103
- . to_string ( )
104
- . into ( ) ) ;
93
+ bail ! ( "Got a 0-size buffer in push_shared_output_data" ) ;
105
94
}
106
95
107
96
// get offset to next free address on the stack
@@ -119,22 +108,14 @@ impl OutputDataSection {
119
108
// It can be equal to the size, but never greater
120
109
// It can never be less than 8. An empty buffer's stack pointer is 8
121
110
if stack_ptr_rel > self . len || stack_ptr_rel < 8 {
122
- return Err ( format ! (
123
- "Invalid stack pointer: {} in push_shared_output_data" ,
124
- stack_ptr_rel
125
- )
126
- . into ( ) ) ;
111
+ bail ! ( "Invalid stack pointer: {} in push_shared_output_data" , stack_ptr_rel) ;
127
112
}
128
113
129
114
// check if there is enough space in the buffer
130
115
let size_required = data. len ( ) + 8 ; // the data plus the pointer pointing to the data
131
116
let size_available = self . len - stack_ptr_rel;
132
117
if size_required > size_available {
133
- return Err ( format ! (
134
- "Not enough space in shared output buffer. Required: {}, Available: {}" ,
135
- size_required, size_available
136
- )
137
- . into ( ) ) ;
118
+ bail ! ( "Not enough space in shared output buffer. Required: {}, Available: {}" , size_required, size_available) ;
138
119
}
139
120
140
121
// write the actual data
0 commit comments