From ad52c285bfdaef3d0ff87627fcb7b94aa4a12ac9 Mon Sep 17 00:00:00 2001 From: Tudor Lechintan Date: Sat, 8 Jan 2022 18:19:50 +0200 Subject: [PATCH] Add MappedAtomicRef(Mut) --- src/lib.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index dbf599a..c3144e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(orig: AtomicRef<'b, T>, f: F) -> MappedAtomicRef<'b, U> + where + F: FnOnce(&'b T) -> U, + { + MappedAtomicRef { + value: f(orig.value), + borrow: orig.borrow, + } + } } impl<'b, T: ?Sized> AtomicRefMut<'b, T> { @@ -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(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`. @@ -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` using the encapsulated data. + #[inline] + pub fn map_into(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` using the encapsulated data. + #[inline] + pub fn map_into(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) @@ -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 Debug for AtomicRefCell { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "AtomicRefCell {{ ... }}")