Struct PoisonError

pub struct PoisonError<T> { pub(in ::sync::poison) data: 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}");
    }
};

Fields

data: T

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) -> 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) -> &T

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

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

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

Trait Implementations

impl<T> Debug for PoisonError<T>

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

impl<T> Display for PoisonError<T>

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

impl<T> Error for PoisonError<T>

Auto Trait Implementations

impl<T> Freeze for PoisonError<T> where T: Freeze,

impl<T> RefUnwindSafe for PoisonError<T> where T: RefUnwindSafe,

impl<T> Send for PoisonError<T> where T: Send,

impl<T> Sync for PoisonError<T> where T: Sync,

impl<T> Unpin for PoisonError<T> where T: Unpin,

impl<T> UnsafeUnpin for PoisonError<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for PoisonError<T> where T: UnwindSafe,

Blanket Implementations

impl<T> Any for PoisonError<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for PoisonError<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for PoisonError<T> where T: ?Sized,

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

impl<T> From<T> for PoisonError<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for PoisonError<T> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for PoisonError<T>

impl<T> ToString for PoisonError<T> where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for PoisonError<T> where U: From<T>,

fn into(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<U> for PoisonError<T> where U: Into<T>,

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

impl<T, U> TryInto<U> for PoisonError<T> where U: TryFrom<T>,

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