Create a TODO application that assigns a 'quantum state' to each task (both done and not done simultaneously) until you actually check the box, then collapses into reality. Track your tasks with unnecessary observer pattern overhead and philosophical judgment about your task completion ratio.
use std::fmt;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
// Represents the quantum superposition of a task's existence
#[derive(Clone, Debug)]
enum QuantumState {
Superposition { completed_probability: f64 },
Collapsed { is_done: bool },
}
// A task that exists in multiple states until observed
struct QuantumTask {
id: u64,
description: String,
state: Arc<Mutex<QuantumState>>,
observers: Arc<Mutex<Vec<Box<dyn Fn(&QuantumTask) + Send>>>>,
}
impl QuantumTask {
fn new(id: u64, description: String) -> Self {
QuantumTask {
id,
description,
state: Arc::new(Mutex::new(QuantumState::Superposition {
completed_probability: 0.5,
})),
observers: Arc::new(Mutex::new(Vec::new())),
}
}
// Collapse the wave function by observing the task
fn observe(&self, completed: bool) -> Result<(), String> {
let mut state = self.state.lock().map_err(|e| format!("Lock poisoned: {}", e))?;
*state = QuantumState::Collapsed { is_done: completed };
// Notify all observers that the task has been observed
if let Ok(observer_list) = self.observers.lock() {
for observer in observer_list.iter() {
observer(self);
}
}
Ok(())
}
// Check if task is in superposition (unobserved)
fn is_superposed(&self) -> bool {
matches!(
*self.state.lock().unwrap(),
QuantumState::Superposition { .. }
)
}
}
struct ProductivityJudge {
tasks: Arc<Mutex<HashMap<u64, QuantumTask>>>,
next_id: Arc<Mutex<u64>>,
}
impl ProductivityJudge {
fn new() -> Self {
ProductivityJudge {
tasks: Arc::new(Mutex::new(HashMap::new())),
next_id: Arc::new(Mutex::new(1)),
}
}
fn add_task(&self, description: String) -> Result<u64, String> {
let mut id_guard = self.next_id.lock().map_err(|e| format!("ID lock failed: {}", e))?;
let task_id = *id_guard;
*id_guard += 1;
let task = QuantumTask::new(task_id, description);
let mut tasks = self.tasks.lock().map_err(|e| format!("Tasks lock failed: {}", e))?;
tasks.insert(task_id, task);
Ok(task_id)
}
fn complete_task(&self, id: u64) -> Result<(), String> {
let tasks = self.tasks.lock().map_err(|e| format!("Lock error: {}", e))?;
if let Some(task) = tasks.get(&id) {
task.observe(true)?;
Ok(())
} else {
Err(format!("Task {} does not exist in this timeline", id))
}
}
fn judge_productivity(&self) -> String {
let tasks = self.tasks.lock().unwrap();
let total = tasks.len();
if total == 0 {
return "You have achieved perfect emptiness. Very zen.".to_string();
}
let completed = tasks
.values()
.filter(|t| matches!(*t.state.lock().unwrap(), QuantumState::Collapsed { is_done: true }))
.count();
let ratio = completed as f64 / total as f64;
if ratio >= 0.9 {
format!("Outstanding! You have transcended procrastination ({}/{})", completed, total)
} else if ratio >= 0.5 {
format!("Acceptable quantum fluctuations ({}/{})", completed, total)
} else {
format!("Your tasks exist in eternal superposition. Please collapse them ({}/{})", completed, total)
}
}
}
fn main() {
let judge = ProductivityJudge::new();
let _ = judge.add_task("Write quantum code".to_string());
let _ = judge.add_task("Debug reality".to_string());
let task3 = judge.add_task("Achieve enlightenment".to_string());
let _ = judge.complete_task(task3.unwrap());
println!("{}", judge.judge_productivity());
}
Code Review
1. Lines 6-12. The QuantumState enum with Superposition containing a probability field is a fun concept, but you're never actually using that probability value anywhere. It's just dead weight that suggests future functionality that never gets implemented. Either use it or remove it.
2. Lines 18-22. Why is observers a Vec<Box<dyn Fn>> that never actually gets populated? You're building out the observer pattern infrastructure here but there are no methods to register observers. This is classic over-engineering for a feature that doesn't exist.
3. Lines 35-41. The error handling in observe() is paranoid. You're using map_err to convert a lock poison error into a String, then later just calling unwrap() on other locks in judge_productivity(). Pick a strategy and stick with it instead of inconsistent error handling throughout.
4. Line 58. The next_id field is wrapped in an Arc<Mutex<u64>>, but you're not sharing this across threads. A simple Cell<u64> or even just a regular mutable field would work fine. This pattern makes sense in multithreaded code, but there's no evidence this needs to be thread-safe.
5. Lines 70-79. You're calling map_err with custom messages on every single lock operation ("ID lock failed", "Tasks lock failed", etc.), but realistically, if your locks are poisoning, your program has bigger problems. These error paths are theater.
6. Lines 97-111. The judge_productivity() method has a .unwrap() on line 98 while earlier methods carefully use map_err for lock failures. Also, why are you filtering based on a pattern match when you could just count Collapsed { is_done: true } more explicitly? The inconsistency is distracting.
7. Lines 115-125. In main(), you're ignoring all the Result types with let _ = but then calling task3.unwrap() on line 122. If you're not going to handle errors properly, at least be consistent. This looks like you copy-pasted and didn't finish the thought.