@@ -24,13 +24,15 @@ use std::boxed::Box;
2424#[ cfg( all( feature = "std" , feature = "c-types" ) ) ]
2525pub mod c;
2626
27- #[ cfg( any( feature = "panic-if-null" , debug_assertions) ) ]
27+ pub mod error;
28+
2829#[ inline]
29- fn panic_if_null < T > ( pointer : * const T ) {
30+ fn null_error_check < T > ( pointer : * const T ) -> Result < ( ) , crate :: error :: PointerError > {
3031 if pointer. is_null ( ) {
31- log:: error!( "Trying to use a NULL pointer as a opaque pointer to Rust data" ) ;
32- unreachable ! ( "Trying to use a NULL pointer as a opaque pointer to Rust data" ) ;
32+ log:: error!( "Using a NULL pointer as a opaque pointer to Rust data" ) ;
33+ return Err ( crate :: error :: PointerError :: NulPointer ) ;
3334 }
35+ return Ok ( ( ) ) ;
3436}
3537
3638/// Get a heap-allocated raw pointer without ownership.
@@ -47,7 +49,7 @@ pub fn raw<T>(data: T) -> *mut T {
4749#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
4850#[ inline]
4951pub unsafe fn free < T > ( pointer : * mut T ) {
50- own_back ( pointer) ;
52+ let _ = own_back ( pointer) ; // Ignore the must use lint as previous behavior was ignore null pointers
5153}
5254
5355/// Opposite of [`raw<T>()`], to use Rust's ownership as usually.
@@ -60,12 +62,11 @@ pub unsafe fn free<T>(pointer: *mut T) {
6062#[ doc( alias = "free" ) ]
6163#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
6264#[ inline]
63- pub unsafe fn own_back < T > ( pointer : * mut T ) -> T {
64- #[ cfg( any( feature = "panic-if-null" , debug_assertions) ) ]
65- panic_if_null ( pointer) ;
65+ pub unsafe fn own_back < T > ( pointer : * mut T ) -> Result < T , crate :: error:: PointerError > {
66+ null_error_check ( pointer) ?;
6667 // CAUTION: this is the unsafe part of the function.
6768 let boxed = Box :: from_raw ( pointer) ;
68- return * boxed;
69+ return Ok ( * boxed) ;
6970}
7071
7172/// Reference to a object but without back to own it.
@@ -77,11 +78,10 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> T {
7778///
7879/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
7980#[ inline]
80- pub unsafe fn object < ' a , T > ( pointer : * const T ) -> & ' a T {
81- #[ cfg( any( feature = "panic-if-null" , debug_assertions) ) ]
82- panic_if_null ( pointer) ;
81+ pub unsafe fn object < ' a , T > ( pointer : * const T ) -> Result < & ' a T , crate :: error:: PointerError > {
82+ null_error_check ( pointer) ?;
8383 // CAUTION: this is unsafe
84- return & * pointer;
84+ return Ok ( & * pointer) ;
8585}
8686
8787/// Mutable reference to a object but without back to own it.
@@ -93,9 +93,8 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
9393///
9494/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
9595#[ inline]
96- pub unsafe fn mut_object < ' a , T > ( pointer : * mut T ) -> & ' a mut T {
97- #[ cfg( any( feature = "panic-if-null" , debug_assertions) ) ]
98- panic_if_null ( pointer) ;
96+ pub unsafe fn mut_object < ' a , T > ( pointer : * mut T ) -> Result < & ' a mut T , crate :: error:: PointerError > {
97+ null_error_check ( pointer) ?;
9998 // CAUTION: this is unsafe
100- return & mut * pointer;
99+ return Ok ( & mut * pointer) ;
101100}
0 commit comments