chore: split `OrPoisoned` trait into its own crate for reuse

This commit is contained in:
Greg Johnston 2024-02-04 20:55:48 -05:00
parent 6bb5d58369
commit f56023bb25
17 changed files with 76 additions and 38 deletions

View File

@ -3,6 +3,7 @@ resolver = "2"
members = [
# utilities
"oco",
"or_poisoned",
# core
"leptos",

6
or_poisoned/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "or_poisoned"
edition = "2021"
version.workspace = true
[dependencies]

54
or_poisoned/src/lib.rs Normal file
View File

@ -0,0 +1,54 @@
//! Provides a simple trait that unwraps the locks provide by [`std::sync::RwLock`].
//!
//! In every case, this is the same as calling `.expect("lock poisoned")`. However, it
//! does not use `.unwrap()` or `.expect()`, which makes it easier to distinguish from
//! other forms of unwrapping when reading code.
//!
//! ```rust
//! use or_poisoned::OrPoisoned;
//! use std::sync::RwLock;
//!
//! let lock = RwLock::new(String::from("Hello!"));
//!
//! let read = lock.read().or_poisoned();
//! // this is identical to
//! let read = lock.read().unwrap();
//! ```
#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard};
/// Unwraps a lock.
pub trait OrPoisoned {
/// The inner guard type.
type Inner;
/// Unwraps the lock.
///
/// ## Panics
///
/// Will panic if the lock is poisoned.
fn or_poisoned(self) -> Self::Inner;
}
impl<'a, T> OrPoisoned
for Result<RwLockReadGuard<'a, T>, PoisonError<RwLockReadGuard<'a, T>>>
{
type Inner = RwLockReadGuard<'a, T>;
fn or_poisoned(self) -> Self::Inner {
self.expect("lock poisoned")
}
}
impl<'a, T> OrPoisoned
for Result<RwLockWriteGuard<'a, T>, PoisonError<RwLockWriteGuard<'a, T>>>
{
type Inner = RwLockWriteGuard<'a, T>;
fn or_poisoned(self) -> Self::Inner {
self.expect("lock poisoned")
}
}

View File

@ -4,6 +4,7 @@ edition = "2021"
version.workspace = true
[dependencies]
or_poisoned = { workspace = true }
futures = "0.3"
glib = { version = "0.18", optional = true }
pin-project-lite = "0.2.13"

View File

@ -6,9 +6,9 @@ use crate::{
},
signal::SignalReadGuard,
traits::{DefinedAt, Readable},
OrPoisoned,
};
use core::fmt::Debug;
use or_poisoned::OrPoisoned;
use std::{
panic::Location,
sync::{Arc, RwLock, Weak},

View File

@ -12,10 +12,10 @@ use crate::{
owner::Owner,
signal::SignalReadGuard,
traits::{DefinedAt, Readable},
OrPoisoned,
};
use core::fmt::Debug;
use futures::{FutureExt, StreamExt};
use or_poisoned::OrPoisoned;
use std::{
future::Future,
mem,

View File

@ -2,8 +2,8 @@ use super::{ArcAsyncDerived, AsyncState};
use crate::{
graph::{AnySource, ToAnySource},
traits::Track,
OrPoisoned,
};
use or_poisoned::OrPoisoned;
use std::{
future::{Future, IntoFuture},
pin::Pin,

View File

@ -5,8 +5,8 @@ use crate::{
SubscriberSet,
},
owner::Owner,
OrPoisoned,
};
use or_poisoned::OrPoisoned;
use std::sync::RwLock;
pub(crate) struct ArcAsyncDerivedInner {

View File

@ -4,8 +4,8 @@ use crate::{
Source, SourceSet, Subscriber, SubscriberSet,
},
owner::Owner,
OrPoisoned,
};
use or_poisoned::OrPoisoned;
use std::sync::{Arc, RwLock};
pub struct MemoInner<T> {

View File

@ -4,9 +4,9 @@ use crate::{
executor::Executor,
graph::{AnySubscriber, SourceSet, Subscriber, ToAnySubscriber},
owner::Owner,
OrPoisoned,
};
use futures::StreamExt;
use or_poisoned::OrPoisoned;
use std::{
mem,
sync::{Arc, RwLock},

View File

@ -4,8 +4,8 @@ use crate::{
AnySource, AnySubscriber, ReactiveNode, SourceSet, Subscriber,
ToAnySubscriber,
},
OrPoisoned,
};
use or_poisoned::OrPoisoned;
use std::sync::{Arc, RwLock, Weak};
pub(crate) struct EffectInner {

View File

@ -4,9 +4,9 @@ use crate::{
executor::Executor,
graph::{AnySubscriber, SourceSet, Subscriber, ToAnySubscriber},
owner::Owner,
OrPoisoned,
};
use futures::StreamExt;
use or_poisoned::OrPoisoned;
use std::{
fmt::Debug,
mem,

View File

@ -84,29 +84,3 @@ pub type PinnedStream<T> = Pin<Box<dyn Stream<Item = T> + Send + Sync>>;
pub mod prelude {
pub use crate::traits::*;
}
trait OrPoisoned {
type Inner;
fn or_poisoned(self) -> Self::Inner;
}
impl<'a, T> OrPoisoned
for Result<RwLockReadGuard<'a, T>, PoisonError<RwLockReadGuard<'a, T>>>
{
type Inner = RwLockReadGuard<'a, T>;
fn or_poisoned(self) -> Self::Inner {
self.expect("lock poisoned")
}
}
impl<'a, T> OrPoisoned
for Result<RwLockWriteGuard<'a, T>, PoisonError<RwLockWriteGuard<'a, T>>>
{
type Inner = RwLockWriteGuard<'a, T>;
fn or_poisoned(self) -> Self::Inner {
self.expect("lock poisoned")
}
}

View File

@ -1,4 +1,4 @@
use crate::OrPoisoned;
use or_poisoned::OrPoisoned;
use rustc_hash::FxHashMap;
use std::{
any::{Any, TypeId},

View File

@ -1,5 +1,5 @@
use super::OWNER;
use crate::OrPoisoned;
use or_poisoned::OrPoisoned;
use slotmap::{new_key_type, SlotMap};
use std::{
any::Any,

View File

@ -1,4 +1,5 @@
use crate::{owner::Owner, OrPoisoned};
use crate::owner::Owner;
use or_poisoned::OrPoisoned;
use std::any::{Any, TypeId};
impl Owner {

View File

@ -14,8 +14,9 @@ use crate::{
ToAnySource,
},
traits::DefinedAt,
unwrap_signal, OrPoisoned,
unwrap_signal,
};
use or_poisoned::OrPoisoned;
use std::{
borrow::Borrow,
sync::{Arc, RwLock, Weak},