We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9f0af96 commit 08f761fCopy full SHA for 08f761f
core/src/slice/raw.rs
@@ -83,6 +83,27 @@ use crate::ub_checks;
83
/// }
84
/// ```
85
///
86
+/// ### FFI: Handling null pointers
87
+///
88
+/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
89
+/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
90
+/// behavior.
91
92
+/// ```
93
+/// use std::slice;
94
95
+/// unsafe extern "C" fn handle_slice(ptr: *const f32, len: usize) {
96
+/// let data = if ptr.is_null() {
97
+/// // `len` is assumed to be 0.
98
+/// &[]
99
+/// } else {
100
+/// unsafe { slice::from_raw_parts(ptr, len) }
101
+/// };
102
+/// dbg!(data);
103
+/// // ...
104
+/// }
105
106
107
/// [valid]: ptr#safety
108
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
109
#[inline]
0 commit comments