diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/ringbuf/examples | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ringbuf/examples')
-rw-r--r-- | third_party/rust/ringbuf/examples/message.rs | 65 | ||||
-rw-r--r-- | third_party/rust/ringbuf/examples/simple.rs | 20 |
2 files changed, 85 insertions, 0 deletions
diff --git a/third_party/rust/ringbuf/examples/message.rs b/third_party/rust/ringbuf/examples/message.rs new file mode 100644 index 0000000000..026dfb1667 --- /dev/null +++ b/third_party/rust/ringbuf/examples/message.rs @@ -0,0 +1,65 @@ +extern crate ringbuf; + +use std::io::Read; +use std::thread; +use std::time::Duration; + +use ringbuf::RingBuffer; + +fn main() { + let buf = RingBuffer::<u8>::new(10); + let (mut prod, mut cons) = buf.split(); + + let smsg = "The quick brown fox jumps over the lazy dog"; + + let pjh = thread::spawn(move || { + println!("-> sending message: '{}'", smsg); + + let zero = [0]; + let mut bytes = smsg.as_bytes().chain(&zero[..]); + loop { + if prod.is_full() { + println!("-> buffer is full, waiting"); + thread::sleep(Duration::from_millis(1)); + } else { + let n = prod.read_from(&mut bytes, None).unwrap(); + if n == 0 { + break; + } + println!("-> {} bytes sent", n); + } + } + + println!("-> message sent"); + }); + + let cjh = thread::spawn(move || { + println!("<- receiving message"); + + let mut bytes = Vec::<u8>::new(); + loop { + if cons.is_empty() { + if bytes.ends_with(&[0]) { + break; + } else { + println!("<- buffer is empty, waiting"); + thread::sleep(Duration::from_millis(1)); + } + } else { + let n = cons.write_into(&mut bytes, None).unwrap(); + println!("<- {} bytes received", n); + } + } + + assert_eq!(bytes.pop().unwrap(), 0); + let msg = String::from_utf8(bytes).unwrap(); + println!("<- message received: '{}'", msg); + + msg + }); + + pjh.join().unwrap(); + let rmsg = cjh.join().unwrap(); + + assert_eq!(smsg, rmsg); +} diff --git a/third_party/rust/ringbuf/examples/simple.rs b/third_party/rust/ringbuf/examples/simple.rs new file mode 100644 index 0000000000..7ee372d4d1 --- /dev/null +++ b/third_party/rust/ringbuf/examples/simple.rs @@ -0,0 +1,20 @@ +extern crate ringbuf; + +use ringbuf::RingBuffer; + +fn main() { + let rb = RingBuffer::<i32>::new(2); + let (mut prod, mut cons) = rb.split(); + + prod.push(0).unwrap(); + prod.push(1).unwrap(); + assert_eq!(prod.push(2), Err(2)); + + assert_eq!(cons.pop().unwrap(), 0); + + prod.push(2).unwrap(); + + assert_eq!(cons.pop().unwrap(), 1); + assert_eq!(cons.pop().unwrap(), 2); + assert_eq!(cons.pop(), None); +} |