From: Greg Burri Date: Thu, 24 Oct 2024 14:16:30 +0000 (+0200) Subject: Exercise 07-08 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=ae76a9ec9fec56151bafd94523b0ce40c3e879b2;p=rust_exercises.git Exercise 07-08 --- diff --git a/exercises/07_threads/08_client/src/lib.rs b/exercises/07_threads/08_client/src/lib.rs index a934bd3..980a53e 100644 --- a/exercises/07_threads/08_client/src/lib.rs +++ b/exercises/07_threads/08_client/src/lib.rs @@ -7,23 +7,43 @@ pub mod store; #[derive(Clone)] // TODO: flesh out the client implementation. -pub struct TicketStoreClient {} +pub struct TicketStoreClient { + sender: Sender, +} impl TicketStoreClient { + fn new(sender: Sender) -> Self { + Self { sender } + } + // Feel free to panic on all errors, for simplicity. pub fn insert(&self, draft: TicketDraft) -> TicketId { - todo!() + let (response_sender, response_receiver) = std::sync::mpsc::channel(); + self.sender + .send(Command::Insert { + draft, + response_channel: response_sender, + }) + .unwrap(); + response_receiver.recv().unwrap() } pub fn get(&self, id: TicketId) -> Option { - todo!() + let (response_sender, response_receiver) = std::sync::mpsc::channel(); + self.sender + .send(Command::Get { + id, + response_channel: response_sender, + }) + .unwrap(); + response_receiver.recv().unwrap() } } pub fn launch() -> TicketStoreClient { let (sender, receiver) = std::sync::mpsc::channel(); std::thread::spawn(move || server(receiver)); - todo!() + TicketStoreClient::new(sender) } // No longer public! This becomes an internal detail of the library now.