ndarray/
impl_internal_constructors.rs

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
9use std::ptr::NonNull;
10
11use crate::imp_prelude::*;
12
13// internal "builder-like" methods
14impl<A, S> ArrayBase<S, Ix1>
15where
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,
29            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
39impl<A, S, D> ArrayBase<S, D>
40where
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        debug_assert_eq!(strides.ndim(), dim.ndim());
59        ArrayBase {
60            data: self.data,
61            ptr: self.ptr,
62            dim,
63            strides,
64        }
65    }
66}