example/
lib.rs

1//!An example dynamically loadable library.
2//!
3//! This crate creates a dynamic library that can be used for testing purposes.
4//! It exports multiple symbols with different types and abis.
5//! It's main purpose is to be used in tests of dynlib crate.
6
7#![allow(non_upper_case_globals)]
8extern crate libc;
9use libc::{c_int, c_char};
10
11
12//FUNCTIONS
13#[no_mangle]
14pub fn rust_fun_print_something() {
15    println!("something");
16}
17
18#[no_mangle]
19pub fn rust_fun_add_one(arg: i32) -> i32 {
20    arg + 1
21}
22
23#[no_mangle]
24pub extern "C" fn c_fun_print_something_else() {
25    println!("something else");
26}
27
28#[no_mangle]
29pub extern "C" fn c_fun_add_two(arg: c_int) -> c_int {
30    arg + 2
31}
32
33#[allow(unused_variables)]
34#[no_mangle]
35pub extern "C" fn c_fun_variadic(txt: * const c_char) {
36    //pretend to be variadic - impossible to do in Rust code
37}
38
39//STATIC DATA
40#[no_mangle]
41pub static mut rust_i32_mut: i32 = 42;
42#[no_mangle]
43pub static rust_i32: i32 = 43;
44
45#[no_mangle]
46pub static mut c_int_mut: c_int = 44;
47#[no_mangle]
48pub static c_int: c_int = 45;
49
50#[repr(C)]
51pub struct SomeData {
52    first: c_int,
53    second: c_int
54}
55
56#[no_mangle]
57pub static c_struct: SomeData = SomeData{first: 1, second: 2};
58
59//STATIC STRINGS
60
61//exporting str directly is not so easy - it is not Sized!
62//you can only export a reference to str and this requires double dereference
63#[no_mangle]
64pub static rust_str: &str = "Hello!";
65
66#[no_mangle]
67pub static c_const_char_ptr: [u8; 4] = [b'H', b'i', b'!', 0];
68
69
70
71
72
73