@@ -48,7 +48,7 @@ ffi_fn! {
48
48
ffi_fn ! {
49
49
/// Free an HTTP request if not going to send it on a client.
50
50
fn hyper_request_free( req: * mut hyper_request) {
51
- drop( unsafe { Box :: from_raw( req) } ) ;
51
+ drop( non_null! ( Box :: from_raw( req) ?= ( ) ) ) ;
52
52
}
53
53
}
54
54
@@ -58,9 +58,10 @@ ffi_fn! {
58
58
let bytes = unsafe {
59
59
std:: slice:: from_raw_parts( method, method_len as usize )
60
60
} ;
61
+ let req = non_null!( & mut * req ?= hyper_code:: HYPERE_INVALID_ARG ) ;
61
62
match Method :: from_bytes( bytes) {
62
63
Ok ( m) => {
63
- * unsafe { & mut * req } . 0 . method_mut( ) = m;
64
+ * req. 0 . method_mut( ) = m;
64
65
hyper_code:: HYPERE_OK
65
66
} ,
66
67
Err ( _) => {
@@ -76,9 +77,10 @@ ffi_fn! {
76
77
let bytes = unsafe {
77
78
std:: slice:: from_raw_parts( uri, uri_len as usize )
78
79
} ;
80
+ let req = non_null!( & mut * req ?= hyper_code:: HYPERE_INVALID_ARG ) ;
79
81
match Uri :: from_maybe_shared( bytes) {
80
82
Ok ( u) => {
81
- * unsafe { & mut * req } . 0 . uri_mut( ) = u;
83
+ * req. 0 . uri_mut( ) = u;
82
84
hyper_code:: HYPERE_OK
83
85
} ,
84
86
Err ( _) => {
@@ -98,7 +100,8 @@ ffi_fn! {
98
100
fn hyper_request_set_version( req: * mut hyper_request, version: c_int) -> hyper_code {
99
101
use http:: Version ;
100
102
101
- * unsafe { & mut * req } . 0 . version_mut( ) = match version {
103
+ let req = non_null!( & mut * req ?= hyper_code:: HYPERE_INVALID_ARG ) ;
104
+ * req. 0 . version_mut( ) = match version {
102
105
super :: HYPER_HTTP_VERSION_NONE => Version :: HTTP_11 ,
103
106
super :: HYPER_HTTP_VERSION_1_0 => Version :: HTTP_10 ,
104
107
super :: HYPER_HTTP_VERSION_1_1 => Version :: HTTP_11 ,
@@ -130,8 +133,9 @@ ffi_fn! {
130
133
/// This takes ownership of the `hyper_body *`, you must not use it or
131
134
/// free it after setting it on the request.
132
135
fn hyper_request_set_body( req: * mut hyper_request, body: * mut hyper_body) -> hyper_code {
133
- let body = unsafe { Box :: from_raw( body) } ;
134
- * unsafe { & mut * req } . 0 . body_mut( ) = body. 0 ;
136
+ let body = non_null!( Box :: from_raw( body) ?= hyper_code:: HYPERE_INVALID_ARG ) ;
137
+ let req = non_null!( & mut * req ?= hyper_code:: HYPERE_INVALID_ARG ) ;
138
+ * req. 0 . body_mut( ) = body. 0 ;
135
139
hyper_code:: HYPERE_OK
136
140
}
137
141
}
@@ -157,7 +161,8 @@ ffi_fn! {
157
161
func: callback,
158
162
data: UserDataPointer ( data) ,
159
163
} ;
160
- unsafe { & mut * req } . 0 . extensions_mut( ) . insert( ext) ;
164
+ let req = non_null!( & mut * req ?= hyper_code:: HYPERE_INVALID_ARG ) ;
165
+ req. 0 . extensions_mut( ) . insert( ext) ;
161
166
hyper_code:: HYPERE_OK
162
167
}
163
168
}
@@ -176,7 +181,7 @@ impl hyper_request {
176
181
ffi_fn ! {
177
182
/// Free an HTTP response after using it.
178
183
fn hyper_response_free( resp: * mut hyper_response) {
179
- drop( unsafe { Box :: from_raw( resp) } ) ;
184
+ drop( non_null! ( Box :: from_raw( resp) ?= ( ) ) ) ;
180
185
}
181
186
}
182
187
@@ -185,7 +190,7 @@ ffi_fn! {
185
190
///
186
191
/// It will always be within the range of 100-599.
187
192
fn hyper_response_status( resp: * const hyper_response) -> u16 {
188
- unsafe { & * resp } . 0 . status( ) . as_u16( )
193
+ non_null! ( & * resp ?= 0 ) . 0 . status( ) . as_u16( )
189
194
}
190
195
}
191
196
@@ -200,7 +205,7 @@ ffi_fn! {
200
205
/// Use `hyper_response_reason_phrase_len()` to get the length of this
201
206
/// buffer.
202
207
fn hyper_response_reason_phrase( resp: * const hyper_response) -> * const u8 {
203
- unsafe { & * resp } . reason_phrase( ) . as_ptr( )
208
+ non_null! ( & * resp ?= std :: ptr :: null ( ) ) . reason_phrase( ) . as_ptr( )
204
209
} ?= std:: ptr:: null( )
205
210
}
206
211
@@ -209,7 +214,7 @@ ffi_fn! {
209
214
///
210
215
/// Use `hyper_response_reason_phrase()` to get the buffer pointer.
211
216
fn hyper_response_reason_phrase_len( resp: * const hyper_response) -> size_t {
212
- unsafe { & * resp } . reason_phrase( ) . len( )
217
+ non_null! ( & * resp ?= 0 ) . reason_phrase( ) . len( )
213
218
}
214
219
}
215
220
@@ -226,7 +231,8 @@ ffi_fn! {
226
231
/// The buffer is not null-terminated, see the `hyper_buf` functions for
227
232
/// getting the bytes and length.
228
233
fn hyper_response_headers_raw( resp: * const hyper_response) -> * const hyper_buf {
229
- match unsafe { & * resp } . 0 . extensions( ) . get:: <RawHeaders >( ) {
234
+ let resp = non_null!( & * resp ?= std:: ptr:: null( ) ) ;
235
+ match resp. 0 . extensions( ) . get:: <RawHeaders >( ) {
230
236
Some ( raw) => & raw. 0 ,
231
237
None => std:: ptr:: null( ) ,
232
238
}
@@ -245,7 +251,7 @@ ffi_fn! {
245
251
fn hyper_response_version( resp: * const hyper_response) -> c_int {
246
252
use http:: Version ;
247
253
248
- match unsafe { & * resp } . 0 . version( ) {
254
+ match non_null! ( & * resp ?= 0 ) . 0 . version( ) {
249
255
Version :: HTTP_10 => super :: HYPER_HTTP_VERSION_1_0 ,
250
256
Version :: HTTP_11 => super :: HYPER_HTTP_VERSION_1_1 ,
251
257
Version :: HTTP_2 => super :: HYPER_HTTP_VERSION_2 ,
@@ -269,7 +275,7 @@ ffi_fn! {
269
275
///
270
276
/// It is safe to free the response even after taking ownership of its body.
271
277
fn hyper_response_body( resp: * mut hyper_response) -> * mut hyper_body {
272
- let body = std:: mem:: take( unsafe { & mut * resp } . 0 . body_mut( ) ) ;
278
+ let body = std:: mem:: take( non_null! ( & mut * resp ?= std :: ptr :: null_mut ( ) ) . 0 . body_mut( ) ) ;
273
279
Box :: into_raw( Box :: new( hyper_body( body) ) )
274
280
} ?= std:: ptr:: null_mut( )
275
281
}
@@ -331,7 +337,7 @@ ffi_fn! {
331
337
/// The callback should return `HYPER_ITER_CONTINUE` to keep iterating, or
332
338
/// `HYPER_ITER_BREAK` to stop.
333
339
fn hyper_headers_foreach( headers: * const hyper_headers, func: hyper_headers_foreach_callback, userdata: * mut c_void) {
334
- let headers = unsafe { & * headers } ;
340
+ let headers = non_null! ( & * headers ?= ( ) ) ;
335
341
// For each header name/value pair, there may be a value in the casemap
336
342
// that corresponds to the HeaderValue. So, we iterator all the keys,
337
343
// and for each one, try to pair the originally cased name with the value.
@@ -366,7 +372,7 @@ ffi_fn! {
366
372
///
367
373
/// This overwrites any previous value set for the header.
368
374
fn hyper_headers_set( headers: * mut hyper_headers, name: * const u8 , name_len: size_t, value: * const u8 , value_len: size_t) -> hyper_code {
369
- let headers = unsafe { & mut * headers } ;
375
+ let headers = non_null! ( & mut * headers ?= hyper_code :: HYPERE_INVALID_ARG ) ;
370
376
match unsafe { raw_name_value( name, name_len, value, value_len) } {
371
377
Ok ( ( name, value, orig_name) ) => {
372
378
headers. headers. insert( & name, value) ;
@@ -384,7 +390,7 @@ ffi_fn! {
384
390
/// If there were already existing values for the name, this will append the
385
391
/// new value to the internal list.
386
392
fn hyper_headers_add( headers: * mut hyper_headers, name: * const u8 , name_len: size_t, value: * const u8 , value_len: size_t) -> hyper_code {
387
- let headers = unsafe { & mut * headers } ;
393
+ let headers = non_null! ( & mut * headers ?= hyper_code :: HYPERE_INVALID_ARG ) ;
388
394
389
395
match unsafe { raw_name_value( name, name_len, value, value_len) } {
390
396
Ok ( ( name, value, orig_name) ) => {
0 commit comments