Struct wolfram_library_link::NumericArray
source · #[repr(transparent)]pub struct NumericArray<T = ()>(_, _);
Expand description
Native Wolfram NumericArray
WL.
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
impl NumericArray
sourcepub fn kind(&self) -> NumericArrayKind<'_>
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?
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
}
sourcepub fn try_kind<T>(&self) -> Result<&NumericArray<T>, ()>where
T: NumericArrayType,
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()
}
sourcepub fn try_into_kind<T>(self) -> Result<NumericArray<T>, NumericArray>where
T: NumericArrayType,
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>
impl<T: NumericArrayType> NumericArray<T>
sourcepub fn from_slice(data: &[T]) -> NumericArray<T>
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?
More examples
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
}
sourcepub fn try_from_slice(data: &[T]) -> Result<NumericArray<T>, errcode_t>
pub fn try_from_slice(data: &[T]) -> Result<NumericArray<T>, errcode_t>
Fallible alternative to NumericArray::from_slice()
.
sourcepub fn from_array(dimensions: &[usize], data: &[T]) -> NumericArray<T>
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]);
sourcepub fn try_from_array(
dimensions: &[usize],
data: &[T]
) -> Result<NumericArray<T>, errcode_t>
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 ofdimensions
sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Access the elements stored in this NumericArray
as a flat buffer.
Examples found in repository?
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
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() }
}
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]
);
}
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
}
sourcepub fn as_slice_mut(&mut self) -> Option<&mut [T]>
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?
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
}
sourcepub unsafe fn as_slice_mut_unchecked(&mut self) -> &mut [T]
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>
impl<T> NumericArray<T>
sourcepub fn into_generic(self) -> NumericArray
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();
sourcepub unsafe fn from_raw(array: MNumericArray) -> NumericArray<T>
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 objectT
must either:- be
()
, representing an array with dynamic element type, or T
must satisfyNumericArrayType
, and the element type ofarray
must be the same asT
.
- be
sourcepub unsafe fn into_raw(self) -> MNumericArray
pub unsafe fn into_raw(self) -> MNumericArray
Convert this NumericArray
into a raw MNumericArray
object.
sourcepub fn data_ptr(&self) -> *mut c_void
pub fn data_ptr(&self) -> *mut c_void
LibraryLink C API Documentation: MNumericArray_getData
sourcepub fn data_type(&self) -> NumericArrayDataType
pub fn data_type(&self) -> NumericArrayDataType
Examples found in repository?
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
}
sourcepub fn data_type_raw(&self) -> numericarray_data_t
pub fn data_type_raw(&self) -> numericarray_data_t
LibraryLink C API Documentation: MNumericArray_getType
sourcepub fn flattened_length(&self) -> usize
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
sourcepub fn rank(&self) -> usize
pub fn rank(&self) -> usize
LibraryLink C API Documentation: MNumericArray_getRank
sourcepub fn dimensions(&self) -> &[usize]
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?
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
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() }
}
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?
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
}
sourcepub fn ptr_eq<T2>(&self, other: &NumericArray<T2>) -> bool
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?
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
}
sourcepub fn convert_to<T2: NumericArrayType>(
&self,
method: NumericArrayConvertMethod,
tolerance: mreal
) -> Result<NumericArray<T2>, errcode_t>
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?
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>
impl<T> Clone for NumericArray<T>
source§fn clone(&self) -> NumericArray<T>
fn clone(&self) -> NumericArray<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T> Debug for NumericArray<T>
impl<T> Debug for NumericArray<T>
source§impl<T> Drop for NumericArray<T>
impl<T> Drop for NumericArray<T>
source§impl<'a> FromArg<'a> for &'a NumericArray<()>
impl<'a> FromArg<'a> for &'a NumericArray<()>
source§impl<'a, T: NumericArrayType> FromArg<'a> for &'a NumericArray<T>
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.