Skip to content

Commit fdb9618

Browse files
committed
API: Deprecate Array::uninitialized. Use Array::uninit instead
Array::uninitialized is very hard to use correctly (internally and for our users). Prefer the new API with Array::uninit using `MaybeUninit<_>` instead.
1 parent 82150cb commit fdb9618

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

src/impl_constructors.rs

+38-35
Original file line numberDiff line numberDiff line change
@@ -478,41 +478,6 @@ where
478478
}
479479
}
480480

481-
/// Create an array with uninitalized elements, shape `shape`.
482-
///
483-
/// Prefer to use [`uninit()`](ArrayBase::uninit) if possible, because it is
484-
/// easier to use correctly.
485-
///
486-
/// **Panics** if the number of elements in `shape` would overflow isize.
487-
///
488-
/// ### Safety
489-
///
490-
/// Accessing uninitalized values is undefined behaviour. You must overwrite *all* the elements
491-
/// in the array after it is created; for example using
492-
/// [`raw_view_mut`](ArrayBase::raw_view_mut) or other low-level element access.
493-
///
494-
/// The contents of the array is indeterminate before initialization and it
495-
/// is an error to perform operations that use the previous values. For
496-
/// example it would not be legal to use `a += 1.;` on such an array.
497-
///
498-
/// This constructor is limited to elements where `A: Copy` (no destructors)
499-
/// to avoid users shooting themselves too hard in the foot.
500-
///
501-
/// (Also note that the constructors `from_shape_vec` and
502-
/// `from_shape_vec_unchecked` allow the user yet more control, in the sense
503-
/// that Arrays can be created from arbitrary vectors.)
504-
pub unsafe fn uninitialized<Sh>(shape: Sh) -> Self
505-
where
506-
A: Copy,
507-
Sh: ShapeBuilder<Dim = D>,
508-
{
509-
let shape = shape.into_shape();
510-
let size = size_of_shape_checked_unwrap!(&shape.dim);
511-
let mut v = Vec::with_capacity(size);
512-
v.set_len(size);
513-
Self::from_shape_vec_unchecked(shape, v)
514-
}
515-
516481
/// Create an array with uninitalized elements, shape `shape`.
517482
///
518483
/// The uninitialized elements of type `A` are represented by the type `MaybeUninit<A>`,
@@ -619,6 +584,44 @@ where
619584
}
620585
array
621586
}
587+
588+
#[deprecated(note = "This method is hard to use correctly. Use `uninit` instead.",
589+
since = "0.15.0")]
590+
/// Create an array with uninitalized elements, shape `shape`.
591+
///
592+
/// Prefer to use [`uninit()`](ArrayBase::uninit) if possible, because it is
593+
/// easier to use correctly.
594+
///
595+
/// **Panics** if the number of elements in `shape` would overflow isize.
596+
///
597+
/// ### Safety
598+
///
599+
/// Accessing uninitalized values is undefined behaviour. You must overwrite *all* the elements
600+
/// in the array after it is created; for example using
601+
/// [`raw_view_mut`](ArrayBase::raw_view_mut) or other low-level element access.
602+
///
603+
/// The contents of the array is indeterminate before initialization and it
604+
/// is an error to perform operations that use the previous values. For
605+
/// example it would not be legal to use `a += 1.;` on such an array.
606+
///
607+
/// This constructor is limited to elements where `A: Copy` (no destructors)
608+
/// to avoid users shooting themselves too hard in the foot.
609+
///
610+
/// (Also note that the constructors `from_shape_vec` and
611+
/// `from_shape_vec_unchecked` allow the user yet more control, in the sense
612+
/// that Arrays can be created from arbitrary vectors.)
613+
pub unsafe fn uninitialized<Sh>(shape: Sh) -> Self
614+
where
615+
A: Copy,
616+
Sh: ShapeBuilder<Dim = D>,
617+
{
618+
let shape = shape.into_shape();
619+
let size = size_of_shape_checked_unwrap!(&shape.dim);
620+
let mut v = Vec::with_capacity(size);
621+
v.set_len(size);
622+
Self::from_shape_vec_unchecked(shape, v)
623+
}
624+
622625
}
623626

624627
impl<S, A, D> ArrayBase<S, D>

tests/array-construct.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn test_arcarray_thread_safe() {
5252

5353
#[test]
5454
#[cfg(feature = "std")]
55+
#[allow(deprecated)] // uninitialized
5556
fn test_uninit() {
5657
unsafe {
5758
let mut a = Array::<f32, _>::uninitialized((3, 4).f());
@@ -192,12 +193,19 @@ fn deny_wraparound_from_shape_fn() {
192193

193194
#[should_panic]
194195
#[test]
195-
fn deny_wraparound_uninit() {
196+
#[allow(deprecated)] // uninitialized
197+
fn deny_wraparound_uninitialized() {
196198
unsafe {
197199
let _five_large = Array::<f32, _>::uninitialized((3, 7, 29, 36760123, 823996703));
198200
}
199201
}
200202

203+
#[should_panic]
204+
#[test]
205+
fn deny_wraparound_uninit() {
206+
let _five_large = Array::<f32, _>::uninit((3, 7, 29, 36760123, 823996703));
207+
}
208+
201209

202210
#[test]
203211
fn maybe_uninit_1() {

0 commit comments

Comments
 (0)