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 = [ members = [
# utilities # utilities
"oco", "oco",
"or_poisoned",
# core # core
"leptos", "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 version.workspace = true
[dependencies] [dependencies]
or_poisoned = { workspace = true }
futures = "0.3" futures = "0.3"
glib = { version = "0.18", optional = true } glib = { version = "0.18", optional = true }
pin-project-lite = "0.2.13" pin-project-lite = "0.2.13"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -84,29 +84,3 @@ pub type PinnedStream<T> = Pin<Box<dyn Stream<Item = T> + Send + Sync>>;
pub mod prelude { pub mod prelude {
pub use crate::traits::*; 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 rustc_hash::FxHashMap;
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},

View File

@ -1,5 +1,5 @@
use super::OWNER; use super::OWNER;
use crate::OrPoisoned; use or_poisoned::OrPoisoned;
use slotmap::{new_key_type, SlotMap}; use slotmap::{new_key_type, SlotMap};
use std::{ use std::{
any::Any, 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}; use std::any::{Any, TypeId};
impl Owner { impl Owner {

View File

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