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