From a7edc9d4c552d1f6565607bb46f5db2feca4519c Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 26 Oct 2024 12:46:11 +0200 Subject: [PATCH] Exercise 07-12 --- exercises/07_threads/12_rw_lock/src/lib.rs | 8 ++++---- exercises/07_threads/12_rw_lock/src/store.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/exercises/07_threads/12_rw_lock/src/lib.rs b/exercises/07_threads/12_rw_lock/src/lib.rs index 0c33f53..9282a36 100644 --- a/exercises/07_threads/12_rw_lock/src/lib.rs +++ b/exercises/07_threads/12_rw_lock/src/lib.rs @@ -1,7 +1,7 @@ // TODO: Replace `Mutex` with `RwLock` in the `TicketStore` struct and // all other relevant places to allow multiple readers to access the ticket store concurrently. use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, RwLock}; use crate::data::{Ticket, TicketDraft}; use crate::store::{TicketId, TicketStore}; @@ -26,7 +26,7 @@ impl TicketStoreClient { Ok(response_receiver.recv().unwrap()) } - pub fn get(&self, id: TicketId) -> Result>>, OverloadedError> { + pub fn get(&self, id: TicketId) -> Result>>, OverloadedError> { let (response_sender, response_receiver) = sync_channel(1); self.sender .try_send(Command::Get { @@ -55,11 +55,11 @@ enum Command { }, Get { id: TicketId, - response_channel: SyncSender>>>, + response_channel: SyncSender>>>, }, } -pub fn server(receiver: Receiver) { +fn server(receiver: Receiver) { let mut store = TicketStore::new(); loop { match receiver.recv() { diff --git a/exercises/07_threads/12_rw_lock/src/store.rs b/exercises/07_threads/12_rw_lock/src/store.rs index 7387efd..c70867c 100644 --- a/exercises/07_threads/12_rw_lock/src/store.rs +++ b/exercises/07_threads/12_rw_lock/src/store.rs @@ -1,13 +1,13 @@ use crate::data::{Status, Ticket, TicketDraft}; use std::collections::BTreeMap; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, RwLock}; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct TicketId(u64); #[derive(Clone)] pub struct TicketStore { - tickets: BTreeMap>>, + tickets: BTreeMap>>, counter: u64, } @@ -28,14 +28,14 @@ impl TicketStore { description: ticket.description, status: Status::ToDo, }; - let ticket = Arc::new(Mutex::new(ticket)); + let ticket = Arc::new(RwLock::new(ticket)); self.tickets.insert(id, ticket); id } // The `get` method should return a handle to the ticket // which allows the caller to either read or modify the ticket. - pub fn get(&self, id: TicketId) -> Option>> { + pub fn get(&self, id: TicketId) -> Option>> { self.tickets.get(&id).cloned() } } -- 2.45.2