“Alright, listen up! The sleigh’s armed with the ElfiX 9000—64 blazing-fast cores. But without a working task queue, we’re going nowhere. I need ideas, and I need them fast!”
Blitzen stretched lazily, balancing a candy cane on his nose. “What’s the big deal? Just make a list, throw everything on there, and let the sleigh do its thing.”
Bernard, the Head Elf, sighed audibly. “Blitzen, a list? Really? We need something efficient, predictable, and thread-safe. We’re not juggling reindeer treats here!”
Santa grunted. “Bernard’s right. This isn’t reindeer games. We need ultimate thread safety and dynamic dispatch. Bernard, go on.”
Bernard adjusted his glasses. “We use a VecDeque
. Tasks get pushed to the back and popped from the front. It’s simple and linear.”
Blitzen tilted his head. “And how do we stop the sleigh from pulling a Rudolph and wandering off the rails?”
Bernard rolled his eyes. “Thread safety. Wrap the VecDeque
in a Mutex
, so each core pops tasks one at a time without stepping on each other’s hooves.”
Santa stroked his beard. “And the tasks? We’ve got a mix: deliveries, routing, even…” he checked the list, “…this goat thing.”
Bernard cut him off. “We implement a SleighTask
trait. Each task adheres to it, and the queue just holds Box<dyn SleighTask>
. The cores process whatever’s next without worrying about specifics.”
Blitzen yawned. “Sounds overly complicated to me.”
“Spoken like someone who’s never touched a Mutex
,” Bernard snapped.
Santa chuckled. “Enough yapping! The VecDeque
is our solution. Let’s build it. Christmas flies on this queue!”
The workshop buzzed with determination. With the VecDeque
at its heart, the sleigh would soon conquer the skies—no reindeer complaints included.
The SantaSleighQueue
should have a field records
, make sure it is thread safe and can be mutated by multiple threads
The records
list should accept either of the two task types: ElfTask
and ReindeerTask
Both task types should implement the SleighTask
trait
The SantaSleighQueue
should have these methods:
new() -> Self
: Creates a new SantaSleighQueue
enqueue
adds a task to the back of the queue, returns ()
get_task
pops the next task from the front of the queue, returns Option<T>
The ElfTask
should have these fields:
name: String
urgency: u32
The ElfTask
should have a new()
associated function that creates a new ElfTask
The ReindeerTask
should have these fields:
name: String
weight: u32
The ReindeerTask
should have a new()
associated function that creates a new ReindeerTask
Don't worry about the use of the urgency
and weight
fields, they are just there for demonstration purposes
You can use unwrap()
on the mutex lock result; for this challenge, you don't need to worry about poisoning.
Make sure all important values are pub
so they can be accessed from outside the module
Make sure you have a look at the bottom of the code to see how Santa wants to use the SantaSleighQueue
API.
If you’re unsure where to start, take a look at these tips:
Use a VecDeque
to store the tasks, so that you can push and pop tasks from the front and back.
Wrap the VecDeque
in a Mutex
to make it thread-safe.
In order to hold either ElfTask
or ReindeerTask
in the VecDeque
, you can use Box
to store a trait object on the heap.
Before mutating the VecDeque
, you need to lock the Mutex
to ensure that only one thread can access it at a time.
Since Mutex
allows interior mutability, you don't need to get a mutable reference to the self
object, you can just use &self
.
aaron-otis
use std::sync::Arc;use std::thread;use std::sync::Mutex;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>, // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}unsafe impl Send for SantaSleighQueue {}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
Ljungg
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>, // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()), } } pub fn enqueue(&self, task: Box<dyn SleighTask>) -> () { self.records.lock().unwrap().push_back(task) } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
tdoan
use std::sync::{Arc, Mutex};use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { let queue = VecDeque::new(); SantaSleighQueue { records: Mutex::new(queue), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) -> () { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
josephcopenhaver
use std::sync::Arc;use std::thread;use std::collections::VecDeque;use std::sync::Mutex;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { // 1. Should store the tasks records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>,}unsafe impl Send for SantaSleighQueue {}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { let records = Mutex::new(VecDeque::<Box<dyn SleighTask + Send>>::new()); Self { records, } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { self.records.lock().unwrap().push_back(task) } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { let name = name.to_owned(); Self { name, urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { let name = name.to_owned(); Self { name, weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
thescooby
use std::sync::Arc;use std::thread;use std::sync::Mutex;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> ElfTask { ElfTask { name: name.to_string(), urgency: urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new (name: &str, weight: u32) -> ReindeerTask { ReindeerTask { name: name.to_string(), weight: weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
overplumbum
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { return SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency} }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
A1-exe
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { // 1. Should store the tasks records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { return SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency} }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
FlorianGD
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { // 1. Should store the tasks records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { return SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency} }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
rjensen
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>> // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { let records = Mutex::new(VecDeque::new()); SantaSleighQueue { records } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u64,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u64) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u64,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u64) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
sarub0b0
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()), } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut queue = self.records.lock().unwrap(); queue.push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut queue = self.records.lock().unwrap(); queue.pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: impl Into<String>, urgency: u32) -> Self { Self { name: name.into(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: impl Into<String>, weight: u32) -> Self { Self { name: name.into(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
habu1010
use std::sync::Arc;use std::sync::Mutex;use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>, // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { let records = Mutex::new(VecDeque::new()); Self { records } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); records.pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
vaclav0411
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send + Sync>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send + Sync>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send + Sync>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> ElfTask { ElfTask { name: name.to_string(), urgency: urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> ReindeerTask { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
mei28
use std::thread;use std::collections::VecDeque;use std::sync::{Arc, Mutex};pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method // Since Mutex allows interior mutability, you don't need to get a mutable reference to the self object, you can just use &self. pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { let mut records = self.records.lock().unwrap(); records.push_back(task); () } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { let mut records = self.records.lock().unwrap(); records.pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the name: String, weight: u32}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
sweet2honey
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Sync + Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
pagyeman
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Sync + Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
titoeb
use std::collections::VecDeque;use std::ops::Deref;use std::ops::DerefMut;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Sync + Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> SantaSleighQueue { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> ElfTask { ElfTask { name: name.into(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> ReindeerTask { ReindeerTask { name: name.into(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
kapaseker
use std::thread;use std::collections::VecDeque;use std::sync::{Arc, Mutex};pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method // Since Mutex allows interior mutability, you don't need to get a mutable reference to the self object, you can just use &self. pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { let mut records = self.records.lock().unwrap(); records.push_back(task); () } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { let mut records = self.records.lock().unwrap(); records.pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the name: String, weight: u32}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
wendko
use std::thread;use std::collections::VecDeque;use std::sync::{Arc, Mutex};pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method // Since Mutex allows interior mutability, you don't need to get a mutable reference to the self object, you can just use &self. pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { let mut records = self.records.lock().unwrap(); records.push_back(task); () } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { let mut records = self.records.lock().unwrap(); records.pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the name: String, weight: u32}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
vihamaki
use std::sync::Arc;use std::thread;use std::sync::Mutex;use std::collections::VecDeque;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>, // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, item: Box<dyn SleighTask + Send>) { self.records.lock().unwrap().push_back(item); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight, } } }impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
MGehrmann
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, record: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(record); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); if records.len() == 0 { None } else { records.pop_front() } }}pub struct ElfTask { // 5. Define the fields name: String, urgency: i32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: i32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: i32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: i32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
KushnerykPavel
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>,// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { self.records.lock().unwrap().push_back(task) } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
frankstolle
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::default(), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: impl ToString, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: impl ToString, weight: u32) -> Self { Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
Fedott
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>,// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { self.records.lock().unwrap().push_back(task) } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
hafihaf123
use std::sync::{Mutex, Arc};use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { let records = Mutex::new(VecDeque::new()); Self { records } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self{ Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self{ Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
CianciuStyles
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { // 1. Should store the tasks records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task) } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
joanne-cmd
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, record: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(record); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); match records.len() { 0 => None, _ => records.pop_front(), } }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
GiulianoCTRL
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } pub fn enqueue<>(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { pub name: String, pub urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { pub name: String, pub weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
gmvar
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, record: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(record); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); match records.len() { 0 => None, _ => records.pop_front(), } }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
gmvar
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, record: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(record); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); if records.len() == 0 { None } else { records.pop_front() } }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
strachan
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>> // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
arm01846
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, record: Box<dyn SleighTask>) { let mut records = self.records.lock().unwrap(); records.push_back(record); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut records = self.records.lock().unwrap(); if records.len() == 0 { None } else { records.pop_front() } }}pub struct ElfTask { // 5. Define the fields name: String, urgency: i32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: i32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: i32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: i32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
tamanishi
use std::sync::Arc;use std::thread;use std::collections::VecDeque;use std::sync::Mutex;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency: urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight: weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
Ankit8848
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
DominicD
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
rony0000013
use std::sync::Arc;use std::sync::Mutex;use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut queue = self.records.lock().unwrap(); queue.push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut queue = self.records.lock().unwrap(); queue.pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: impl Into<String>, urgency: u32) -> Self { ElfTask { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: impl Into<String>, weight: u32) -> Self { ReindeerTask { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
Stephan-Lindner
use std::sync::Arc;use std::sync::Mutex;use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut queue = self.records.lock().unwrap(); queue.push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut queue = self.records.lock().unwrap(); queue.pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: impl Into<String>, urgency: u32) -> Self { ElfTask { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: impl Into<String>, weight: u32) -> Self { ReindeerTask { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
wamimi
use std::sync::Arc;use std::sync::Mutex;use std::collections::VecDeque;use std::thread;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask + Send>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) { let mut queue = self.records.lock().unwrap(); queue.push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { let mut queue = self.records.lock().unwrap(); queue.pop_front() }}pub struct ElfTask { pub name: String, pub urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { pub name: String, pub weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
chriswmann
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task)} // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
gjdenhertog
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
IslameN
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
DomiDre
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>> // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
SirVer
use std::sync::{Mutex, Arc};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut k = self.records.lock().unwrap(); k.push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut k = self.records.lock().unwrap(); k.pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: impl Into<String>, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: impl Into<String>, weight: u32) -> Self { ReindeerTask { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
KLcpb
use std::sync::{Arc, Mutex};use std::thread;use std::collections::VecDeque;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self{ SantaSleighQueue { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self,task: Box<dyn SleighTask>) -> (){ let mut records = self.records.lock().unwrap(); records.push_back(task); () } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>>{ let mut records = self.records.lock().unwrap(); if let Some(t) = records.pop_front() { return Some(t); }else{ return None; }; }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(task: &str,urgency:u32) -> Self{ ElfTask{ name: task.to_string(), urgency: urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(task: &str,weight:u32) -> Self{ ReindeerTask {name:task.to_string(), weight} }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
Burnus
use std::collections::VecDeque;use std::sync::{Arc, Mutex};use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>,}impl SantaSleighQueue { pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } pub fn enqueue(&self, task: Box<dyn SleighTask>) -> () { self.records.lock().unwrap().push_back(task); () } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { pub name: String, pub urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { pub name: String, pub weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
sriharshamadala
use std::sync::Arc;use std::thread;use std::collections::VecDeque;use std::sync::Mutex;pub trait SleighTask : Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { pub fn new() -> Self { let records = Mutex::new(VecDeque::new()); Self { records } } pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
LenRemmerswaal
use std::sync::Arc;use std::thread;use std::sync::Mutex;use std::collections::VecDeque;/// The trait does not know its implementors are Send./// So tell the compiler that every SleighTask must be Send: /// "pub trait SleighTask: Send {"pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>, // 1. Should store the tasks}impl SantaSleighQueue{ // 2. Define the `new` constructor pub fn new() -> Self { Self {records: Mutex::new(VecDeque::new())} } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
jean-roland
use std::sync::Arc;use std::sync::Mutex;use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>// 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { SantaSleighQueue { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { let mut queue = self.records.lock().unwrap(); queue.push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { let mut queue = self.records.lock().unwrap(); queue.pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { ElfTask { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields pub name: String, pub weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { ReindeerTask { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
lulingar
use std::sync::{Arc, Mutex};use std::collections::VecDeque;use std::thread;pub trait SleighTask: Send { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { // 5. Define the fields pub name: String, pub urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32) -> Self { Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
zilid10
use std::sync::Arc;use std::thread;use std::collections::VecDeque;use std::sync::Mutex;pub trait SleighTask: Send + Sync { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Mutex<VecDeque<Box<dyn SleighTask>>>, // 1. Should store the tasks}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Mutex::new(VecDeque::new()), } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.records.lock().unwrap().push_back(task); } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask>> { self.records.lock().unwrap().pop_front() }}#[derive(Default)]pub struct ElfTask { // 5. Define the fields name: String, urgency: u32,}impl ElfTask { // 6. Define the `new` constructor pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.to_string(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}#[derive(Default)]pub struct ReindeerTask { // 7. Define the fields name: String, weight: u32,}impl ReindeerTask { // 8. Define the `new` constructor pub fn new(name: &str, weight: u32)-> Self { Self { name: name.to_string(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}
tonisk
use std::sync::Arc;use std::sync::Mutex;use std::thread;use std::collections::VecDeque;pub trait SleighTask { fn describe(&self) -> String;}pub struct SantaSleighQueue { records: Arc<Mutex<VecDeque<Box<dyn SleighTask + Send>>>>,}impl SantaSleighQueue { // 2. Define the `new` constructor pub fn new() -> Self { Self { records: Arc::new(Mutex::new(VecDeque::new())) } } // 3. Define the `enqueue` method pub fn enqueue(&self, task: Box<dyn SleighTask + Send>) -> () { self.records.lock().unwrap().push_back(task); () } // 4. Define the `get_task` method pub fn get_task(&self) -> Option<Box<dyn SleighTask + Send>> { self.records.lock().unwrap().pop_front() }}pub struct ElfTask { name: String, urgency: u32,}impl ElfTask { pub fn new(name: &str, urgency: u32) -> Self { Self { name: name.into(), urgency, } }}impl SleighTask for ElfTask { fn describe(&self) -> String { format!("Elf task: {} (urgency {})", self.name, self.urgency) }}pub struct ReindeerTask { name: String, weight: u32,}impl ReindeerTask { pub fn new(name: &str, weight: u32) -> Self { Self { name: name.into(), weight, } }}impl SleighTask for ReindeerTask { fn describe(&self) -> String { format!("Reindeer task: {} ({} kg)", self.name, self.weight) }}pub fn main() { let queue = Arc::new(SantaSleighQueue::new()); let producer_queue = Arc::clone(&queue); let producer = thread::spawn(move || { producer_queue.enqueue(Box::new(ReindeerTask::new("Deliver Toys", 100))); producer_queue.enqueue(Box::new(ElfTask::new("Wrap Gifts", 3))); producer_queue.enqueue(Box::new(ReindeerTask::new("Collect Reindeer Feed", 50))); producer_queue.enqueue(Box::new(ElfTask::new("Decorate Tree", 7))); }); thread::sleep(std::time::Duration::from_millis(10)); let consumer_queue = Arc::clone(&queue); let consumer = thread::spawn(move || loop { if let Some(task) = consumer_queue.get_task() { println!("{}", task.describe()); } else { break; } }); producer.join().unwrap(); consumer.join().unwrap();}