@@ -478,47 +478,6 @@ where
478478 }
479479 }
480480
481- /// Create an array with uninitalized elements, shape `shape`.
482- ///
483- /// Prefer to use [`maybe_uninit()`](ArrayBase::maybe_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- }
516-
517- impl < S , A , D > ArrayBase < S , D >
518- where
519- S : DataOwned < Elem = MaybeUninit < A > > ,
520- D : Dimension ,
521- {
522481 /// Create an array with uninitalized elements, shape `shape`.
523482 ///
524483 /// The uninitialized elements of type `A` are represented by the type `MaybeUninit<A>`,
@@ -550,7 +509,7 @@ where
550509 ///
551510 /// fn shift_by_two(a: &Array2<f32>) -> Array2<f32> {
552511 /// // create an uninitialized array
553- /// let mut b = Array2::maybe_uninit (a.dim());
512+ /// let mut b = Array2::uninit (a.dim());
554513 ///
555514 /// // two first columns in b are two last in a
556515 /// // rest of columns in b are the initial columns in a
@@ -580,6 +539,100 @@ where
580539 ///
581540 /// # shift_by_two(&Array2::zeros((8, 8)));
582541 /// ```
542+ pub fn uninit < Sh > ( shape : Sh ) -> ArrayBase < S :: MaybeUninit , D >
543+ where
544+ Sh : ShapeBuilder < Dim = D > ,
545+ {
546+ unsafe {
547+ let shape = shape. into_shape ( ) ;
548+ let size = size_of_shape_checked_unwrap ! ( & shape. dim) ;
549+ let mut v = Vec :: with_capacity ( size) ;
550+ v. set_len ( size) ;
551+ ArrayBase :: from_shape_vec_unchecked ( shape, v)
552+ }
553+ }
554+
555+ /// Create an array with uninitalized elements, shape `shape`.
556+ ///
557+ /// The uninitialized elements of type `A` are represented by the type `MaybeUninit<A>`,
558+ /// an easier way to handle uninit values correctly.
559+ ///
560+ /// The `builder` closure gets unshared access to the array through a raw view
561+ /// and can use it to modify the array before it is returned. This allows initializing
562+ /// the array for any owned array type (avoiding clone requirements for copy-on-write,
563+ /// because the array is unshared when initially created).
564+ ///
565+ /// Only *when* the array is completely initialized with valid elements, can it be
566+ /// converted to an array of `A` elements using [`.assume_init()`].
567+ ///
568+ /// **Panics** if the number of elements in `shape` would overflow isize.
569+ ///
570+ /// ### Safety
571+ ///
572+ /// The whole of the array must be initialized before it is converted
573+ /// using [`.assume_init()`] or otherwise traversed.
574+ ///
575+ pub ( crate ) fn build_uninit < Sh , F > ( shape : Sh , builder : F ) -> ArrayBase < S :: MaybeUninit , D >
576+ where
577+ Sh : ShapeBuilder < Dim = D > ,
578+ F : FnOnce ( RawArrayViewMut < MaybeUninit < A > , D > ) ,
579+ {
580+ let mut array = Self :: uninit ( shape) ;
581+ // Safe because: the array is unshared here
582+ unsafe {
583+ builder ( array. raw_view_mut_unchecked ( ) ) ;
584+ }
585+ array
586+ }
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+
625+ }
626+
627+ impl < S , A , D > ArrayBase < S , D >
628+ where
629+ S : DataOwned < Elem = MaybeUninit < A > > ,
630+ D : Dimension ,
631+ {
632+ /// Create an array with uninitalized elements, shape `shape`.
633+ ///
634+ /// This method has been renamed to `uninit`
635+ #[ deprecated( note = "Renamed to `uninit`" , since = "0.15.0" ) ]
583636 pub fn maybe_uninit < Sh > ( shape : Sh ) -> Self
584637 where
585638 Sh : ShapeBuilder < Dim = D > ,
0 commit comments