Skip to content

Commit 38e1f45

Browse files
committed
feature: Make panic_if_null optional with panic-if-null feature
1 parent 62a509a commit 38e1f45

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ alloc = []
1818
# Allow to use some functions for FFI C types
1919
c-types = ["std"]
2020
# Check pointers before to use it to panic if it is null
21+
panic-if-null = []

src/c.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::os::raw::c_char;
66
use std::ffi::CStr;
77

8+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
89
use super::panic_if_null;
910

1011
/// Convert a reference to a C string into a static reference to Rust `str`.
@@ -19,6 +20,7 @@ use super::panic_if_null;
1920
#[must_use]
2021
#[inline]
2122
pub unsafe fn ref_str<'a>(string: *const c_char) -> &'a str {
23+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
2224
panic_if_null(string);
2325
// CAUTION: this is unsafe
2426
let string = CStr::from_ptr(string);

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::boxed::Box;
2424
#[cfg(all(feature = "std", feature = "c-types"))]
2525
pub mod c;
2626

27+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
2728
#[inline]
2829
fn panic_if_null<T>(pointer: *const T) {
2930
if pointer.is_null() {
@@ -69,6 +70,7 @@ pub unsafe fn free<T>(pointer: *mut T) {
6970
#[cfg(any(feature = "alloc", feature = "std"))]
7071
#[inline]
7172
pub unsafe fn own_back<T>(pointer: *mut T) -> T {
73+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
7274
panic_if_null(pointer);
7375
// CAUTION: this is unsafe
7476
let boxed = Box::from_raw(pointer);
@@ -87,6 +89,7 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> T {
8789
/// That could produce a HEAP error that produce a crash.
8890
#[inline]
8991
pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
92+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
9093
panic_if_null(pointer);
9194
// CAUTION: this is unsafe
9295
return &*pointer;
@@ -104,6 +107,7 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
104107
/// That could produce a HEAP error that produce a crash.
105108
#[inline]
106109
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> &'a mut T {
110+
#[cfg(any(feature = "panic-if-null", debug_assertions))]
107111
panic_if_null(pointer);
108112
// CAUTION: this is unsafe
109113
return &mut *pointer;

0 commit comments

Comments
 (0)