summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-11-14 12:19:45 -0600
committerBrad Clawsie <brad@b7j0c.org>2026-11-14 12:19:45 -0600
commitafa39bf9f5a25cbd49998d37fd224063c3894332 (patch)
tree84313bbf04a77f5a6beb686ff546836cc3191bde
parent5a95866c8a8b22d673b1dab543a9b6a240ca6f22 (diff)
feat: learn_7
commit 00a7e3004c68679f6a38a29d85ff15e0571ff2d4 Author: Brad Clawsie <brad@b7j0c.org> Date: Sat Nov 14 12:19:29 2026 -0600 feat: learn_7
-rw-r--r--learn_7/Cargo.lock7
-rw-r--r--learn_7/Cargo.toml6
-rw-r--r--learn_7/src/main.rs96
3 files changed, 109 insertions, 0 deletions
diff --git a/learn_7/Cargo.lock b/learn_7/Cargo.lock
new file mode 100644
index 0000000..1221f91
--- /dev/null
+++ b/learn_7/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "learn_7"
+version = "0.1.0"
diff --git a/learn_7/Cargo.toml b/learn_7/Cargo.toml
new file mode 100644
index 0000000..d7b3626
--- /dev/null
+++ b/learn_7/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "learn_7"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/learn_7/src/main.rs b/learn_7/src/main.rs
new file mode 100644
index 0000000..e950bac
--- /dev/null
+++ b/learn_7/src/main.rs
@@ -0,0 +1,96 @@
+struct Job {
+ id: u64,
+ description: String,
+ payload: Vec<u8>,
+}
+
+struct WorkQueue {
+ jobs: Vec<Job>,
+}
+
+impl WorkQueue {
+ pub fn new() -> Self {
+ Self {
+ jobs: Vec::new(),
+ }
+ }
+
+ pub fn submit(&mut self, job: Job) {
+ self.jobs.push(job)
+ }
+
+ pub fn next(&mut self) -> Option<Job> {
+ if self.jobs.len() != 0 {
+ return Some(self.jobs.remove(0));
+ }
+ None
+ }
+
+ pub fn len(&self) -> usize {
+ self.jobs.len()
+ }
+
+ pub fn drain_descriptions(&mut self) -> Vec<String> {
+ self.jobs.drain(..).map(|job| job.description).collect()
+ }
+}
+
+fn main() {
+ ()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::*;
+
+ #[test]
+ pub fn test_new() {
+ assert_eq!(WorkQueue::new().jobs.len(), 0);
+ }
+
+ #[test]
+ pub fn test_submit() {
+ let mut wq = WorkQueue::new();
+ assert_eq!(wq.jobs.len(), 0);
+ wq.submit(Job{id:1, description:String::from("1"), payload: Vec::new()});
+ assert_eq!(wq.jobs.len(), 1);
+ }
+
+ #[test]
+ pub fn test_next() {
+ let mut wq = WorkQueue::new();
+ assert_eq!(wq.jobs.len(), 0);
+ wq.submit(Job{id:1, description:String::from("1"), payload: Vec::new()});
+ assert_eq!(wq.jobs.len(), 1);
+ wq.submit(Job{id:2, description:String::from("2"), payload: Vec::new()});
+ assert_eq!(wq.jobs.len(), 2);
+ assert!(wq.next().is_some_and(|x| x.id == 1));
+ assert!(wq.next().is_some_and(|x| x.id == 2));
+ assert!(wq.next().is_none());
+ }
+
+ #[test]
+ pub fn test_len() {
+ let mut wq = WorkQueue::new();
+ assert_eq!(wq.len(), 0);
+ wq.submit(Job{id:1, description:String::from("1"), payload: Vec::new()});
+ assert_eq!(wq.len(), 1);
+ wq.submit(Job{id:2, description:String::from("2"), payload: Vec::new()});
+ assert_eq!(wq.len(), 2);
+ assert!(wq.next().is_some_and(|x| x.id == 1));
+ assert_eq!(wq.len(), 1);
+ assert!(wq.next().is_some_and(|x| x.id == 2));
+ assert_eq!(wq.len(), 0);
+ assert!(wq.next().is_none());
+ }
+
+ #[test]
+ pub fn test_drain_descriptions() {
+ let mut wq = WorkQueue::new();
+ assert_eq!(wq.drain_descriptions().len(), 0);
+ wq.submit(Job{id:1, description:String::from("1"), payload: Vec::new()});
+ wq.submit(Job{id:2, description:String::from("2"), payload: Vec::new()});
+ let descriptions = wq.drain_descriptions();
+ assert_eq!(descriptions.as_slice(), &["1", "2"]);
+ }
+}