Struct NumericArray

Source
pub struct NumericArray<T = ()>(/* private fields */);
Expand description

Native Wolfram NumericArrayWL.

This type is an ABI-compatible wrapper around wolfram_library_link_sys::MNumericArray.

A NumericArray can contain any type T which satisfies the trait NumericArrayType.

Use NumericArray::kind() to dynamically resolve a NumericArray with unknown element type into a NumericArray<T> with explicit element type.

Use UninitNumericArray to construct a NumericArray without requiring an intermediate allocation to copy the elements from.

Implementations§

Source§

impl NumericArray

Source

pub fn kind(&self) -> NumericArrayKind<'_>

Dynamically resolve a NumericArray of unknown element type into a NumericArray<T> with explicit element type.

§Example

Implement a function which returns the sum of an integral NumericArray

use wolfram_library_link::{NumericArray, NumericArrayKind};

fn sum(array: NumericArray) -> i64 {
    match array.kind() {
        NumericArrayKind::Bit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::Bit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::Bit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::Bit64(na) => na.as_slice().into_iter().sum(),
        NumericArrayKind::UBit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::UBit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::UBit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
        NumericArrayKind::UBit64(na) => {
            match i64::try_from(na.as_slice().into_iter().sum::<u64>()) {
                Ok(sum) => sum,
                Err(_) => panic!("overflows i64"),
            }
        },
        NumericArrayKind::Real32(_)
        | NumericArrayKind::Real64(_)
        | NumericArrayKind::ComplexReal64(_) => panic!("bad type"),
    }
}
Examples found in repository?
examples/numeric_arrays.rs (line 16)
14fn sum_int_numeric_array(na: &NumericArray) -> i64 {
15    #[rustfmt::skip]
16    let sum: i64 = match na.kind() {
17        NumericArrayKind::Bit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
18        NumericArrayKind::Bit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
19        NumericArrayKind::Bit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
20        NumericArrayKind::Bit64(na) => na.as_slice().into_iter().sum(),
21        NumericArrayKind::UBit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
22        NumericArrayKind::UBit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
23        NumericArrayKind::UBit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
24        NumericArrayKind::UBit64(na) => {
25            match i64::try_from(na.as_slice().into_iter().sum::<u64>()) {
26                Ok(sum) => sum,
27                Err(_) => panic!("NumericArray UnsignedInteger64 sum overflows i64"),
28            }
29        },
30
31        NumericArrayKind::Real32(_)
32        | NumericArrayKind::Real64(_)
33        | NumericArrayKind::ComplexReal64(_) => panic!(
34            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
35            na.data_type()
36        ),
37    };
38
39    sum
40}
41
42#[wll::export]
43fn sum_real_numeric_array(na: &NumericArray) -> f64 {
44    let sum: f64 = match na.kind() {
45        NumericArrayKind::Real32(na) => {
46            na.as_slice().into_iter().copied().map(f64::from).sum()
47        },
48        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
49        _ => panic!(
50            "sum_real_numeric_array cannot handle non-real data type: {:?}",
51            na.data_type()
52        ),
53    };
54
55    sum
56}
Source

pub fn try_kind<T>(&self) -> Result<&NumericArray<T>, ()>

Attempt to resolve this NumericArray into a &NumericArray<T> of the specified element type.

If the element type of this array does not match T, an error will be returned.

§Example

Implement a function which unwraps the &[u8] data in a NumericArray of 8-bit integers.

use wolfram_library_link::NumericArray;

fn bytes(array: &NumericArray) -> &[u8] {
    let byte_array: &NumericArray<u8> = match array.try_kind::<u8>() {
        Ok(array) => array,
        Err(_) => panic!("expected NumericArray of UnsignedInteger8")
    };

    byte_array.as_slice()
}
Source

pub fn try_into_kind<T>(self) -> Result<NumericArray<T>, NumericArray>

Attempt to resolve this NumericArray into a NumericArray<T> of the specified element type.

If the element type of this array does not match T, the original untyped array will be returned as the error value.

Source§

