diff options
Diffstat (limited to 'third_party/rust/oneshot-uniffi/examples')
10 files changed, 145 insertions, 0 deletions
diff --git a/third_party/rust/oneshot-uniffi/examples/recv_before_send.rs b/third_party/rust/oneshot-uniffi/examples/recv_before_send.rs new file mode 100644 index 0000000000..2eda3dd610 --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_before_send.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + sender.send(9u128).unwrap(); + }); + assert_eq!(receiver.recv(), Ok(9)); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_before_send_then_drop_sender.rs b/third_party/rust/oneshot-uniffi/examples/recv_before_send_then_drop_sender.rs new file mode 100644 index 0000000000..aea7d66b90 --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_before_send_then_drop_sender.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel::<u128>(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + std::mem::drop(sender); + }); + assert!(receiver.recv().is_err()); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send.rs b/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send.rs new file mode 100644 index 0000000000..6ed74ddfca --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + sender.send(9u128).unwrap(); + }); + assert_eq!(receiver.recv_ref(), Ok(9)); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send_then_drop_sender.rs b/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send_then_drop_sender.rs new file mode 100644 index 0000000000..75ff3d6006 --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_ref_before_send_then_drop_sender.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel::<u128>(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + std::mem::drop(sender); + }); + assert!(receiver.recv_ref().is_err()); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send.rs b/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send.rs new file mode 100644 index 0000000000..85a2ac88be --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + sender.send(9u128).unwrap(); + }); + assert_eq!(receiver.recv_timeout(Duration::from_millis(100)), Ok(9)); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send_then_drop_sender.rs b/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send_then_drop_sender.rs new file mode 100644 index 0000000000..32c31fcd9c --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_timeout_before_send_then_drop_sender.rs @@ -0,0 +1,18 @@ +#[cfg(feature = "std")] +fn main() { + use std::thread; + use std::time::Duration; + + let (sender, receiver) = oneshot::channel::<u128>(); + let t = thread::spawn(move || { + thread::sleep(Duration::from_millis(2)); + std::mem::drop(sender); + }); + assert!(receiver.recv_timeout(Duration::from_millis(100)).is_err()); + t.join().unwrap(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/recv_with_dropped_sender.rs b/third_party/rust/oneshot-uniffi/examples/recv_with_dropped_sender.rs new file mode 100644 index 0000000000..f7a7171e1b --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/recv_with_dropped_sender.rs @@ -0,0 +1,11 @@ +#[cfg(feature = "std")] +fn main() { + let (sender, receiver) = oneshot::channel::<u128>(); + std::mem::drop(sender); + receiver.recv().unwrap_err(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/send_before_recv.rs b/third_party/rust/oneshot-uniffi/examples/send_before_recv.rs new file mode 100644 index 0000000000..c31ba658d3 --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/send_before_recv.rs @@ -0,0 +1,11 @@ +#[cfg(feature = "std")] +fn main() { + let (sender, receiver) = oneshot::channel(); + assert!(sender.send(19i128).is_ok()); + assert_eq!(receiver.recv(), Ok(19i128)); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("This example is only for when the \"sync\" feature is used"); +} diff --git a/third_party/rust/oneshot-uniffi/examples/send_then_drop_receiver.rs b/third_party/rust/oneshot-uniffi/examples/send_then_drop_receiver.rs new file mode 100644 index 0000000000..941c508d5b --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/send_then_drop_receiver.rs @@ -0,0 +1,7 @@ +use std::mem; + +fn main() { + let (sender, receiver) = oneshot::channel(); + assert!(sender.send(19i128).is_ok()); + mem::drop(receiver); +} diff --git a/third_party/rust/oneshot-uniffi/examples/send_with_dropped_receiver.rs b/third_party/rust/oneshot-uniffi/examples/send_with_dropped_receiver.rs new file mode 100644 index 0000000000..19bfa385f0 --- /dev/null +++ b/third_party/rust/oneshot-uniffi/examples/send_with_dropped_receiver.rs @@ -0,0 +1,8 @@ +use std::mem; + +fn main() { + let (sender, receiver) = oneshot::channel(); + mem::drop(receiver); + let send_error = sender.send(5u128).unwrap_err(); + assert_eq!(send_error.into_inner(), 5); +} |