summaryrefslogtreecommitdiffstats
path: root/third_party/rust/oneshot-uniffi/tests/raw.rs
blob: e38dc45c481c5843af8543d962e4a9f5396ce41f (plain)
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
#![cfg(not(loom))]

use oneshot::{channel, Receiver, Sender};

#[test]
fn test_raw_sender() {
    let (sender, receiver) = channel::<u32>();
    let raw = sender.into_raw();
    let recreated = unsafe { Sender::<u32>::from_raw(raw) };
    recreated
        .send(100)
        .unwrap_or_else(|e| panic!("error sending after into_raw/from_raw roundtrip: {e}"));
    assert_eq!(receiver.try_recv(), Ok(100))
}

#[test]
fn test_raw_receiver() {
    let (sender, receiver) = channel::<u32>();
    let raw = receiver.into_raw();
    sender.send(100).unwrap();
    let recreated = unsafe { Receiver::<u32>::from_raw(raw) };
    assert_eq!(
        recreated
            .try_recv()
            .unwrap_or_else(|e| panic!("error receiving after into_raw/from_raw roundtrip: {e}")),
        100
    )
}

#[test]
fn test_raw_sender_and_receiver() {
    let (sender, receiver) = channel::<u32>();
    let raw_receiver = receiver.into_raw();
    let raw_sender = sender.into_raw();

    let recreated_sender = unsafe { Sender::<u32>::from_raw(raw_sender) };
    recreated_sender.send(100).unwrap();

    let recreated_receiver = unsafe { Receiver::<u32>::from_raw(raw_receiver) };
    assert_eq!(
        recreated_receiver
            .try_recv()
            .unwrap_or_else(|e| panic!("error receiving after into_raw/from_raw roundtrip: {e}")),
        100
    )
}