diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2024-04-26 20:53:23 +0200 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2024-04-26 20:53:23 +0200 |
| commit | 4df1303cff1eeb3b0c7f00d70fecf160279e7263 (patch) | |
| tree | caf047ac2e349c2fa383d1564fbe8234ca6b26e7 | |
| parent | 22319bf6ba861eb0b5efb81aaf7616db30f4872f (diff) | |
| download | rust-4df1303cff1eeb3b0c7f00d70fecf160279e7263.tar.gz rust-4df1303cff1eeb3b0c7f00d70fecf160279e7263.zip | |
Extend the example code and assert the result
| -rw-r--r-- | library/core/src/slice/raw.rs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/library/core/src/slice/raw.rs b/library/core/src/slice/raw.rs index 8ff429218a4..efdd210c1fa 100644 --- a/library/core/src/slice/raw.rs +++ b/library/core/src/slice/raw.rs @@ -92,11 +92,13 @@ use crate::ub_checks; /// ``` /// 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 handle_slice(ptr: *const f32, len: usize) { +/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 { /// let data = if ptr.is_null() { /// // `len` is assumed to be 0. /// &[] @@ -104,9 +106,14 @@ use crate::ub_checks; /// // SAFETY: see function docstring. /// unsafe { slice::from_raw_parts(ptr, len) } /// }; -/// dbg!(data); -/// // ... +/// data.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 |
