diff --git a/Cargo.lock b/Cargo.lock index e569c6f..dd0e968 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -897,6 +897,13 @@ dependencies = [ "anyhow", ] +[[package]] +name = "mpsc-full" +version = "0.1.0" +dependencies = [ + "anyhow", +] + [[package]] name = "nom" version = "7.1.3" diff --git a/Cargo.toml b/Cargo.toml index f3fd1e1..814e3b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,13 @@ members = [ "members/matchmatchmatch", "members/future_stream", "members/mpsc", + "members/mpsc-full", ] default-members = [ ".", "members/revsqrt", "members/mpsc", + "members/mpsc-full", "members/criterion-demo", "members/shortc", "members/pest-demo", diff --git a/README.md b/README.md index 2258f9d..c4d46a8 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,11 @@ This project contains various smaller rust projects, often made by myself to gain more understanding with a topic or dependency. +```mermaid +graph TD +Start --> End +``` + ## Rust unsafe Unsafe rust offers many possibilities otherwise locked from rust, which might diff --git a/members/mpsc-full/Cargo.toml b/members/mpsc-full/Cargo.toml new file mode 100644 index 0000000..0370fe0 --- /dev/null +++ b/members/mpsc-full/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "mpsc-full" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow.workspace = true diff --git a/members/mpsc-full/src/main.rs b/members/mpsc-full/src/main.rs new file mode 100644 index 0000000..bcded64 --- /dev/null +++ b/members/mpsc-full/src/main.rs @@ -0,0 +1,31 @@ +//! what happens when we send a lot of items to an mpsc? + +use std::{ + sync::{mpsc, Barrier}, + thread, +}; + +const NUMS: &[u8] = &[1, 3, 3, 7, 0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf]; + +fn main() -> anyhow::Result<()> { + let (sender, receiver) = mpsc::channel(); + + for n in NUMS { + sender.send(n).unwrap(); + } + drop(sender); + + let mut collect = Vec::new(); + + while let Ok(n) = receiver.recv() { + collect.push(*n); + } + + for i in NUMS { + assert!(collect.contains(i)) + } + + println!("it stores the values, even when the sender is dropped"); + + Ok(()) +}