From 00991791bba57a8757ba207a573e360224f43e6e Mon Sep 17 00:00:00 2001 From: Botahamec Date: Thu, 26 Sep 2024 22:12:52 -0400 Subject: Docs and improvements --- src/poisonable/error.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'src/poisonable/error.rs') diff --git a/src/poisonable/error.rs b/src/poisonable/error.rs index 2384953..1c4d60a 100644 --- a/src/poisonable/error.rs +++ b/src/poisonable/error.rs @@ -18,21 +18,117 @@ impl fmt::Display for PoisonError { impl Error for PoisonError {} impl PoisonError { + /// Creates a `PoisonError` + /// + /// This is generally created by methods like [`Poisonable::lock`]. + /// + /// ``` + /// use happylock::poisonable::PoisonError; + /// + /// let error = PoisonError::new("oh no"); + /// ``` + /// + /// [`Poisonable::lock`]: `crate::poisonable::Poisonable::lock` #[must_use] pub const fn new(guard: Guard) -> Self { Self { guard } } + /// Consumes the error indicating that a lock is poisonmed, returning the + /// underlying guard to allow access regardless. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::sync::Arc; + /// use std::thread; + /// + /// use happylock::{Mutex, Poisonable, ThreadKey}; + /// + /// let mutex = Arc::new(Poisonable::new(Mutex::new(HashSet::new()))); + /// + /// // poison the mutex + /// let c_mutex = Arc::clone(&mutex); + /// let _ = thread::spawn(move || { + /// let key = ThreadKey::get().unwrap(); + /// let mut data = c_mutex.lock(key).unwrap(); + /// data.insert(10); + /// panic!(); + /// }).join(); + /// + /// let key = ThreadKey::get().unwrap(); + /// let p_err = mutex.lock(key).unwrap_err(); + /// let data = p_err.into_inner(); + /// println!("recovered {} items", data.len()); + /// ``` #[must_use] pub fn into_inner(self) -> Guard { self.guard } + /// Reaches into this error indicating that a lock is poisoned, returning a + /// reference to the underlying guard to allow access regardless. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::sync::Arc; + /// use std::thread; + /// + /// use happylock::{Mutex, Poisonable, ThreadKey}; + /// + /// let mutex = Arc::new(Poisonable::new(Mutex::new(HashSet::new()))); + /// + /// // poison the mutex + /// let c_mutex = Arc::clone(&mutex); + /// let _ = thread::spawn(move || { + /// let key = ThreadKey::get().unwrap(); + /// let mut data = c_mutex.lock(key).unwrap(); + /// data.insert(10); + /// panic!(); + /// }).join(); + /// + /// let key = ThreadKey::get().unwrap(); + /// let p_err = mutex.lock(key).unwrap_err(); + /// let data = p_err.get_ref(); + /// println!("recovered {} items", data.len()); + /// ``` #[must_use] pub const fn get_ref(&self) -> &Guard { &self.guard } + /// Reaches into this error indicating that a lock is poisoned, returning a + /// mutable reference to the underlying guard to allow access regardless. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::sync::Arc; + /// use std::thread; + /// + /// use happylock::{Mutex, Poisonable, ThreadKey}; + /// + /// let mutex = Arc::new(Poisonable::new(Mutex::new(HashSet::new()))); + /// + /// // poison the mutex + /// let c_mutex = Arc::clone(&mutex); + /// let _ = thread::spawn(move || { + /// let key = ThreadKey::get().unwrap(); + /// let mut data = c_mutex.lock(key).unwrap(); + /// data.insert(10); + /// panic!(); + /// }).join(); + /// + /// let key = ThreadKey::get().unwrap(); + /// let mut p_err = mutex.lock(key).unwrap_err(); + /// let data = p_err.get_mut(); + /// data.insert(20); + /// println!("recovered {} items", data.len()); + /// ``` #[must_use] pub fn get_mut(&mut self) -> &mut Guard { &mut self.guard -- cgit v1.2.3