about summary refs log tree commit diff
path: root/library/core/src/slice/raw.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-12 11:10:50 +0000
committerbors <bors@rust-lang.org>2024-06-12 11:10:50 +0000
commitbbe9a9c20bac888efae2c7f033fb6cb3925a65b7 (patch)
tree7b1bfe4686f672ce09818155b2144d3b8d1aee3e /library/core/src/slice/raw.rs
parent02c7a5921e3de5c2b3ecb2e0082c1dafce0729a1 (diff)
parent0a5284917b26496f608bff8737f32fa1b502b3d5 (diff)
downloadrust-bbe9a9c20bac888efae2c7f033fb6cb3925a65b7.tar.gz
rust-bbe9a9c20bac888efae2c7f033fb6cb3925a65b7.zip
Auto merge of #126319 - workingjubilee:rollup-lendnud, r=workingjubilee
Rollup of 16 pull requests

Successful merges:

 - #123374 (DOC: Add FFI example for slice::from_raw_parts())
 - #124514 (Recommend to never display zero disambiguators when demangling v0 symbols)
 - #125978 (Cleanup: HIR ty lowering: Consolidate the places that do assoc item probing & access checking)
 - #125980 (Nvptx remove direct passmode)
 - #126187 (For E0277 suggest adding `Result` return type for function when using QuestionMark `?` in the body.)
 - #126210 (docs(core): make more const_ptr doctests assert instead of printing)
 - #126249 (Simplify `[T; N]::try_map` signature)
 - #126256 (Add {{target}} substitution to compiletest)
 - #126263 (Make issue-122805.rs big endian compatible)
 - #126281 (set_env: State the conclusion upfront)
 - #126286 (Make `storage-live.rs` robust against rustc internal changes.)
 - #126287 (Update a cranelift patch file for formatting changes.)
 - #126301 (Use `tidy` to sort crate attributes for all compiler crates.)
 - #126305 (Make PathBuf less Ok with adding UTF-16 then `into_string`)
 - #126310 (Migrate run make prefer rlib)
 - #126314 (fix RELEASES: we do not support upcasting to auto traits)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src/slice/raw.rs')
-rw-r--r--library/core/src/slice/raw.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs
index 3f4305866e6..280aead270e 100644
--- a/library/core/src/slice/raw.rs
+++ b/library/core/src/slice/raw.rs
@@ -82,6 +82,39 @@ use crate::ub_checks;
 /// }
 /// ```
 ///
+/// ### FFI: Handling null pointers
+///
+/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
+/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
+/// behavior.
+///
+/// ```
+/// use std::slice;
+///
+/// /// Sum the elements of an FFI slice.
+/// ///
+/// /// # Safety
+/// ///
+/// /// If ptr is not NULL, it must be correctly aligned and
+/// /// point to `len` initialized items of type `f32`.
+/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 {
+///     let data = if ptr.is_null() {
+///         // `len` is assumed to be 0.
+///         &[]
+///     } else {
+///         // SAFETY: see function docstring.
+///         unsafe { slice::from_raw_parts(ptr, len) }
+///     };
+///     data.into_iter().sum()
+/// }
+///
+/// // This could be the result of C++'s std::vector::data():
+/// let ptr = std::ptr::null();
+/// // And this could be std::vector::size():
+/// let len = 0;
+/// assert_eq!(unsafe { sum_slice(ptr, len) }, 0.0);
+/// ```
+///
 /// [valid]: ptr#safety
 /// [`NonNull::dangling()`]: ptr::NonNull::dangling
 #[inline]