Skip to content

Commit 2a5af32

Browse files
committed
Extend the example code and assert the result
1 parent 678e5a0 commit 2a5af32

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

core/src/slice/raw.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,28 @@ use crate::ub_checks;
9292
/// ```
9393
/// use std::slice;
9494
///
95+
/// /// Sum the elements of an FFI slice.
96+
/// ///
9597
/// /// # Safety
9698
/// ///
9799
/// /// If ptr is not NULL, it must be correctly aligned and
98100
/// /// point to `len` initialized items of type `f32`.
99-
/// unsafe extern "C" fn handle_slice(ptr: *const f32, len: usize) {
101+
/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 {
100102
/// let data = if ptr.is_null() {
101103
/// // `len` is assumed to be 0.
102104
/// &[]
103105
/// } else {
104106
/// // SAFETY: see function docstring.
105107
/// unsafe { slice::from_raw_parts(ptr, len) }
106108
/// };
107-
/// dbg!(data);
108-
/// // ...
109+
/// data.sum()
109110
/// }
111+
///
112+
/// // This could be the result of C++'s std::vector::data():
113+
/// let ptr = std::ptr::null();
114+
/// // And this could be std::vector::size():
115+
/// let len = 0;
116+
/// assert_eq!(unsafe { sum_slice(ptr, len) }, 0.0);
110117
/// ```
111118
///
112119
/// [valid]: ptr#safety

0 commit comments

Comments
 (0)