From 0e38fa8ca768b8912eca7d86c71f50f437304949 Mon Sep 17 00:00:00 2001 From: LukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com> Date: Mon, 13 May 2024 20:52:40 +0200 Subject: [PATCH] Fix: error type --- Cargo.lock | 11 +++++--- exercises/07_threads/10_patch/Cargo.toml | 1 + exercises/07_threads/10_patch/src/lib.rs | 32 ++++++++++++++-------- exercises/07_threads/11_locks/Cargo.toml | 1 + exercises/07_threads/11_locks/src/lib.rs | 28 ++++++++++++------- exercises/07_threads/12_rw_lock/Cargo.toml | 1 + exercises/07_threads/12_rw_lock/src/lib.rs | 28 ++++++++++++------- 7 files changed, 66 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d84c057..4f1e698 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -234,6 +234,7 @@ dependencies = [ name = "locks" version = "0.1.0" dependencies = [ + "thiserror", "ticket_fields", ] @@ -296,6 +297,7 @@ version = "0.1.0" name = "patch" version = "0.1.0" dependencies = [ + "thiserror", "ticket_fields", ] @@ -336,6 +338,7 @@ dependencies = [ name = "rwlock" version = "0.1.0" dependencies = [ + "thiserror", "ticket_fields", ] @@ -416,18 +419,18 @@ version = "0.1.0" [[package]] name = "thiserror" -version = "1.0.59" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" +checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.59" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" +checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" dependencies = [ "proc-macro2", "quote", diff --git a/exercises/07_threads/10_patch/Cargo.toml b/exercises/07_threads/10_patch/Cargo.toml index 57b902a..ff20e0f 100644 --- a/exercises/07_threads/10_patch/Cargo.toml +++ b/exercises/07_threads/10_patch/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] ticket_fields = { path = "../../../helpers/ticket_fields" } +thiserror = "1.0.59" diff --git a/exercises/07_threads/10_patch/src/lib.rs b/exercises/07_threads/10_patch/src/lib.rs index 1213f3f..492f767 100644 --- a/exercises/07_threads/10_patch/src/lib.rs +++ b/exercises/07_threads/10_patch/src/lib.rs @@ -1,4 +1,4 @@ -use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError}; +use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; // TODO: Implement the patching functionality. use crate::data::{Ticket, TicketDraft, TicketPatch}; @@ -13,27 +13,35 @@ pub struct TicketStoreClient { } impl TicketStoreClient { - pub fn insert(&self, draft: TicketDraft) -> Result> { + pub fn insert(&self, draft: TicketDraft) -> Result { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Insert { - draft, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Insert { + draft, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } - pub fn get(&self, id: TicketId) -> Result, TrySendError> { + pub fn get(&self, id: TicketId) -> Result, OverloadedError> { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Get { - id, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Get { + id, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } - pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), TrySendError> {} + pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), OverloadedError> {} } +#[derive(Debug, thiserror::Error)] +#[error("The store is overloaded")] +pub struct OverloadedError; + pub fn launch(capacity: usize) -> TicketStoreClient { let (sender, receiver) = sync_channel(capacity); std::thread::spawn(move || server(receiver)); diff --git a/exercises/07_threads/11_locks/Cargo.toml b/exercises/07_threads/11_locks/Cargo.toml index f79fff4..0dbe4e8 100644 --- a/exercises/07_threads/11_locks/Cargo.toml +++ b/exercises/07_threads/11_locks/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] ticket_fields = { path = "../../../helpers/ticket_fields" } +thiserror = "1.0.60" diff --git a/exercises/07_threads/11_locks/src/lib.rs b/exercises/07_threads/11_locks/src/lib.rs index 18d78c8..b5e6c0d 100644 --- a/exercises/07_threads/11_locks/src/lib.rs +++ b/exercises/07_threads/11_locks/src/lib.rs @@ -16,25 +16,33 @@ pub struct TicketStoreClient { } impl TicketStoreClient { - pub fn insert(&self, draft: TicketDraft) -> Result> { + pub fn insert(&self, draft: TicketDraft) -> Result { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Insert { - draft, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Insert { + draft, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } - pub fn get(&self, id: TicketId) -> Result>>, TrySendError> { + pub fn get(&self, id: TicketId) -> Result>>, OverloadedError> { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Get { - id, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Get { + id, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } } +#[derive(Debug, thiserror::Error)] +#[error("The store is overloaded")] +pub struct OverloadedError; + pub fn launch(capacity: usize) -> TicketStoreClient { let (sender, receiver) = sync_channel(capacity); std::thread::spawn(move || server(receiver)); diff --git a/exercises/07_threads/12_rw_lock/Cargo.toml b/exercises/07_threads/12_rw_lock/Cargo.toml index 1f626c7..e54f769 100644 --- a/exercises/07_threads/12_rw_lock/Cargo.toml +++ b/exercises/07_threads/12_rw_lock/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] ticket_fields = { path = "../../../helpers/ticket_fields" } +thiserror = "1.0.60" diff --git a/exercises/07_threads/12_rw_lock/src/lib.rs b/exercises/07_threads/12_rw_lock/src/lib.rs index 1235df7..0c33f53 100644 --- a/exercises/07_threads/12_rw_lock/src/lib.rs +++ b/exercises/07_threads/12_rw_lock/src/lib.rs @@ -15,25 +15,33 @@ pub struct TicketStoreClient { } impl TicketStoreClient { - pub fn insert(&self, draft: TicketDraft) -> Result> { + pub fn insert(&self, draft: TicketDraft) -> Result { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Insert { - draft, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Insert { + draft, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } - pub fn get(&self, id: TicketId) -> Result>>, TrySendError> { + pub fn get(&self, id: TicketId) -> Result>>, OverloadedError> { let (response_sender, response_receiver) = sync_channel(1); - self.sender.try_send(Command::Get { - id, - response_channel: response_sender, - })?; + self.sender + .try_send(Command::Get { + id, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; Ok(response_receiver.recv().unwrap()) } } +#[derive(Debug, thiserror::Error)] +#[error("The store is overloaded")] +pub struct OverloadedError; + pub fn launch(capacity: usize) -> TicketStoreClient { let (sender, receiver) = sync_channel(capacity); std::thread::spawn(move || server(receiver));