impl<T: NumericArrayType> NumericArray<T>

Source

pub fn from_slice(data: &[T]) -> NumericArray<T>

Construct a new one-dimensional NumericArray from a slice.

Use NumericArray::from_array() to construct multidimensional numeric arrays.

§Panics

This function will panic if NumericArray::try_from_array() returns an error.

§Example
let array = NumericArray::from_slice(&[1, 2, 3, 4, 5]);
§Alternatives

UninitNumericArray can be used to allocate a mutable numeric array, eliminating the need for an intermediate allocation.

Examples found in repository?
examples/tests/test_numeric_array_conversions.rs (line 6)
5fn from_slice<T: wll::NumericArrayType>(slice: &[T]) -> NumericArray<T> {
6    NumericArray::from_slice(slice)
7}
More examples
Hide additional examples
examples/tests/test_data_store.rs (line 76)
75fn test_named_numeric_array_data_store() -> DataStore {
76    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]).into_generic();
77
78    let mut data = DataStore::new();
79    data.add_named_numeric_array("array", array);
80
81    data
82}
examples/tests/test_share_counts.rs (line 67)
66fn test_na_clone() -> bool {
67    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);
68
69    assert!(array.share_count() == 0);
70
71    let clone = array.clone();
72
73    assert!(!array.ptr_eq(&clone));
74
75    assert!(array.share_count() == 0);
76    assert!(clone.share_count() == 0);
77
78    true
79}
Source

pub fn try_from_slice(data: &[T]) -> Result<NumericArray<T>, errcode_t>

Fallible alternative to NumericArray::from_slice().

Source

pub fn from_array(dimensions: &[usize], data: &[T]) -> NumericArray<T>

Construct a new multidimensional NumericArray from a list of dimensions and the flat slice of data.

§Panics

This function will panic if NumericArray::try_from_array() returns an error.

§Example

Construct the 2x2 NumericArray {{1, 2}, {3, 4}} from a list of dimensions and a flat buffer.

let array = NumericArray::from_array(&[2, 2], &[1, 2, 3, 4]);
Source

pub fn try_from_array( dimensions: &[usize], data: &[T], ) -> Result<NumericArray<T>, errcode_t>

Fallible alternative to NumericArray::from_array().

This function will return an error if:

  • dimensions is empty
  • the product of dimensions is 0
  • data.len() is not equal to the product of dimensions
Source

pub fn as_slice(&self) -> &[T]

Access the elements stored in this NumericArray as a flat buffer.

