pub struct NumericArray<T = ()>(/* private fields */);
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?
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}
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
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}
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?
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
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}
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}
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}
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?
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}
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?
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}
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?
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}
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?
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}
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?
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}
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?
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>
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>
§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:
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.