Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MappedAtomicRef(Mut) #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ impl<'b, T: ?Sized> AtomicRef<'b, T> {
borrow: orig.borrow,
})
}

/// Make a new `MappedAtomicRef` using the borrowed data.
#[inline]
pub fn map_into<U, F>(orig: AtomicRef<'b, T>, f: F) -> MappedAtomicRef<'b, U>
where
F: FnOnce(&'b T) -> U,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'b is the lifetime of the shared borrow of the AtomicRefCell (and not actually the lifetime that &T is valid for which is up until the AtomicRef/MappedAtomicRef is dropped).

Unfortunately, as noted here rust-lang/rust#70263 (comment), that means this API allows for UB.

E.g. the following test compiles:

#[test]
fn owned_map() {
    use atomic_refcell::*;

    let cell = AtomicRefCell::new(3u8);
    let mut other = &mut 0u8;

    let guard = AtomicRefMut::map_into(cell.borrow_mut(), |inner: &mut u8| {
        // sneak reference out
        other = inner;
    });
    // guard dropped but reference still accessible via `other`
    drop(guard);

    assert_eq!(*other, 3);

    *cell.borrow_mut() = 1;

    assert_eq!(*other, 1);
}

There are some potential workarounds noted in the comment I linked, but they can make things difficult to work with.

{
MappedAtomicRef {
value: f(orig.value),
borrow: orig.borrow,
}
}
}

impl<'b, T: ?Sized> AtomicRefMut<'b, T> {
Expand Down Expand Up @@ -443,6 +455,18 @@ impl<'b, T: ?Sized> AtomicRefMut<'b, T> {
borrow: orig.borrow,
})
}

/// Make a new `MappedAtomicRefMut` using the borrowed data.
#[inline]
pub fn map_into<U, F>(orig: AtomicRefMut<'b, T>, f: F) -> MappedAtomicRefMut<'b, U>
where
F: FnOnce(&'b mut T) -> U,
{
MappedAtomicRefMut {
value: f(orig.value),
borrow: orig.borrow,
}
}
}

/// A wrapper type for a mutably borrowed value from an `AtomicRefCell<T>`.
Expand All @@ -467,6 +491,71 @@ impl<'b, T: ?Sized> DerefMut for AtomicRefMut<'b, T> {
}
}

/// A wraper type for data derived from an immutably borrowed value from an `AtomicRefCell`.
pub struct MappedAtomicRef<'b, T> {
value: T,
borrow: AtomicBorrowRef<'b>,
}

impl<'b, T> Deref for MappedAtomicRef<'b, T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}

impl<'b, T> MappedAtomicRef<'b, T> {
/// Make a new `MappedAtomicRef<T>` using the encapsulated data.
#[inline]
pub fn map_into<U, F>(orig: MappedAtomicRef<'b, T>, f: F) -> MappedAtomicRef<'b, U>
where
F: FnOnce(T) -> U,
{
MappedAtomicRef {
value: f(orig.value),
borrow: orig.borrow,
}
}
}

/// A wraper type for data derived from a mutably borrowed value from an `AtomicRefCell`.
pub struct MappedAtomicRefMut<'b, T> {
value: T,
borrow: AtomicBorrowRefMut<'b>,
}

impl<'b, T> Deref for MappedAtomicRefMut<'b, T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}

impl<'b, T> DerefMut for MappedAtomicRefMut<'b, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}

impl<'b, T> MappedAtomicRefMut<'b, T> {
/// Make a new `MappedAtomicRefMut<T>` using the encapsulated data.
#[inline]
pub fn map_into<U, F>(orig: MappedAtomicRefMut<'b, T>, f: F) -> MappedAtomicRefMut<'b, U>
where
F: FnOnce(T) -> U,
{
MappedAtomicRefMut {
value: f(orig.value),
borrow: orig.borrow,
}
}
}

impl<'b, T: ?Sized + Debug + 'b> Debug for AtomicRef<'b, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
Expand All @@ -479,6 +568,18 @@ impl<'b, T: ?Sized + Debug + 'b> Debug for AtomicRefMut<'b, T> {
}
}

impl<'b, T: Debug + 'b> Debug for MappedAtomicRef<'b, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}

impl<'b, T: Debug + 'b> Debug for MappedAtomicRefMut<'b, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}

impl<T: ?Sized + Debug> Debug for AtomicRefCell<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AtomicRefCell {{ ... }}")
Expand Down