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
use std;

pub struct Mpsc {
    tx: std::sync::mpsc::Sender<*mut UnknownData>,
    rx: std::sync::mpsc::Receiver<*mut UnknownData>
}

#[derive(Clone)]
pub struct Sender {
    tx: std::sync::mpsc::Sender<*mut UnknownData>
}

pub struct UnknownData {}

impl Mpsc {
    pub fn new() -> Mpsc {
        let (tx, rx) = std::sync::mpsc::channel();
        Mpsc {
            tx: tx,
            rx: rx
        }
    }
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_create() -> *mut Mpsc {
    Box::into_raw(Box::new(Mpsc::new()))
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_destroy(mpsc: *mut Mpsc) {
    Box::from_raw(mpsc);
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_create_sender(mpsc: *mut Mpsc) -> *mut Sender {
    let mpsc = &*mpsc;
    Box::into_raw(Box::new(Sender {
        tx: mpsc.tx.clone()
    }))
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_destroy_sender(sender: *mut Sender) {
    Box::from_raw(sender);
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_sender_clone(sender: *mut Sender) -> *mut Sender {
    let sender = &*sender;
    Box::into_raw(Box::new(sender.clone()))
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_sender_write(sender: *mut Sender, data: *mut UnknownData) {
    let sender = &*sender;
    sender.tx.send(data).unwrap();
}

#[no_mangle]
pub unsafe fn ice_ext_mpsc_read(mpsc: *mut Mpsc) -> *mut UnknownData {
    let mpsc = &*mpsc;
    mpsc.rx.recv().unwrap()
}