Skip to content

Commit

Permalink
sync: Implement map methods of parking_lot fame
Browse files Browse the repository at this point in the history
I've introduced `MappedRwLockReadGuard` and `MappedRwLockWriteGuard`,
which are produced by the relevant `map` and `try_map` methods.

Generally, this mimics the way `MappedRwLock*Guard`s are implemented in
`parking_lot`. By storing a raw pointer in the guards themselves
referencing the mapped data and maintaining type invariants through
`PhantomData`. I didn't try to think too much about this, so if someone
has objections I'd love to hear them.

I've also dropped the internal use of `ReleasingPermit`, since it made
the guards unecessarily large. The number of permits that need to be
released are already known by the guards themselves, and is instead
governed directly in the relevant `Drop` impls.  This has the benefit of
making the guards as small as possible, for the non-mapped variants this
means a single reference is enough.

`fmt::Debug` impls have been adjusted to behave exactly like the
delegating impls in `parking_lot`. `fmt::Display` impls have been added
for all guard types which behave the same. This does change the format
of debug impls, for which I'm not sure if we provide any guarantees.
  • Loading branch information
udoprog committed Jul 9, 2020
1 parent 2aa8751 commit bae2a6a
Show file tree
Hide file tree
Showing 2 changed files with 618 additions and 32 deletions.
2 changes: 1 addition & 1 deletion tokio/src/sync/mod.rs
Expand Up @@ -455,7 +455,7 @@ cfg_sync! {
pub use semaphore::{Semaphore, SemaphorePermit, OwnedSemaphorePermit};

mod rwlock;
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard};

mod task;
pub(crate) use task::AtomicWaker;
Expand Down

0 comments on commit bae2a6a

Please sign in to comment.