Examples found in repository?
examples/basic_types.rs (line 77)
76fn total_i64(list: &NumericArray<i64>) -> i64 {
77    list.as_slice().into_iter().sum()
78}
79
80//---------------
81// positive_i64()
82//---------------
83
84/// Get the sign of every element in `list` as a numeric array of 0's and 1's.
85///
86/// The returned array will have the same dimensions as `list`.
87#[wll::export]
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 108)
107fn total_i64(list: &NumericArray<i64>) -> i64 {
108    list.as_slice().into_iter().sum()
109}
110
111/// Get the sign of every element in `list` as a numeric array of 0's and 1's.
112///
113/// The returned array will have the same dimensions as `list`.
114#[wll::export]
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_numeric_array_conversions.rs (line 35)
10fn test_na_conversions() {
11    //
12    // i16 -> i8 conversions
13    //
14
15    assert!(from_slice(&[i16::MAX])
16        .convert_to::<i8>(Method::Check, 1.0)
17        .is_err());
18
19    assert!(from_slice(&[i16::MAX])
20        .convert_to::<i8>(Method::Cast, 1.0)
21        .is_err());
22
23    assert!(from_slice(&[i16::MAX])
24        .convert_to::<i8>(Method::Coerce, 1.0)
25        .is_err());
26
27    assert!(from_slice(&[i16::MAX])
28        .convert_to::<i8>(Method::Round, 1.0)
29        .is_err());
30
31    assert_eq!(
32        from_slice(&[i16::MAX])
33            .convert_to::<i8>(Method::Scale, 1.0)
34            .unwrap()
35            .as_slice(),
36        [i8::MAX]
37    );
38}
examples/numeric_arrays.rs (line 17)
14fn sum_int_numeric_array(na: &NumericArray) -> i64 {
15    #[rustfmt::skip]
16    let sum: i64 = match na.kind() {
17        NumericArrayKind::Bit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
18        NumericArrayKind::Bit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
19        NumericArrayKind::Bit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
20        NumericArrayKind::Bit64(na) => na.as_slice().into_iter().sum(),
21        NumericArrayKind::UBit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
22        NumericArrayKind::UBit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
23        NumericArrayKind::UBit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
24        NumericArrayKind::UBit64(na) => {
25            match i64::try_from(na.as_slice().into_iter().sum::<u64>()) {
26                Ok(sum) => sum,
27                Err(_) => panic!("NumericArray UnsignedInteger64 sum overflows i64"),
28            }
29        },
30
31        NumericArrayKind::Real32(_)
32        | NumericArrayKind::Real64(_)
33        | NumericArrayKind::ComplexReal64(_) => panic!(
34            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
35            na.data_type()
36        ),
37    };
38
39    sum
40}
41
42#[wll::export]
43fn sum_real_numeric_array(na: &NumericArray) -> f64 {
44    let sum: f64 = match na.kind() {
45        NumericArrayKind::Real32(na) => {
46            na.as_slice().into_iter().copied().map(f64::from).sum()
47        },
48        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
49        _ => panic!(
50            "sum_real_numeric_array cannot handle non-real data type: {:?}",
51            na.data_type()
52        ),
53    };
54
55    sum
56}
Source

pub fn as_slice_mut(&mut self) -> Option<&mut [T]>

Access the elements stored in this NumericArray as a mutable flat buffer.

If the share_count() of this array is >= 1, this function will return None.

Examples found in repository?
examples/tests/test_share_counts.rs (line 45)
38fn test_na_manual_are_not_ptr_eq(
39    mut array1: NumericArray<i64>,
40    array2: NumericArray<i64>,
41) -> DataStore {
42    let mut data = DataStore::new();
43    data.add_bool(array1.ptr_eq(&array2));
44    data.add_i64(array1.share_count() as i64);
45    data.add_bool(array1.as_slice_mut().is_some());
46    data
47}
48
49#[wll::export]
50fn test_na_shared_are_ptr_eq(
51    mut array1: NumericArray<i64>,
52    array2: NumericArray<i64>,
53) -> DataStore {
54    let mut data = DataStore::new();
55    data.add_bool(array1.ptr_eq(&array2));
56    data.add_i64(array1.share_count() as i64);
57    data.add_bool(array1.as_slice_mut().is_some());
58    data
59}
Source

pub unsafe fn as_slice_mut_unchecked(&mut self) -> &mut [T]

Access the elements stored in this NumericArray as a mutable flat buffer.

§Safety

NumericArray is an immutable shared data structure. There is no robust, easy way to determine whether mutation of a NumericArray is safe. Prefer to use UninitNumericArray to create and initialize a numeric array value instead of mutating an existing NumericArray.

Source§

impl<T> NumericArray<T>

Source

pub fn into_generic(self) -> NumericArray

Erase the concrete T data type associated with this NumericArray.

Use NumericArray::try_into_kind() to convert back into a NumericArray<T>.

§Example
let array: NumericArray<i64> = NumericArray::from_slice(&[1, 2, 3]);

let array: NumericArray = array.into_generic();
Examples found in repository?
examples/tests/test_data_store.rs (line 76)
75fn test_named_numeric_array_data_store() -> DataStore {
76    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]).into_generic();
77
78    let mut data = DataStore::new();
79    data.add_named_numeric_array("array", array);
80
81    data
82}
Source

pub unsafe fn from_raw(array: MNumericArray) -> NumericArray<T>

Construct a NumericArray<T> from a raw MNumericArray.

§Safety

