Skip to content

Commit b9c12b0

Browse files
committed
FEAT: Rename maybe_uninit to Array::uninit and move to base type
Old type situation: `Array<MaybeUninit<i32>, D>::maybe_uninit()` New type situation: `Array<i32, D>::uninit()` The link between the regular array storage type and the maybeuninit version of it is made explicit in the DataOwned trait and this makes it much easier to work with this constructor in generic code. The new name is "uninit", just like many types in std (likej `Box::uninit`). The old name is deprecated. Because of the unfortunate generics situation of the old name, the implementation is a copy & paste (short allegory - inside ArrayBase::uninit we have types S and S::MaybeUninit "available" as known type, inside ArrayBase::maybe_uninit we only have a `DataOwned<Elem = MaybeUninit<_>>` type "avaialable" and no way to find the corresponding "plain" storage type.)
1 parent 2af780f commit b9c12b0

File tree

8 files changed

+46
-19
lines changed

8 files changed

+46
-19
lines changed

benches/bench1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn add_2d_alloc_zip_uninit(bench: &mut test::Bencher) {
271271
let a = Array::<i32, _>::zeros((ADD2DSZ, ADD2DSZ));
272272
let b = Array::<i32, _>::zeros((ADD2DSZ, ADD2DSZ));
273273
bench.iter(|| unsafe {
274-
let mut c = Array::<MaybeUninit<i32>, _>::maybe_uninit(a.dim());
274+
let mut c = Array::<i32, _>::uninit(a.dim());
275275
azip!((&a in &a, &b in &b, c in c.raw_view_mut().cast::<i32>())
276276
c.write(a + b)
277277
);

examples/sort-axis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ where
102102
assert_eq!(axis_len, perm.indices.len());
103103
debug_assert!(perm.correct());
104104

105-
let mut result = Array::maybe_uninit(self.dim());
105+
let mut result = Array::uninit(self.dim());
106106

107107
unsafe {
108108
// logically move ownership of all elements from self into result

src/data_traits.rs

+10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//! The data (inner representation) traits for ndarray
1010
1111
use rawpointer::PointerExt;
12+
1213
use std::mem::{self, size_of};
14+
use std::mem::MaybeUninit;
1315
use std::ptr::NonNull;
1416
use alloc::sync::Arc;
1517
use alloc::vec::Vec;
@@ -401,6 +403,10 @@ unsafe impl<'a, A> DataMut for ViewRepr<&'a mut A> {}
401403
///
402404
/// ***Internal trait, see `Data`.***
403405
pub unsafe trait DataOwned: Data {
406+
/// Corresponding owned data with MaybeUninit elements
407+
type MaybeUninit: DataOwned<Elem = MaybeUninit<Self::Elem>>
408+
+ RawDataSubst<Self::Elem, Output=Self>;
409+
404410
#[doc(hidden)]
405411
fn new(elements: Vec<Self::Elem>) -> Self;
406412

@@ -421,6 +427,8 @@ unsafe impl<A> DataShared for OwnedArcRepr<A> {}
421427
unsafe impl<'a, A> DataShared for ViewRepr<&'a A> {}
422428

423429
unsafe impl<A> DataOwned for OwnedRepr<A> {
430+
type MaybeUninit = OwnedRepr<MaybeUninit<A>>;
431+
424432
fn new(elements: Vec<A>) -> Self {
425433
OwnedRepr::from(elements)
426434
}
@@ -430,6 +438,8 @@ unsafe impl<A> DataOwned for OwnedRepr<A> {
430438
}
431439

432440
unsafe impl<A> DataOwned for OwnedArcRepr<A> {
441+
type MaybeUninit = OwnedArcRepr<MaybeUninit<A>>;
442+
433443
fn new(elements: Vec<A>) -> Self {
434444
OwnedArcRepr(Arc::new(OwnedRepr::from(elements)))
435445
}

src/impl_constructors.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ where
480480

481481
/// Create an array with uninitalized elements, shape `shape`.
482482
///
483-
/// Prefer to use [`maybe_uninit()`](ArrayBase::maybe_uninit) if possible, because it is
483+
/// Prefer to use [`uninit()`](ArrayBase::uninit) if possible, because it is
484484
/// easier to use correctly.
485485
///
486486
/// **Panics** if the number of elements in `shape` would overflow isize.
@@ -512,13 +512,7 @@ where
512512
v.set_len(size);
513513
Self::from_shape_vec_unchecked(shape, v)
514514
}
515-
}
516515

517-
impl<S, A, D> ArrayBase<S, D>
518-
where
519-
S: DataOwned<Elem = MaybeUninit<A>>,
520-
D: Dimension,
521-
{
522516
/// Create an array with uninitalized elements, shape `shape`.
523517
///
524518
/// The uninitialized elements of type `A` are represented by the type `MaybeUninit<A>`,
@@ -550,7 +544,7 @@ where
550544
///
551545
/// fn shift_by_two(a: &Array2<f32>) -> Array2<f32> {
552546
/// // create an uninitialized array
553-
/// let mut b = Array2::maybe_uninit(a.dim());
547+
/// let mut b = Array2::uninit(a.dim());
554548
///
555549
/// // two first columns in b are two last in a
556550
/// // rest of columns in b are the initial columns in a
@@ -580,6 +574,29 @@ where
580574
///
581575
/// # shift_by_two(&Array2::zeros((8, 8)));
582576
/// ```
577+
pub fn uninit<Sh>(shape: Sh) -> ArrayBase<S::MaybeUninit, D>
578+
where
579+
Sh: ShapeBuilder<Dim = D>,
580+
{
581+
unsafe {
582+
let shape = shape.into_shape();
583+
let size = size_of_shape_checked_unwrap!(&shape.dim);
584+
let mut v = Vec::with_capacity(size);
585+
v.set_len(size);
586+
ArrayBase::from_shape_vec_unchecked(shape, v)
587+
}
588+
}
589+
}
590+
591+
impl<S, A, D> ArrayBase<S, D>
592+
where
593+
S: DataOwned<Elem = MaybeUninit<A>>,
594+
D: Dimension,
595+
{
596+
/// Create an array with uninitalized elements, shape `shape`.
597+
///
598+
/// This method has been renamed to `uninit`
599+
#[deprecated(note = "Renamed to `uninit`", since = "0.15.0")]
583600
pub fn maybe_uninit<Sh>(shape: Sh) -> Self
584601
where
585602
Sh: ShapeBuilder<Dim = D>,

src/linalg/impl_linalg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ where
326326

327327
// Avoid initializing the memory in vec -- set it during iteration
328328
unsafe {
329-
let mut c = Array1::maybe_uninit(m);
329+
let mut c = Array1::uninit(m);
330330
general_mat_vec_mul_impl(A::one(), self, rhs, A::zero(), c.raw_view_mut().cast::<A>());
331331
c.assume_init()
332332
}

src/stacking.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ where
9191

9292
// we can safely use uninitialized values here because we will
9393
// overwrite every one of them.
94-
let mut res = Array::maybe_uninit(res_dim);
94+
let mut res = Array::uninit(res_dim);
9595

9696
{
9797
let mut assign_view = res.view_mut();
@@ -159,7 +159,7 @@ where
159159

160160
// we can safely use uninitialized values here because we will
161161
// overwrite every one of them.
162-
let mut res = Array::maybe_uninit(res_dim);
162+
let mut res = Array::uninit(res_dim);
163163

164164
res.axis_iter_mut(axis)
165165
.zip(arrays.iter())

src/zip/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ where
814814
pub(crate) fn uninitalized_for_current_layout<T>(&self) -> Array<MaybeUninit<T>, D>
815815
{
816816
let is_f = self.prefer_f();
817-
Array::maybe_uninit(self.dimension.clone().set_f(is_f))
817+
Array::uninit(self.dimension.clone().set_f(is_f))
818818
}
819819
}
820820

tests/array-construct.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,18 @@ fn maybe_uninit_1() {
205205

206206
unsafe {
207207
// Array
208-
type Mat<D> = Array<MaybeUninit<f32>, D>;
208+
type Mat<D> = Array<f32, D>;
209209

210-
let mut a = Mat::maybe_uninit((10, 10));
210+
let mut a = Mat::uninit((10, 10));
211211
a.mapv_inplace(|_| MaybeUninit::new(1.));
212212

213213
let a_init = a.assume_init();
214214
assert_eq!(a_init, Array2::from_elem(a_init.dim(), 1.));
215215

216216
// ArcArray
217-
type ArcMat<D> = ArcArray<MaybeUninit<f32>, D>;
217+
type ArcMat<D> = ArcArray<f32, D>;
218218

219-
let mut a = ArcMat::maybe_uninit((10, 10));
219+
let mut a = ArcMat::uninit((10, 10));
220220
a.mapv_inplace(|_| MaybeUninit::new(1.));
221221
let a2 = a.clone();
222222

@@ -228,7 +228,7 @@ fn maybe_uninit_1() {
228228
assert_eq!(av_init, Array2::from_elem(a_init.dim(), 1.));
229229

230230
// RawArrayViewMut
231-
let mut a = Mat::maybe_uninit((10, 10));
231+
let mut a = Mat::uninit((10, 10));
232232
let v = a.raw_view_mut();
233233
Zip::from(v)
234234
.for_each(|ptr| *(*ptr).as_mut_ptr() = 1.);

0 commit comments

Comments
 (0)