Struct PoisonError

struct PoisonError<T> { ... }

A type of error which can be returned whenever a lock is acquired.

Both Mutexes and RwLocks are poisoned whenever a thread fails while the lock is held. The precise semantics for when a lock is poisoned is documented on each lock. For a lock in the poisoned state, unless the state is cleared manually, all future acquisitions will return this error.

Examples

use std::sync::{Arc, Mutex};
use std::thread;

let mutex = Arc::new(Mutex::new(1));

// poison the mutex
let c_mutex = Arc::clone(&mutex);
let _ = thread::spawn(move || {
    let mut data = c_mutex.lock().unwrap();
    *data = 2;
    panic!();
}).join();

match mutex.lock() {
    Ok(_) => unreachable!(),
    Err(p_err) => {
        let data = p_err.get_ref();
        println!("recovered: {data}");
    }
};

Implementations

impl<T> PoisonError<T>

fn new(data: T) -> PoisonError<T>

Creates a PoisonError.

This is generally created by methods like Mutex::lock or RwLock::read.

This method may panic if std was built with panic="abort".

fn into_inner(self: Self) -> T

Consumes this error indicating that a lock is poisoned, returning the associated data.

Examples

use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::thread;

let mutex = Arc::new(Mutex::new(HashSet::new()));

// poison the mutex
let c_mutex = Arc::clone(&mutex);
let _ = thread::spawn(move || {
    let mut data = c_mutex.lock().unwrap();
    data.insert(10);
    panic!();
}).join();

let p_err = mutex.lock().unwrap_err();
let data = p_err.into_inner();
println!("recovered {} items", data.len());
fn get_ref(self: &Self) -> &T

Reaches into this error indicating that a lock is poisoned, returning a reference to the associated data.

fn get_mut(self: &mut Self) -> &mut T

Reaches into this error indicating that a lock is poisoned, returning a mutable reference to the associated data.

impl<T> Any for PoisonError<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for PoisonError<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for PoisonError<T>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> Debug for PoisonError<T>

fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result

impl<T> Display for PoisonError<T>

fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result

impl<T> Error for PoisonError<T>

impl<T> Freeze for PoisonError<T>

impl<T> From for PoisonError<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> RefUnwindSafe for PoisonError<T>

impl<T> Send for PoisonError<T>

impl<T> Sync for PoisonError<T>

impl<T> ToString for PoisonError<T>

fn to_string(self: &Self) -> String

impl<T> Unpin for PoisonError<T>

impl<T> UnwindSafe for PoisonError<T>

impl<T, U> Into for PoisonError<T>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for PoisonError<T>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for PoisonError<T>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>