ndarray/impl_special_element_types.rs
1// Copyright 2020 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::mem::MaybeUninit;
10
11use crate::imp_prelude::*;
12use crate::RawDataSubst;
13
14/// Methods specific to arrays with `MaybeUninit` elements.
15///
16/// ***See also all methods for [`ArrayBase`]***
17impl<A, S, D> ArrayBase<S, D>
18where
19 S: RawDataSubst<A, Elem = MaybeUninit<A>>,
20 D: Dimension,
21{
22 /// **Promise** that the array's elements are all fully initialized, and convert
23 /// the array from element type `MaybeUninit<A>` to `A`.
24 ///
25 /// For example, it can convert an `Array<MaybeUninit<f64>, D>` to `Array<f64, D>`.
26 ///
27 /// ## Safety
28 ///
29 /// Safe to use if all the array's elements have been initialized.
30 ///
31 /// Note that for owned and shared ownership arrays, the promise must include all of the
32 /// array's storage; it is for example possible to slice these in place, but that must
33 /// only be done after all elements have been initialized.
34 pub unsafe fn assume_init(self) -> ArrayBase<<S as RawDataSubst<A>>::Output, D>
35 {
36 let ArrayBase {
37 data,
38 ptr,
39 dim,
40 strides,
41 } = self;
42
43 // "transmute" from storage of MaybeUninit<A> to storage of A
44 let data = S::data_subst(data);
45 let ptr = ptr.cast::<A>();
46 ArrayBase::from_data_ptr(data, ptr).with_strides_dim(strides, dim)
47 }
48}