The following conditions must be met for safe usage of this function:

  • array must be a fully initialized and valid numeric array object
  • T must either:
    • be (), representing an array with dynamic element type, or
    • T must satisfy NumericArrayType, and the element type of array must be the same as T.
Source

pub unsafe fn into_raw(self) -> MNumericArray

Convert this NumericArray into a raw MNumericArray object.

Source

pub fn data_ptr(&self) -> *mut c_void

LibraryLink C API Documentation: MNumericArray_getData

Source

pub fn data_type(&self) -> NumericArrayDataType

Examples found in repository?
examples/numeric_arrays.rs (line 35)
14fn sum_int_numeric_array(na: &NumericArray) -> i64 {
15    #[rustfmt::skip]
16    let sum: i64 = match na.kind() {
17        NumericArrayKind::Bit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
18        NumericArrayKind::Bit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
19        NumericArrayKind::Bit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
20        NumericArrayKind::Bit64(na) => na.as_slice().into_iter().sum(),
21        NumericArrayKind::UBit8(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
22        NumericArrayKind::UBit16(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
23        NumericArrayKind::UBit32(na) => na.as_slice().into_iter().copied().map(i64::from).sum(),
24        NumericArrayKind::UBit64(na) => {
25            match i64::try_from(na.as_slice().into_iter().sum::<u64>()) {
26                Ok(sum) => sum,
27                Err(_) => panic!("NumericArray UnsignedInteger64 sum overflows i64"),
28            }
29        },
30
31        NumericArrayKind::Real32(_)
32        | NumericArrayKind::Real64(_)
33        | NumericArrayKind::ComplexReal64(_) => panic!(
34            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
35            na.data_type()
36        ),
37    };
38
39    sum
40}
41
42#[wll::export]
43fn sum_real_numeric_array(na: &NumericArray) -> f64 {
44    let sum: f64 = match na.kind() {
45        NumericArrayKind::Real32(na) => {
46            na.as_slice().into_iter().copied().map(f64::from).sum()
47        },
48        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
49        _ => panic!(
50            "sum_real_numeric_array cannot handle non-real data type: {:?}",
51            na.data_type()
52        ),
53    };
54
55    sum
56}
Source

pub fn data_type_raw(&self) -> numericarray_data_t

LibraryLink C API Documentation: MNumericArray_getType

Source

pub fn flattened_length(&self) -> usize

The number of elements in the underlying flat data array.

This is the product of the dimension lengths of this NumericArray.

This is not the number of bytes.

LibraryLink C API Documentation: MNumericArray_getFlattenedLength

Source

pub fn rank(&self) -> usize

LibraryLink C API Documentation: MNumericArray_getRank

Source

pub fn dimensions(&self) -> &[usize]

Get the dimensions of this NumericArray.

LibraryLink C API Documentation: MNumericArray_getDimensions

§Example
let array = NumericArray::from_array(&[2, 2], &[1, 2, 3, 4]);

assert_eq!(array.dimensions(), &[2, 2]);
assert_eq!(array.rank(), array.dimensions().len());
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}
Source

pub fn share_count(&self) -> usize

Returns the share count of this NumericArray.

If this NumericArray is not shared, the share count is 0.

If this NumericArray was passed into the current library “by reference” due to use of the Automatic or "Constant" memory management strategy, that reference is not reflected in the share_count().

LibraryLink C API Documentation: MNumericArray_shareCount

Examples found in repository?
examples/tests/test_share_counts.rs (line 6)
5fn test_na_automatic_count(array: &NumericArray) -> i64 {
6    array.share_count() as i64
7}
8
9#[wll::export]
10fn test_na_constant_count(array: &NumericArray) -> i64 {
11    array.share_count() as i64
12}
13
14#[wll::export]
15fn test_na_manual_count(array: NumericArray) -> i64 {
16    array.share_count() as i64
17}
18
19#[wll::export]
20fn test_na_shared_count(array: NumericArray) -> i64 {
21    array.share_count() as i64
22}
23
24//
25
26#[wll::export]
27fn test_na_constant_are_ptr_eq(
28    array1: &NumericArray<i64>,
29    array2: &NumericArray<i64>,
30) -> DataStore {
31    let mut data = DataStore::new();
32    data.add_bool(array1.ptr_eq(&array2));
33    data.add_i64(array1.share_count() as i64);
34    data
35}
36
37#[wll::export]
38fn test_na_manual_are_not_ptr_eq(
39    mut array1: NumericArray<i64>,
40    array2: NumericArray<i64>,
41) -> DataStore {
42    let mut data = DataStore::new();
43    data.add_bool(array1.ptr_eq(&array2));
44    data.add_i64(array1.share_count() as i64);
45    data.add_bool(array1.as_slice_mut().is_some());
46    data
47}
48
49#[wll::export]
50fn test_na_shared_are_ptr_eq(
51    mut array1: NumericArray<i64>,
52    array2: NumericArray<i64>,
53) -> DataStore {
54    let mut data = DataStore::new();
55    data.add_bool(array1.ptr_eq(&array2));
56    data.add_i64(array1.share_count() as i64);
57    data.add_bool(array1.as_slice_mut().is_some());
58    data
59}
60
61//----------------------------
62// Test cloning NumericArray's
63//----------------------------
64
65#[wll::export]
66fn test_na_clone() -> bool {
67    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);
68
69    assert!(array.share_count() == 0);
70
71    let clone = array.clone();
72
73    assert!(!array.ptr_eq(&clone));
74
75    assert!(array.share_count() == 0);
76    assert!(clone.share_count() == 0);
77
78    true
79}
80
81#[wll::export]
82fn test_na_shared_clone(array: NumericArray<i64>) -> bool {
83    assert!(array.share_count() == 1);
84
85    let clone = array.clone();
86
87    assert!(!array.ptr_eq(&clone));
88
89    assert!(array.share_count() == 1);
90    assert!(clone.share_count() == 0);
91
92    true
93}
Source

