Skip to content

Commit

Permalink
Merge pull request #70 from earthstar-project/shared_consumer
Browse files Browse the repository at this point in the history
Add SharedConsumer
  • Loading branch information
AljoschaMeyer authored Jan 6, 2025
2 parents 9337925 + c36c3e2 commit b95c32a
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 34 deletions.
9 changes: 7 additions & 2 deletions wb_async_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ pub mod spsc;
#[cfg(feature = "ufotofu_utils")]
pub mod shared_producer;

#[cfg(feature = "ufotofu_utils")]
pub mod shared_consumer;

// This is safe if and only if the object pointed at by `reference` lives for at least `'longer`.
// See https://doc.rust-lang.org/nightly/std/intrinsics/fn.transmute.html for more detail.
pub(crate) unsafe fn extend_lifetime<'shorter, 'longer, T: ?Sized>(reference: &'shorter T) -> &'longer T {
pub(crate) unsafe fn extend_lifetime<'shorter, 'longer, T: ?Sized>(
reference: &'shorter T,
) -> &'longer T {
core::mem::transmute::<&'shorter T, &'longer T>(reference)
}

Expand All @@ -28,4 +33,4 @@ pub(crate) unsafe fn extend_lifetime_mut<'shorter, 'longer, T: ?Sized>(
reference: &'shorter mut T,
) -> &'longer mut T {
core::mem::transmute::<&'shorter mut T, &'longer mut T>(reference)
}
}
14 changes: 7 additions & 7 deletions wb_async_utils/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Mutex<T> {

impl<T> Mutex<T> {
/// Creates a new mutex storing the given value.
///
///
/// ```
/// use wb_async_utils::mutex::*;
///
Expand Down Expand Up @@ -384,8 +384,8 @@ impl<T> Deref for ReadGuard<'_, T> {

fn deref(&self) -> &T {
let borrowed = unsafe { self.mutex.value.borrow() }; // Safe because a ReadGuard can never live at the same time as another guard or a `&mut Mutex`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
unsafe { extend_lifetime(borrowed.deref()) }
}
}
Expand Down Expand Up @@ -430,17 +430,17 @@ impl<T> Deref for WriteGuard<'_, T> {

fn deref(&self) -> &T {
let borrowed = unsafe { self.mutex.value.borrow() }; // Safe because a WriteGuard can never live at the same time as another guard or a `&mut Mutex`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
unsafe { extend_lifetime(borrowed.deref()) }
}
}

impl<T> DerefMut for WriteGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
let mut borrowed = unsafe { self.mutex.value.borrow_mut() }; // Safe because a WriteGuard can never live at the same time as another or a `&mut Mutex`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
// We can only obtain references with a lifetime tied to `borrowed`, but we know the refs to be both alive and exclusive for as long
// as `self`.
unsafe { extend_lifetime_mut(borrowed.deref_mut()) }
}
}
Expand Down
Loading

0 comments on commit b95c32a

Please # to comment.