Skip to content

Commit

Permalink
Merge pull request #25 from SrTobi-Forks/tobias/relax-atomic-ref
Browse files Browse the repository at this point in the history
relax lifetime constraints on AtomicRef
  • Loading branch information
bholley authored Oct 10, 2023
2 parents b5c9671 + 72f52f4 commit 77d1519
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "Threadsafe RefCell"
license = "Apache-2.0/MIT"
repository = "https://github.com/bholley/atomic_refcell"
documentation = "https://docs.rs/atomic_refcell/"
edition = "2018"

[dependencies]
serde = { version = "1.0", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ pub struct AtomicRef<'b, T: ?Sized + 'b> {

// SAFETY: `AtomicRef<'_, T> acts as a reference. `AtomicBorrowRef` is a
// reference to an atomic.
unsafe impl<'b, T: ?Sized + 'b> Sync for AtomicRef<'b, T> where for<'a> &'a T: Sync {}
unsafe impl<'b, T: ?Sized + 'b> Send for AtomicRef<'b, T> where for<'a> &'a T: Send {}
unsafe impl<'b, T: ?Sized> Sync for AtomicRef<'b, T> where for<'a> &'a T: Sync {}
unsafe impl<'b, T: ?Sized> Send for AtomicRef<'b, T> where for<'a> &'a T: Send {}

impl<'b, T: ?Sized> Deref for AtomicRef<'b, T> {
type Target = T;
Expand Down Expand Up @@ -472,8 +472,8 @@ pub struct AtomicRefMut<'b, T: ?Sized + 'b> {

// SAFETY: `AtomicRefMut<'_, T> acts as a mutable reference.
// `AtomicBorrowRefMut` is a reference to an atomic.
unsafe impl<'b, T: ?Sized + 'b> Sync for AtomicRefMut<'b, T> where for<'a> &'a mut T: Sync {}
unsafe impl<'b, T: ?Sized + 'b> Send for AtomicRefMut<'b, T> where for<'a> &'a mut T: Send {}
unsafe impl<'b, T: ?Sized> Sync for AtomicRefMut<'b, T> where for<'a> &'a mut T: Sync {}
unsafe impl<'b, T: ?Sized> Send for AtomicRefMut<'b, T> where for<'a> &'a mut T: Send {}

impl<'b, T: ?Sized> Deref for AtomicRefMut<'b, T> {
type Target = T;
Expand Down
34 changes: 34 additions & 0 deletions tests/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::future::Future;

use atomic_refcell::AtomicRefCell;

fn spawn<Fut>(_future: Fut)
where
Fut: Future<Output = ()> + Send + Sync,
{
}

async fn something_async() {}

// see https://github.com/bholley/atomic_refcell/issues/24
#[test]
fn test_atomic_ref_in_spawn() {
let arc: Box<dyn Fn() -> usize + Send + Sync> = Box::new(|| 42);
let a = AtomicRefCell::new(arc);
spawn(async move {
let x = a.borrow();
something_async().await;
assert_eq!(x(), 42);
});
}

#[test]
fn test_atomic_ref_mut_in_spawn() {
let arc: Box<dyn Fn() -> usize + Send + Sync> = Box::new(|| 42);
let a = AtomicRefCell::new(arc);
spawn(async move {
let x = a.borrow_mut();
something_async().await;
assert_eq!(x(), 42);
});
}

0 comments on commit 77d1519

Please sign in to comment.