From ae76a9ec9fec56151bafd94523b0ce40c3e879b2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 24 Oct 2024 16:16:30 +0200 Subject: [PATCH] Exercise 07-08 --- exercises/07_threads/08_client/src/lib.rs | 28 +++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) 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. -- 2.45.2