Fix: error type
authorLukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com>
Mon, 13 May 2024 18:52:40 +0000 (20:52 +0200)
committerLukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com>
Mon, 13 May 2024 18:59:12 +0000 (20:59 +0200)
Cargo.lock
exercises/07_threads/10_patch/Cargo.toml
exercises/07_threads/10_patch/src/lib.rs
exercises/07_threads/11_locks/Cargo.toml
exercises/07_threads/11_locks/src/lib.rs
exercises/07_threads/12_rw_lock/Cargo.toml
exercises/07_threads/12_rw_lock/src/lib.rs

index d84c057..4f1e698 100644 (file)
@@ -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",
index 57b902a..ff20e0f 100644 (file)
@@ -5,3 +5,4 @@ edition = "2021"
 
 [dependencies]
 ticket_fields = { path = "../../../helpers/ticket_fields" }
+thiserror = "1.0.59"
index 1213f3f..492f767 100644 (file)
@@ -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<TicketId, TrySendError<Command>> {
+    pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, OverloadedError> {
         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<Option<Ticket>, TrySendError<Command>> {
+    pub fn get(&self, id: TicketId) -> Result<Option<Ticket>, 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<Command>> {}
+    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));
index f79fff4..0dbe4e8 100644 (file)
@@ -5,3 +5,4 @@ edition = "2021"
 
 [dependencies]
 ticket_fields = { path = "../../../helpers/ticket_fields" }
+thiserror = "1.0.60"
index 18d78c8..b5e6c0d 100644 (file)
@@ -16,25 +16,33 @@ pub struct TicketStoreClient {
 }
 
 impl TicketStoreClient {
-    pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, TrySendError<Command>> {
+    pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, OverloadedError> {
         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<Option<Arc<Mutex<Ticket>>>, TrySendError<Command>> {
+    pub fn get(&self, id: TicketId) -> Result<Option<Arc<Mutex<Ticket>>>, 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));
index 1f626c7..e54f769 100644 (file)
@@ -5,3 +5,4 @@ edition = "2021"
 
 [dependencies]
 ticket_fields = { path = "../../../helpers/ticket_fields" }
+thiserror = "1.0.60"
index 1235df7..0c33f53 100644 (file)
@@ -15,25 +15,33 @@ pub struct TicketStoreClient {
 }
 
 impl TicketStoreClient {
-    pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, TrySendError<Command>> {
+    pub fn insert(&self, draft: TicketDraft) -> Result<TicketId, OverloadedError> {
         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<Option<Arc<Mutex<Ticket>>>, TrySendError<Command>> {
+    pub fn get(&self, id: TicketId) -> Result<Option<Arc<Mutex<Ticket>>>, 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));