pub fn ptr_eq<T2>(&self, other: &NumericArray<T2>) -> bool

Returns true if self and other are pointers to the name underlying numeric array object.

Examples found in repository?
examples/tests/test_share_counts.rs (line 32)
27fn test_na_constant_are_ptr_eq(
28    array1: &NumericArray<i64>,
29    array2: &NumericArray<i64>,
30) -> DataStore {
31    let mut data = DataStore::new();
32    data.add_bool(array1.ptr_eq(&array2));
33    data.add_i64(array1.share_count() as i64);
34    data
35}
36
37#[wll::export]
38fn test_na_manual_are_not_ptr_eq(
39    mut array1: NumericArray<i64>,
40    array2: NumericArray<i64>,
41) -> DataStore {
42    let mut data = DataStore::new();
43    data.add_bool(array1.ptr_eq(&array2));
44    data.add_i64(array1.share_count() as i64);
45    data.add_bool(array1.as_slice_mut().is_some());
46    data
47}
48
49#[wll::export]
50fn test_na_shared_are_ptr_eq(
51    mut array1: NumericArray<i64>,
52    array2: NumericArray<i64>,
53) -> DataStore {
54    let mut data = DataStore::new();
55    data.add_bool(array1.ptr_eq(&array2));
56    data.add_i64(array1.share_count() as i64);
57    data.add_bool(array1.as_slice_mut().is_some());
58    data
59}
60
61//----------------------------
62// Test cloning NumericArray's
63//----------------------------
64
65#[wll::export]
66fn test_na_clone() -> bool {
67    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);
68
69    assert!(array.share_count() == 0);
70
71    let clone = array.clone();
72
73    assert!(!array.ptr_eq(&clone));
74
75    assert!(array.share_count() == 0);
76    assert!(clone.share_count() == 0);
77
78    true
79}
80
81#[wll::export]
82fn test_na_shared_clone(array: NumericArray<i64>) -> bool {
83    assert!(array.share_count() == 1);
84
85    let clone = array.clone();
86
87    assert!(!array.ptr_eq(&clone));
88
89    assert!(array.share_count() == 1);
90    assert!(clone.share_count() == 0);
91
92    true
93}
Source

