-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic_types.rs
139 lines (115 loc) · 3.14 KB
/
basic_types.rs
1
2
3
4
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! This example demonstrates how LibraryLink native data types can be used in Rust
//! functions called via LibraryLink.
use wolfram_library_link::{self as wll, NumericArray, UninitNumericArray};
wll::generate_loader!(load_basic_types_functions);
//======================================
// Primitive data types
//======================================
//---------
// square()
//---------
/// Define a function to square a number.
///
/// The exported LibraryLink function may be loaded and used by evaluating:
///
/// ```wolfram
/// square = LibraryFunctionLoad["libbasic_types", "square", {Integer}, Integer];
/// square[2]
/// ```
//
// Export the `square` function via LibraryLink. This will generate a "wrapper" function
// that correctly implements the lightweight LibraryLink <=> Rust conversion.
#[wll::export]
fn square(x: i64) -> i64 {
x * x
}
//-----------------
// reverse_string()
//-----------------
#[wll::export]
fn reverse_string(string: String) -> String {
string.chars().rev().collect()
}
//------------------
// add2() and add3()
//------------------
#[wll::export]
fn add2(x: i64, y: i64) -> i64 {
x + y
}
#[wll::export]
fn add3(x: i64, y: i64, z: i64) -> i64 {
x + y + z
}
//======================================
// NumericArray's
//======================================
//------------
// total_i64()
//------------
// Load and use by evaluating:
//
// ```wolfram
// total = LibraryFunctionLoad[
// "libbasic_types",
// "total_i64",
// {LibraryDataType[NumericArray, "Integer64"]},
// Integer
// ];
//
// total[NumericArray[Range[100], "Integer64"]]
// ```
#[wll::export]
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() }
}
//======================================
// get_random_number()
//======================================
// Load and use by evaluating:
//
// ```wolfram
// randomNumber = LibraryFunctionLoad[
// "libbasic_types",
// "xkcd_get_random_number",
// {},
// Integer
// ];
// randomNumber[]
// ```
#[wll::export(name = "xkcd_get_random_number")]
fn get_random_number() -> i64 {
// chosen by fair dice roll.
// guaranteed to be random.
// xkcd.com/221
4
}
//======================================
// raw_square()
//======================================
#[wll::export]
fn raw_square(args: &[wll::sys::MArgument], ret: wll::sys::MArgument) {
if args.len() != 1 {
panic!("unexpected number of arguments");
}
let x: i64 = unsafe { *args[0].integer };
unsafe {
*ret.integer = x * x;
}
}