pub struct UninitNumericArray<T: NumericArrayType>(/* private fields */);
Expand description
Represents an allocated NumericArray
whose elements have not yet been initialized.
Use as_slice_mut()
to initialize the
elements of this UninitNumericArray
.
Implementations§
Source§impl<T: NumericArrayType> UninitNumericArray<T>
impl<T: NumericArrayType> UninitNumericArray<T>
Sourcepub fn from_dimensions(dimensions: &[usize]) -> UninitNumericArray<T>
pub fn from_dimensions(dimensions: &[usize]) -> UninitNumericArray<T>
Construct a new uninitialized NumericArray
with the specified dimensions.
§Panics
This function will panic if UninitNumericArray::try_from_dimensions()
returns
an error.
Examples found in repository?
88fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
89 let mut bools: UninitNumericArray<u8> =
90 UninitNumericArray::from_dimensions(list.dimensions());
91
92 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
93 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
94
95 entry.write(u8::from(elem.is_positive()));
96 }
97
98 unsafe { bools.assume_init() }
99}
More examples
115fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
116 let mut bools: UninitNumericArray<u8> =
117 UninitNumericArray::from_dimensions(list.dimensions());
118
119 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
120 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
121
122 entry.write(u8::from(elem.is_positive()));
123 }
124
125 unsafe { bools.assume_init() }
126}
7fn test_image_arg(image: &Image<bool>) -> NumericArray<i8> {
8 let mut array = UninitNumericArray::from_dimensions(&[image.flattened_length()]);
9
10 for pair in image
11 .as_slice()
12 .iter()
13 .zip(array.as_slice_mut().into_iter())
14 {
15 let (pixel, elem): (&i8, &mut std::mem::MaybeUninit<i8>) = pair;
16 elem.write(*pixel);
17 }
18
19 // Safety: We iterated over every element in `array` and initialized it with the
20 // corresponding pixel value from `image`.
21 unsafe { array.assume_init() }
22}
Sourcepub fn try_from_dimensions(
dimensions: &[usize],
) -> Result<UninitNumericArray<T>, errcode_t>
pub fn try_from_dimensions( dimensions: &[usize], ) -> Result<UninitNumericArray<T>, errcode_t>
Try to construct a new uninitialized NumericArray with the specified dimensions.
This function will return an error if:
dimensions
is empty.- the product of
dimensions
is equal to 0. - the underlying allocation function returns
NULL
.
Sourcepub fn init_from_slice(self, source: &[T]) -> NumericArray<T>
pub fn init_from_slice(self, source: &[T]) -> NumericArray<T>
§Panics
This function will panic if source
does not have the same length as
this array’s as_slice_mut()
slice.
Sourcepub fn as_slice_mut(&mut self) -> &mut [MaybeUninit<T>]
pub fn as_slice_mut(&mut self) -> &mut [MaybeUninit<T>]
Mutable access to the elements of this UninitNumericArray
.
This function returns a mutable slice of std::mem::MaybeUninit<T>
. This is done
because it is undefined behavior in Rust to construct a &
(or &mut
) reference
to a value which has not been initialized. Note that it is undefined behavior even
if the reference is never read from. The MaybeUninit
type explicitly makes the
compiler aware that the T
value might not be initialized.
§Example
Construct the numeric array {1, 2, 3, 4, 5}
.
use wolfram_library_link::{NumericArray, UninitNumericArray};
// Construct a `1x5` numeric array with elements of type `f64`.
let mut uninit = UninitNumericArray::<f64>::from_dimensions(&[5]);
for (index, elem) in uninit.as_slice_mut().into_iter().enumerate() {
elem.write(index as f64 + 1.0);
}
// Now that we've taken responsibility for initializing every
// element of the UninitNumericArray, we've upheld the
// invariant necessary to make a call to `assume_init()` safe.
let array: NumericArray<f64> = unsafe { uninit.assume_init() };
See assume_init()
.
Examples found in repository?
88fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
89 let mut bools: UninitNumericArray<u8> =
90 UninitNumericArray::from_dimensions(list.dimensions());
91
92 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
93 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
94
95 entry.write(u8::from(elem.is_positive()));
96 }
97
98 unsafe { bools.assume_init() }
99}
More examples
115fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
116 let mut bools: UninitNumericArray<u8> =
117 UninitNumericArray::from_dimensions(list.dimensions());
118
119 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
120 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
121
122 entry.write(u8::from(elem.is_positive()));
123 }
124
125 unsafe { bools.assume_init() }
126}
7fn test_image_arg(image: &Image<bool>) -> NumericArray<i8> {
8 let mut array = UninitNumericArray::from_dimensions(&[image.flattened_length()]);
9
10 for pair in image
11 .as_slice()
12 .iter()
13 .zip(array.as_slice_mut().into_iter())
14 {
15 let (pixel, elem): (&i8, &mut std::mem::MaybeUninit<i8>) = pair;
16 elem.write(*pixel);
17 }
18
19 // Safety: We iterated over every element in `array` and initialized it with the
20 // corresponding pixel value from `image`.
21 unsafe { array.assume_init() }
22}
Sourcepub unsafe fn assume_init(self) -> NumericArray<T>
pub unsafe fn assume_init(self) -> NumericArray<T>
Assume that this NumericArray’s elements have been initialized.
Use as_slice_mut()
to initialize the values
in this array.
§Safety
This function must only be called once all elements of this NumericArray have
been initialized. It is undefined behavior to construct a NumericArray
without
first initializing the data array.
Examples found in repository?
88fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
89 let mut bools: UninitNumericArray<u8> =
90 UninitNumericArray::from_dimensions(list.dimensions());
91
92 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
93 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
94
95 entry.write(u8::from(elem.is_positive()));
96 }
97
98 unsafe { bools.assume_init() }
99}
More examples
115fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
116 let mut bools: UninitNumericArray<u8> =
117 UninitNumericArray::from_dimensions(list.dimensions());
118
119 for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
120 let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;
121
122 entry.write(u8::from(elem.is_positive()));
123 }
124
125 unsafe { bools.assume_init() }
126}
7fn test_image_arg(image: &Image<bool>) -> NumericArray<i8> {
8 let mut array = UninitNumericArray::from_dimensions(&[image.flattened_length()]);
9
10 for pair in image
11 .as_slice()
12 .iter()
13 .zip(array.as_slice_mut().into_iter())
14 {
15 let (pixel, elem): (&i8, &mut std::mem::MaybeUninit<i8>) = pair;
16 elem.write(*pixel);
17 }
18
19 // Safety: We iterated over every element in `array` and initialized it with the
20 // corresponding pixel value from `image`.
21 unsafe { array.assume_init() }
22}