// SPDX-FileCopyrightText: 2022-Present Nils Andreas Svee mailto:contact@lochnair.net (github @Lochnair) // // SPDX-License-Identifier: MPL-2.0 use anyhow::anyhow; use std::sync::{Arc, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; pub type ArcMutex = Arc>; pub type ArcRwLock = Arc>; pub trait MutexExt { fn lock_anyhow(&self) -> anyhow::Result>; } impl MutexExt for Mutex { fn lock_anyhow(&self) -> anyhow::Result> { self.lock().map_err(|e| anyhow!("mutex poisoned: {e}")) } } pub trait RwLockExt { fn read_anyhow(&self) -> anyhow::Result>; fn write_anyhow(&self) -> anyhow::Result>; } impl RwLockExt for RwLock { fn read_anyhow(&self) -> anyhow::Result> { self.read().map_err(|e| anyhow!("rwlock poisoned: {e}")) } fn write_anyhow(&self) -> anyhow::Result> { self.write().map_err(|e| anyhow!("rwlock poisoned: {e}")) } }