-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change ArrayBase.ptr to NonNull type (#683)
* Switch ArrayBase.ptr to be NonNull * Handle NonNull ArrayBase.ptr in linalg * Reorder impl_clone imports * Switch entirely to NonNull pointers in data_traits * Use unchecked NonNull creation in RawArrayViews * MAINT: Add dependency on rawpointer rawpointer 0.2 implements offset methods for NonNull<T>, which will be useful for us. This was already a dev-dependency, and also already a regular transitive dependency (from matrixmultiply). * Use NonNull::offset method and factor out nonnull constructors Add two constructors that suffice for ndarray: - Get NonNull<T> from Vec<T>'s buffer pointer - Get NonNull<T> from raw pointer, and check if it's nonnull with a debug assertion In all other cases we can use .offset() on the NonNull (method from rawpointer 0.2, so we don't have to explicit roundtrip through raw pointers when we offset NonNull<T>. For safety, see this documentation in the rawpointer crate, which says why using .offset() on NonNull is safe. NonNull<T> supports the same offsetting methods under the same safety constraints as the other raw pointer implementations. There is no difference - both when offsetting *mut T and NonNull<T>, the offset is only well defined if we remain inside the same object or one-past the end, and we can never land in a null pointer while obeying those rules.
- Loading branch information
1 parent
c9c6112
commit b98c844
Showing
17 changed files
with
131 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2019 bluss and ndarray developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Extension traits and utility functions for types from outside ndarray | ||
pub(crate) mod nonnull; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::ptr::NonNull; | ||
|
||
/// Return a NonNull<T> pointer to the vector's data | ||
pub(crate) fn nonnull_from_vec_data<T>(v: &mut Vec<T>) -> NonNull<T> { | ||
// this pointer is guaranteed to be non-null | ||
unsafe { NonNull::new_unchecked(v.as_mut_ptr()) } | ||
} | ||
|
||
/// Converts `ptr` to `NonNull<T>` | ||
/// | ||
/// Safety: `ptr` *must* be non-null. | ||
/// This is checked with a debug assertion, and will panic if this is not true, | ||
/// but treat this as an unconditional conversion. | ||
#[inline] | ||
pub(crate) unsafe fn nonnull_debug_checked_from_ptr<T>(ptr: *mut T) -> NonNull<T> { | ||
debug_assert!(!ptr.is_null()); | ||
NonNull::new_unchecked(ptr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.