From: Greg Burri Date: Fri, 25 Oct 2024 22:46:18 +0000 (+0200) Subject: Exercise 07-10 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=d9d71dfeb956a150e27d9c9ebcecb589e8571bd9;p=rust_exercises.git Exercise 07-10 --- diff --git a/exercises/07_threads/10_patch/src/lib.rs b/exercises/07_threads/10_patch/src/lib.rs index 492f767..61bdd43 100644 --- a/exercises/07_threads/10_patch/src/lib.rs +++ b/exercises/07_threads/10_patch/src/lib.rs @@ -35,7 +35,16 @@ impl TicketStoreClient { Ok(response_receiver.recv().unwrap()) } - pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), OverloadedError> {} + pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), OverloadedError> { + let (response_sender, response_receiver) = sync_channel(1); + self.sender + .try_send(Command::Update { + patch: ticket_patch, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; + Ok(response_receiver.recv().unwrap()) + } } #[derive(Debug, thiserror::Error)] @@ -63,7 +72,7 @@ enum Command { }, } -pub fn server(receiver: Receiver) { +fn server(receiver: Receiver) { let mut store = TicketStore::new(); loop { match receiver.recv() { @@ -85,7 +94,18 @@ pub fn server(receiver: Receiver) { patch, response_channel, }) => { - todo!() + if let Some(ticket) = store.get_mut(patch.id) { + if let Some(title) = patch.title { + ticket.title = title; + } + if let Some(description) = patch.description { + ticket.description = description; + } + if let Some(status) = patch.status { + ticket.status = status; + } + } + let _ = response_channel.send(()); } Err(_) => { // There are no more senders, so we can safely break