pub fn convert_to<T2: NumericArrayType>( &self, method: NumericArrayConvertMethod, tolerance: mreal, ) -> Result<NumericArray<T2>, errcode_t>

LibraryLink C API Documentation: MNumericArray_convertType

Examples found in repository?
examples/tests/test_numeric_array_conversions.rs (line 16)
10fn test_na_conversions() {
11    //
12    // i16 -> i8 conversions
13    //
14
15    assert!(from_slice(&[i16::MAX])
16        .convert_to::<i8>(Method::Check, 1.0)
17        .is_err());
18
19    assert!(from_slice(&[i16::MAX])
20        .convert_to::<i8>(Method::Cast, 1.0)
21        .is_err());
22
23    assert!(from_slice(&[i16::MAX])
24        .convert_to::<i8>(Method::Coerce, 1.0)
25        .is_err());
26
27    assert!(from_slice(&[i16::MAX])
28        .convert_to::<i8>(Method::Round, 1.0)
29        .is_err());
30
31    assert_eq!(
32        from_slice(&[i16::MAX])
33            .convert_to::<i8>(Method::Scale, 1.0)
34            .unwrap()
35            .as_slice(),
36        [i8::MAX]
37    );
38}

Trait Implementations§

Source§

impl<T> Clone for NumericArray<T>

Source§

fn clone(&self) -> NumericArray<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for NumericArray<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for NumericArray<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> FromArg<'a> for &'a NumericArray<()>

Source§

unsafe fn from_arg(arg: &'a MArgument) -> &'a NumericArray<()>

Source§

fn parameter_type() -> Expr

Return the LibraryLink parameter type as a Wolfram Language expression. Read more
Source§

impl<'a, T: NumericArrayType> FromArg<'a> for &'a NumericArray<T>

§Safety

FromArg for NumericArray<T> MUST be constrained by T: NumericArrayType to prevent accidental creation of invalid NumericArray conversions. Without this constraint, it would be possible to write code like:

#[export] // Unsafe!
fn and(bools: NumericArray<bool>) -> bool {
    // ...
}

which is not valid because bool is not a valid numeric array type.

Source§

unsafe fn from_arg(arg: &'a MArgument) -> &'a NumericArray<T>

Source§

fn parameter_type() -> Expr

Return the LibraryLink parameter type as a Wolfram Language expression. Read more
Source§

impl<'a> FromArg<'a> for NumericArray<()>

Source§

unsafe fn from_arg(arg: &'a MArgument) -> NumericArray<()>

Source§

fn parameter_type() -> Expr

Return the LibraryLink parameter type as a Wolfram Language expression. Read more
Source§

impl<'a, T: NumericArrayType> FromArg<'a> for NumericArray<T>

Source§

unsafe fn from_arg(arg: &'a MArgument) -> NumericArray<T>

Source§

fn parameter_type() -> Expr

Return the LibraryLink parameter type as a Wolfram Language expression. Read more
Source§

impl IntoArg for NumericArray<()>

Source§

unsafe fn into_arg(self, arg: MArgument)

Move self into arg. Read more
Source§

fn return_type() -> Expr

Return the LibraryLink return type as a Wolfram Language expression. Read more
Source§

impl<T: NumericArrayType> IntoArg for NumericArray<T>

Source§

unsafe fn into_arg(self, arg: MArgument)

Move self into arg. Read more
Source§

fn return_type() -> Expr

Return the LibraryLink return type as a Wolfram Language expression. Read more
Source§

impl<T> RefCast for NumericArray<T>

Source§

type From = *mut st_MNumericArray

Source§

fn ref_cast(_from: &Self::From) -> &Self

Source§

fn ref_cast_mut(_from: &mut Self::From) -> &mut Self

Auto Trait Implementations§

§

impl<T> Freeze for NumericArray<T>

§

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

§

impl<T = ()> !Send for NumericArray<T>

§

impl<T = ()> !Sync for NumericArray<T>

§

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

§

impl<T> UnwindSafe for NumericArray<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.