-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
134 lines (122 loc) · 3.29 KB
/
lib.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
//! `embed-c` is a crate that allows you to embed C code inside Rust code files. The C code is
//! translated into Rust code at compile time using [C2Rust](https://github.com/immunant/c2rust),
//! which means that it is fully interoperable with Rust. C code can call Rust code, and vice-versa.
//!
//! ### Basic usage
//! ```rust
//! use embed_c::embed_c;
//!
//! embed_c! {
//! int add(int x, int y) {
//! return x + y;
//! }
//! }
//!
//! fn main() {
//! let x = unsafe { add(1, 2) };
//! println!("{}", x);
//! }
//! ```
//!
//! See more examples in [src/lib.rs](src/lib.rs)
//!
//! ### Limitations
//! Many
//!
//! ### Motivation
//! N/A
//!
//! ### License
//!
//! This project is licensed under either of
//!
//! * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
//! https://www.apache.org/licenses/LICENSE-2.0)
//! * MIT license ([LICENSE-MIT](LICENSE-MIT) or
//! https://opensource.org/licenses/MIT)
//!
//! at your option.
#![feature(matches_macro)]
pub use embed_c_macros::embed_c;
#[cfg(test)]
mod tests
{
use embed_c_macros::embed_c;
embed_c!
{
int add(int x, int y)
{
return x + y;
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Quick sort implementation
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int i = partition(arr, low, high);
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
}
// Duff's device
// Safely copies an array into another
void send(to, from, count)
register short *to, *from;
register count;
{
register n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}
}
#[test]
fn test_c_macro()
{
unsafe { assert_eq!(add(1, 2), 3); }
}
#[test]
fn test_quicksort()
{
let mut arr = [1, 5, 3, 2, 4];
unsafe { quickSort(arr.as_mut_ptr(), 0, 4); };
assert_eq!(arr, [1, 2, 3, 4, 5]);
}
#[test]
fn test_duff()
{
let mut source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let mut dest = [0; 10];
unsafe { send(dest.as_mut_ptr(), source.as_mut_ptr(), 10); };
assert_eq!(source, dest);
}
}