Struct UninitNumericArray

Source
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>

Source

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?
examples/basic_types.rs (line 90)
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
Hide additional examples
examples/tests/test_native_args.rs (line 117)
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}
examples/tests/test_images.rs (line 8)
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}
Source

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.
Source

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.

Source

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?
examples/basic_types.rs (line 92)
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
Hide additional examples
examples/tests/test_native_args.rs (line 119)
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}
examples/tests/test_images.rs (line 13)
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}
Source

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?
examples/basic_types.rs (line 98)
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
Hide additional examples
examples/tests/test_native_args.rs (line 125)
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}
examples/tests/test_images.rs (line 21)
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}

Auto Trait Implementations§

§

impl<T> Freeze for UninitNumericArray<T>

§

impl<T> RefUnwindSafe for UninitNumericArray<T>
where T: RefUnwindSafe,

§

impl<T> !Send for UninitNumericArray<T>

§

impl<T> !Sync for UninitNumericArray<T>

§

impl<T> Unpin for UninitNumericArray<T>
where T: Unpin,

§

impl<T> UnwindSafe for UninitNumericArray<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.