lib.rs

 1use plugin::prelude::*;
 2
 3#[export]
 4pub fn noop() {}
 5
 6#[export]
 7pub fn constant() -> u32 {
 8    27
 9}
10
11#[export]
12pub fn identity(i: u32) -> u32 {
13    i
14}
15
16#[export]
17pub fn add(a: u32, b: u32) -> u32 {
18    a + b
19}
20
21#[export]
22pub fn swap(a: u32, b: u32) -> (u32, u32) {
23    (b, a)
24}
25
26#[export]
27pub fn sort(mut list: Vec<u32>) -> Vec<u32> {
28    list.sort();
29    list
30}
31
32#[export]
33pub fn print(string: String) {
34    println!("to stdout: {}", string);
35    eprintln!("to stderr: {}", string);
36}
37
38#[import]
39fn mystery_number(input: u32) -> u32;
40
41#[export]
42pub fn and_back(secret: u32) -> u32 {
43    mystery_number(secret)
44}
45
46#[import]
47fn import_noop() -> ();
48
49#[import]
50fn import_identity(i: u32) -> u32;
51
52#[import]
53fn import_swap(a: u32, b: u32) -> (u32, u32);
54
55#[export]
56pub fn imports(x: u32) -> u32 {
57    let a = import_identity(7);
58    import_noop();
59    let (b, c) = import_swap(a, x);
60    assert_eq!(a, c);
61    assert_eq!(x, b);
62    a + b // should be 7 + x
63}
64
65#[import]
66fn import_half(a: u32) -> u32;
67
68#[export]
69pub fn half_async(a: u32) -> u32 {
70    import_half(a)
71}
72
73#[import]
74fn command_async(command: String) -> Option<Vec<u8>>;
75
76#[export]
77pub fn echo_async(message: String) -> String {
78    let command = format!("echo {}", message);
79    let result = command_async(command);
80    let result = result.expect("Could not run command");
81    String::from_utf8_lossy(&result).to_string()
82}