@@ -12,7 +12,7 @@ use alloc::{
12
12
} ;
13
13
use core:: convert:: From ;
14
14
use core:: fmt;
15
- use core:: num:: TryFromIntError ;
15
+ use core:: num:: { NonZeroI16 , TryFromIntError } ;
16
16
use core:: str:: { self , Utf8Error } ;
17
17
18
18
/// Contains the C-compatible error codes.
@@ -22,7 +22,7 @@ pub mod code {
22
22
$(
23
23
#[ doc = $doc]
24
24
) *
25
- pub const $err: super :: Error = super :: Error ( -( crate :: bindings:: $err as i32 ) ) ;
25
+ pub const $err: super :: Error = super :: Error ( unsafe { super :: NonZeroI16 :: new_unchecked ( -( crate :: bindings:: $err as i16 ) ) } ) ;
26
26
} ;
27
27
}
28
28
@@ -317,7 +317,7 @@ pub mod code {
317
317
///
318
318
/// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
319
319
#[ derive( Clone , Copy , PartialEq , Eq ) ]
320
- pub struct Error ( core :: ffi :: c_int ) ;
320
+ pub struct Error ( NonZeroI16 ) ;
321
321
322
322
impl Error {
323
323
/// Creates an [`Error`] from a kernel error code.
@@ -336,7 +336,7 @@ impl Error {
336
336
337
337
// INVARIANT: The check above ensures the type invariant
338
338
// will hold.
339
- Error ( errno)
339
+ Error ( unsafe { NonZeroI16 :: new_unchecked ( errno as i16 ) } )
340
340
}
341
341
342
342
/// Creates an [`Error`] from a kernel error code.
@@ -347,19 +347,19 @@ impl Error {
347
347
pub ( crate ) unsafe fn from_kernel_errno_unchecked ( errno : core:: ffi:: c_int ) -> Error {
348
348
// INVARIANT: The contract ensures the type invariant
349
349
// will hold.
350
- Error ( errno)
350
+ Error ( unsafe { NonZeroI16 :: new_unchecked ( errno as i16 ) } )
351
351
}
352
352
353
353
/// Returns the kernel error code.
354
354
pub fn to_kernel_errno ( self ) -> core:: ffi:: c_int {
355
- self . 0
355
+ self . 0 . get ( ) as i32
356
356
}
357
357
358
358
/// Returns a string representing the error, if one exists.
359
359
#[ cfg( not( testlib) ) ]
360
360
pub fn name ( & self ) -> Option < & ' static CStr > {
361
361
// SAFETY: Just an FFI call, there are no extra safety requirements.
362
- let ptr = unsafe { bindings:: errname ( -self . 0 ) } ;
362
+ let ptr = unsafe { bindings:: errname ( -( self . 0 . get ( ) as i32 ) ) } ;
363
363
if ptr. is_null ( ) {
364
364
None
365
365
} else {
@@ -383,7 +383,10 @@ impl fmt::Debug for Error {
383
383
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
384
384
match self . name ( ) {
385
385
// Print out number if no name can be found.
386
- None => f. debug_tuple ( "Error" ) . field ( & -self . 0 ) . finish ( ) ,
386
+ None => f
387
+ . debug_tuple ( "Error" )
388
+ . field ( & -( self . 0 . get ( ) as i32 ) )
389
+ . finish ( ) ,
387
390
// SAFETY: These strings are ASCII-only.
388
391
Some ( name) => f
389
392
. debug_tuple ( unsafe { str:: from_utf8_unchecked ( name) } )
0 commit comments