#[repr(transparent)]
pub struct NumericArray<T = ()>(_, _);
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)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn sum_int_numeric_array(na: &NumericArray) -> i64 {
    #[rustfmt::skip]
    let sum: i64 = match na.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!("NumericArray UnsignedInteger64 sum overflows i64"),
            }
        },

        NumericArrayKind::Real32(_)
        | NumericArrayKind::Real64(_)
        | NumericArrayKind::ComplexReal64(_) => panic!(
            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
            na.data_type()
        ),
    };

    sum
}

#[wll::export]
fn sum_real_numeric_array(na: &NumericArray) -> f64 {
    let sum: f64 = match na.kind() {
        NumericArrayKind::Real32(na) => {
            na.as_slice().into_iter().copied().map(f64::from).sum()
        },
        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
        _ => panic!(
            "sum_real_numeric_array cannot handle non-real data type: {:?}",
            na.data_type()
        ),
    };

    sum
}
source

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

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>where T: NumericArrayType,

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)
5
6
7
fn from_slice<T: wll::NumericArrayType>(slice: &[T]) -> NumericArray<T> {
    NumericArray::from_slice(slice)
}
More examples
Hide additional examples
examples/tests/test_data_store.rs (line 76)
75
76
77
78
79
80
81
82
fn test_named_numeric_array_data_store() -> DataStore {
    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]).into_generic();

    let mut data = DataStore::new();
    data.add_named_numeric_array("array", array);

    data
}
examples/tests/test_share_counts.rs (line 67)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
fn test_na_clone() -> bool {
    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);

    assert!(array.share_count() == 0);

    let clone = array.clone();

    assert!(!array.ptr_eq(&clone));

    assert!(array.share_count() == 0);
    assert!(clone.share_count() == 0);

    true
}
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)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
fn total_i64(list: &NumericArray<i64>) -> i64 {
    list.as_slice().into_iter().sum()
}

//---------------
// positive_i64()
//---------------

/// Get the sign of every element in `list` as a numeric array of 0's and 1's.
///
/// The returned array will have the same dimensions as `list`.
#[wll::export]
fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
    let mut bools: UninitNumericArray<u8> =
        UninitNumericArray::from_dimensions(list.dimensions());

    for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
        let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;

        entry.write(u8::from(elem.is_positive()));
    }

    unsafe { bools.assume_init() }
}
More examples
Hide additional examples
examples/tests/test_native_args.rs (line 108)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
fn total_i64(list: &NumericArray<i64>) -> i64 {
    list.as_slice().into_iter().sum()
}

/// Get the sign of every element in `list` as a numeric array of 0's and 1's.
///
/// The returned array will have the same dimensions as `list`.
#[wll::export]
fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
    let mut bools: UninitNumericArray<u8> =
        UninitNumericArray::from_dimensions(list.dimensions());

    for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
        let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;

        entry.write(u8::from(elem.is_positive()));
    }

    unsafe { bools.assume_init() }
}
examples/tests/test_numeric_array_conversions.rs (line 35)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn test_na_conversions() {
    //
    // i16 -> i8 conversions
    //

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Check, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Cast, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Coerce, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Round, 1.0)
        .is_err());

    assert_eq!(
        from_slice(&[i16::MAX])
            .convert_to::<i8>(Method::Scale, 1.0)
            .unwrap()
            .as_slice(),
        [i8::MAX]
    );
}
examples/numeric_arrays.rs (line 17)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn sum_int_numeric_array(na: &NumericArray) -> i64 {
    #[rustfmt::skip]
    let sum: i64 = match na.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!("NumericArray UnsignedInteger64 sum overflows i64"),
            }
        },

        NumericArrayKind::Real32(_)
        | NumericArrayKind::Real64(_)
        | NumericArrayKind::ComplexReal64(_) => panic!(
            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
            na.data_type()
        ),
    };

    sum
}

#[wll::export]
fn sum_real_numeric_array(na: &NumericArray) -> f64 {
    let sum: f64 = match na.kind() {
        NumericArrayKind::Real32(na) => {
            na.as_slice().into_iter().copied().map(f64::from).sum()
        },
        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
        _ => panic!(
            "sum_real_numeric_array cannot handle non-real data type: {:?}",
            na.data_type()
        ),
    };

    sum
}
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)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn test_na_manual_are_not_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}

#[wll::export]
fn test_na_shared_are_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}
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)
75
76
77
78
79
80
81
82
fn test_named_numeric_array_data_store() -> DataStore {
    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]).into_generic();

    let mut data = DataStore::new();
    data.add_named_numeric_array("array", array);

    data
}
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)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn sum_int_numeric_array(na: &NumericArray) -> i64 {
    #[rustfmt::skip]
    let sum: i64 = match na.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!("NumericArray UnsignedInteger64 sum overflows i64"),
            }
        },

        NumericArrayKind::Real32(_)
        | NumericArrayKind::Real64(_)
        | NumericArrayKind::ComplexReal64(_) => panic!(
            "sum_int_numeric_array cannot handle non-integer data type: {:?}",
            na.data_type()
        ),
    };

    sum
}

