Skip to content

Commit 50419d3

Browse files
committed
FEAT: Add internal array builder methods from_data_ptr and with_strides_dim
These methods will be used instead of the ArrayBase struct constructor expression.
1 parent cb544a0 commit 50419d3

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/impl_internal_constructors.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2021 bluss and ndarray developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use std::ptr::NonNull;
10+
11+
use crate::imp_prelude::*;
12+
13+
// internal "builder-like" methods
14+
impl<A, S> ArrayBase<S, Ix1>
15+
where
16+
S: RawData<Elem = A>,
17+
{
18+
/// Create an (initially) empty one-dimensional array from the given data and array head
19+
/// pointer
20+
///
21+
/// ## Safety
22+
///
23+
/// The caller must ensure that the data storage and pointer is valid.
24+
///
25+
/// See ArrayView::from_shape_ptr for general pointer validity documentation.
26+
pub(crate) unsafe fn from_data_ptr(data: S, ptr: NonNull<A>) -> Self {
27+
let array = ArrayBase {
28+
data: data,
29+
ptr: ptr,
30+
dim: Ix1(0),
31+
strides: Ix1(1),
32+
};
33+
debug_assert!(array.pointer_is_inbounds());
34+
array
35+
}
36+
}
37+
38+
// internal "builder-like" methods
39+
impl<A, S, D> ArrayBase<S, D>
40+
where
41+
S: RawData<Elem = A>,
42+
D: Dimension,
43+
{
44+
45+
/// Set strides and dimension of the array to the new values
46+
///
47+
/// The argument order with strides before dimensions is used because strides are often
48+
/// computed as derived from the dimension.
49+
///
50+
/// ## Safety
51+
///
52+
/// The caller needs to ensure that the new strides and dimensions are correct
53+
/// for the array data.
54+
pub(crate) unsafe fn with_strides_dim<E>(self, strides: E, dim: E) -> ArrayBase<S, E>
55+
where
56+
E: Dimension
57+
{
58+
ArrayBase {
59+
data: self.data,
60+
ptr: self.ptr,
61+
dim,
62+
strides,
63+
}
64+
}
65+
}

src/impl_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ where
19421942
self.index_axis_move(axis, 0)
19431943
}
19441944

1945-
fn pointer_is_inbounds(&self) -> bool {
1945+
pub(crate) fn pointer_is_inbounds(&self) -> bool {
19461946
match self.data._data_slice() {
19471947
None => {
19481948
// special case for non-owned views

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ impl<'a, A> CowRepr<'a, A> {
14881488

14891489
mod impl_clone;
14901490

1491+
mod impl_internal_constructors;
14911492
mod impl_constructors;
14921493

14931494
mod impl_methods;

0 commit comments

Comments
 (0)