Extend the example code and assert the result
This commit is contained in:
parent
22319bf6ba
commit
4df1303cff
1 changed files with 10 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue