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