#[wll::export]
fn sum_real_numeric_array(na: &NumericArray) -> f64 {
    let sum: f64 = match na.kind() {
        NumericArrayKind::Real32(na) => {
            na.as_slice().into_iter().copied().map(f64::from).sum()
        },
        NumericArrayKind::Real64(na) => na.as_slice().into_iter().copied().sum(),
        _ => panic!(
            "sum_real_numeric_array cannot handle non-real data type: {:?}",
            na.data_type()
        ),
    };

    sum
}
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)
88
89
90
91
92
93
94
95
96
97
98
99
fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
    let mut bools: UninitNumericArray<u8> =
        UninitNumericArray::from_dimensions(list.dimensions());

    for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
        let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;

        entry.write(u8::from(elem.is_positive()));
    }

    unsafe { bools.assume_init() }
}
More examples
Hide additional examples
examples/tests/test_native_args.rs (line 117)
115
116
117
118
119
120
121
122
123
124
125
126
fn positive_i64(list: &NumericArray<i64>) -> NumericArray<u8> {
    let mut bools: UninitNumericArray<u8> =
        UninitNumericArray::from_dimensions(list.dimensions());

    for pair in list.as_slice().into_iter().zip(bools.as_slice_mut()) {
        let (elem, entry): (&i64, &mut std::mem::MaybeUninit<u8>) = pair;

        entry.write(u8::from(elem.is_positive()));
    }

    unsafe { bools.assume_init() }
}
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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
fn test_na_automatic_count(array: &NumericArray) -> i64 {
    array.share_count() as i64
}

#[wll::export]
fn test_na_constant_count(array: &NumericArray) -> i64 {
    array.share_count() as i64
}

#[wll::export]
fn test_na_manual_count(array: NumericArray) -> i64 {
    array.share_count() as i64
}

#[wll::export]
fn test_na_shared_count(array: NumericArray) -> i64 {
    array.share_count() as i64
}

//

#[wll::export]
fn test_na_constant_are_ptr_eq(
    array1: &NumericArray<i64>,
    array2: &NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data
}

#[wll::export]
fn test_na_manual_are_not_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}

#[wll::export]
fn test_na_shared_are_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}

//----------------------------
// Test cloning NumericArray's
//----------------------------

#[wll::export]
fn test_na_clone() -> bool {
    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);

    assert!(array.share_count() == 0);

    let clone = array.clone();

    assert!(!array.ptr_eq(&clone));

    assert!(array.share_count() == 0);
    assert!(clone.share_count() == 0);

    true
}

#[wll::export]
fn test_na_shared_clone(array: NumericArray<i64>) -> bool {
    assert!(array.share_count() == 1);

    let clone = array.clone();

    assert!(!array.ptr_eq(&clone));

    assert!(array.share_count() == 1);
    assert!(clone.share_count() == 0);

    true
}
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)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
fn test_na_constant_are_ptr_eq(
    array1: &NumericArray<i64>,
    array2: &NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data
}

#[wll::export]
fn test_na_manual_are_not_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}

#[wll::export]
fn test_na_shared_are_ptr_eq(
    mut array1: NumericArray<i64>,
    array2: NumericArray<i64>,
) -> DataStore {
    let mut data = DataStore::new();
    data.add_bool(array1.ptr_eq(&array2));
    data.add_i64(array1.share_count() as i64);
    data.add_bool(array1.as_slice_mut().is_some());
    data
}

//----------------------------
// Test cloning NumericArray's
//----------------------------

#[wll::export]
fn test_na_clone() -> bool {
    let array = NumericArray::<i64>::from_slice(&[1, 2, 3]);

    assert!(array.share_count() == 0);

    let clone = array.clone();

    assert!(!array.ptr_eq(&clone));

    assert!(array.share_count() == 0);
    assert!(clone.share_count() == 0);

    true
}

#[wll::export]
fn test_na_shared_clone(array: NumericArray<i64>) -> bool {
    assert!(array.share_count() == 1);

    let clone = array.clone();

    assert!(!array.ptr_eq(&clone));

    assert!(array.share_count() == 1);
    assert!(clone.share_count() == 0);

    true
}
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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn test_na_conversions() {
    //
    // i16 -> i8 conversions
    //

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Check, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Cast, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Coerce, 1.0)
        .is_err());

    assert!(from_slice(&[i16::MAX])
        .convert_to::<i8>(Method::Round, 1.0)
        .is_err());

    assert_eq!(
        from_slice(&[i16::MAX])
            .convert_to::<i8>(Method::Scale, 1.0)
            .unwrap()
            .as_slice(),
        [i8::MAX]
    );
}

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>

§

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> 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.