1#![allow(unsafe_code)]
10
11use core::fmt;
12use core::marker::PhantomData;
13use core::mem::ManuallyDrop;
14use core::ops::{Deref, DerefMut};
15use core::ptr;
16use std::sync::Arc;
17
18use super::RwLock;
19use crate::sync::raw_rwlock::RawRwLock;
20
21#[must_use = "dropping the guard releases the lock at once"]
29pub struct RwLockReadGuard<'a, T: ?Sized> {
30 raw: &'a RawRwLock,
31 data: *const T,
32 marker: PhantomData<&'a T>,
33}
34
35unsafe impl<T: ?Sized + Sync> Send for RwLockReadGuard<'_, T> {}
38unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockReadGuard<'_, T> {}
40
41impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
42 pub(super) fn new(lock: &'a RwLock<T>) -> Self {
43 Self {
44 raw: &lock.raw,
45 data: lock.value.get(),
46 marker: PhantomData,
47 }
48 }
49
50 fn into_parts(self) -> (&'a RawRwLock, *const T) {
53 let spent = ManuallyDrop::new(self);
54 (spent.raw, spent.data)
55 }
56
57 #[inline]
63 pub fn map<F, U>(this: Self, f: F) -> RwLockReadGuard<'a, U>
64 where
65 U: ?Sized,
66 F: FnOnce(&T) -> &U,
67 {
68 let data = ptr::from_ref(f(&*this));
69 let (raw, _) = this.into_parts();
70 RwLockReadGuard {
71 raw,
72 data,
73 marker: PhantomData,
74 }
75 }
76
77 #[inline]
80 pub fn try_map<F, U>(this: Self, f: F) -> Result<RwLockReadGuard<'a, U>, Self>
81 where
82 U: ?Sized,
83 F: FnOnce(&T) -> Option<&U>,
84 {
85 let data = match f(&*this) {
86 Some(data) => ptr::from_ref(data),
87 None => return Err(this),
88 };
89 let (raw, _) = this.into_parts();
90 Ok(RwLockReadGuard {
91 raw,
92 data,
93 marker: PhantomData,
94 })
95 }
96}
97
98impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
99 type Target = T;
100
101 fn deref(&self) -> &T {
102 unsafe { &*self.data }
105 }
106}
107
108impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
109 fn drop(&mut self) {
110 self.raw.read_unlock();
111 }
112}
113
114impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 fmt::Debug::fmt(&**self, f)
117 }
118}
119
120impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 fmt::Display::fmt(&**self, f)
123 }
124}
125
126#[must_use = "dropping the guard releases the lock at once"]
130pub struct OwnedRwLockReadGuard<T: ?Sized, U: ?Sized = T> {
131 lock: Arc<RwLock<T>>,
132 data: *const U,
133 _p: PhantomData<T>,
134}
135
136unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Sync> Send for OwnedRwLockReadGuard<T, U> {}
139unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync for OwnedRwLockReadGuard<T, U> {}
141
142impl<T: ?Sized> OwnedRwLockReadGuard<T> {
143 pub(super) fn new(lock: Arc<RwLock<T>>) -> Self {
144 Self {
145 data: lock.value.get(),
146 lock,
147 _p: PhantomData,
148 }
149 }
150}
151
152impl<T: ?Sized, U: ?Sized> OwnedRwLockReadGuard<T, U> {
153 fn into_parts(self) -> (Arc<RwLock<T>>, *const U) {
156 let spent = ManuallyDrop::new(self);
157 (unsafe { ptr::read(&spent.lock) }, spent.data)
160 }
161
162 #[inline]
168 pub fn map<F, V>(this: Self, f: F) -> OwnedRwLockReadGuard<T, V>
169 where
170 V: ?Sized,
171 F: FnOnce(&U) -> &V,
172 {
173 let data = ptr::from_ref(f(&*this));
174 let (lock, _) = this.into_parts();
175 OwnedRwLockReadGuard {
176 lock,
177 data,
178 _p: PhantomData,
179 }
180 }
181
182 #[inline]
185 pub fn try_map<F, V>(this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, V>, Self>
186 where
187 V: ?Sized,
188 F: FnOnce(&U) -> Option<&V>,
189 {
190 let data = match f(&*this) {
191 Some(data) => ptr::from_ref(data),
192 None => return Err(this),
193 };
194 let (lock, _) = this.into_parts();
195 Ok(OwnedRwLockReadGuard {
196 lock,
197 data,
198 _p: PhantomData,
199 })
200 }
201
202 #[inline]
204 pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
205 &this.lock
206 }
207}
208
209impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockReadGuard<T, U> {
210 type Target = U;
211
212 fn deref(&self) -> &U {
213 unsafe { &*self.data }
216 }
217}
218
219impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockReadGuard<T, U> {
220 fn drop(&mut self) {
221 self.lock.raw.read_unlock();
222 }
223}
224
225impl<T: ?Sized, U: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockReadGuard<T, U> {
226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227 fmt::Debug::fmt(&**self, f)
228 }
229}
230
231impl<T: ?Sized, U: ?Sized + fmt::Display> fmt::Display for OwnedRwLockReadGuard<T, U> {
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233 fmt::Display::fmt(&**self, f)
234 }
235}
236
237#[must_use = "dropping the guard releases the lock at once"]
240pub struct RwLockWriteGuard<'a, T: ?Sized> {
241 raw: &'a RawRwLock,
242 data: *mut T,
243 marker: PhantomData<&'a mut T>,
244}
245
246unsafe impl<T: ?Sized + Send + Sync> Send for RwLockWriteGuard<'_, T> {}
249unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockWriteGuard<'_, T> {}
251
252impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
253 pub(super) fn new(lock: &'a RwLock<T>) -> Self {
254 Self {
255 raw: &lock.raw,
256 data: lock.value.get(),
257 marker: PhantomData,
258 }
259 }
260
261 fn into_parts(self) -> (&'a RawRwLock, *mut T) {
264 let spent = ManuallyDrop::new(self);
265 (spent.raw, spent.data)
266 }
267
268 #[inline]
274 pub fn map<F, U>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
275 where
276 U: ?Sized,
277 F: FnOnce(&mut T) -> &mut U,
278 {
279 let data = ptr::from_mut(f(&mut *this));
280 let (raw, _) = this.into_parts();
281 RwLockMappedWriteGuard {
282 raw,
283 data,
284 marker: PhantomData,
285 }
286 }
287
288 #[inline]
294 pub fn downgrade_map<F, U>(this: Self, f: F) -> RwLockReadGuard<'a, U>
295 where
296 U: ?Sized,
297 F: FnOnce(&T) -> &U,
298 {
299 let data = ptr::from_ref(f(&*this));
300 let (raw, _) = this.into_parts();
301 let guard = RwLockReadGuard {
304 raw,
305 data,
306 marker: PhantomData,
307 };
308 raw.downgrade();
309 guard
310 }
311
312 #[inline]
315 pub fn try_map<F, U>(mut this: Self, f: F) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
316 where
317 U: ?Sized,
318 F: FnOnce(&mut T) -> Option<&mut U>,
319 {
320 let data = match f(&mut *this) {
321 Some(data) => ptr::from_mut(data),
322 None => return Err(this),
323 };
324 let (raw, _) = this.into_parts();
325 Ok(RwLockMappedWriteGuard {
326 raw,
327 data,
328 marker: PhantomData,
329 })
330 }
331
332 #[inline]
336 pub fn try_downgrade_map<F, U>(this: Self, f: F) -> Result<RwLockReadGuard<'a, U>, Self>
337 where
338 U: ?Sized,
339 F: FnOnce(&T) -> Option<&U>,
340 {
341 let data = match f(&*this) {
342 Some(data) => ptr::from_ref(data),
343 None => return Err(this),
344 };
345 let (raw, _) = this.into_parts();
346 let guard = RwLockReadGuard {
347 raw,
348 data,
349 marker: PhantomData,
350 };
351 raw.downgrade();
352 Ok(guard)
353 }
354
355 #[inline]
358 pub fn into_mapped(this: Self) -> RwLockMappedWriteGuard<'a, T> {
359 Self::map(this, |me| me)
360 }
361
362 pub fn downgrade(self) -> RwLockReadGuard<'a, T> {
383 let (raw, data) = self.into_parts();
384 let guard = RwLockReadGuard {
385 raw,
386 data,
387 marker: PhantomData,
388 };
389 raw.downgrade();
390 guard
391 }
392}
393
394impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
395 type Target = T;
396
397 fn deref(&self) -> &T {
398 unsafe { &*self.data }
400 }
401}
402
403impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
404 fn deref_mut(&mut self) -> &mut T {
405 unsafe { &mut *self.data }
407 }
408}
409
410impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
411 fn drop(&mut self) {
412 self.raw.write_unlock();
413 }
414}
415
416impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
417 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
418 fmt::Debug::fmt(&**self, f)
419 }
420}
421
422impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
423 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
424 fmt::Display::fmt(&**self, f)
425 }
426}
427
428#[must_use = "dropping the guard releases the lock at once"]
432pub struct RwLockMappedWriteGuard<'a, T: ?Sized> {
433 raw: &'a RawRwLock,
434 data: *mut T,
435 marker: PhantomData<&'a mut T>,
436}
437
438unsafe impl<T: ?Sized + Send + Sync> Send for RwLockMappedWriteGuard<'_, T> {}
440unsafe impl<T: ?Sized + Send + Sync> Sync for RwLockMappedWriteGuard<'_, T> {}
442
443impl<'a, T: ?Sized> RwLockMappedWriteGuard<'a, T> {
444 fn into_raw(self) -> &'a RawRwLock {
447 ManuallyDrop::new(self).raw
448 }
449
450 #[inline]
456 pub fn map<F, U>(mut this: Self, f: F) -> RwLockMappedWriteGuard<'a, U>
457 where
458 U: ?Sized,
459 F: FnOnce(&mut T) -> &mut U,
460 {
461 let data = ptr::from_mut(f(&mut *this));
462 RwLockMappedWriteGuard {
463 raw: this.into_raw(),
464 data,
465 marker: PhantomData,
466 }
467 }
468
469 #[inline]
472 pub fn try_map<F, U>(mut this: Self, f: F) -> Result<RwLockMappedWriteGuard<'a, U>, Self>
473 where
474 U: ?Sized,
475 F: FnOnce(&mut T) -> Option<&mut U>,
476 {
477 let data = match f(&mut *this) {
478 Some(data) => ptr::from_mut(data),
479 None => return Err(this),
480 };
481 Ok(RwLockMappedWriteGuard {
482 raw: this.into_raw(),
483 data,
484 marker: PhantomData,
485 })
486 }
487}
488
489impl<T: ?Sized> Deref for RwLockMappedWriteGuard<'_, T> {
490 type Target = T;
491
492 fn deref(&self) -> &T {
493 unsafe { &*self.data }
496 }
497}
498
499impl<T: ?Sized> DerefMut for RwLockMappedWriteGuard<'_, T> {
500 fn deref_mut(&mut self) -> &mut T {
501 unsafe { &mut *self.data }
503 }
504}
505
506impl<T: ?Sized> Drop for RwLockMappedWriteGuard<'_, T> {
507 fn drop(&mut self) {
508 self.raw.write_unlock();
509 }
510}
511
512impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLockMappedWriteGuard<'_, T> {
513 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
514 fmt::Debug::fmt(&**self, f)
515 }
516}
517
518impl<T: ?Sized + fmt::Display> fmt::Display for RwLockMappedWriteGuard<'_, T> {
519 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
520 fmt::Display::fmt(&**self, f)
521 }
522}
523
524#[must_use = "dropping the guard releases the lock at once"]
528pub struct OwnedRwLockWriteGuard<T: ?Sized> {
529 lock: Arc<RwLock<T>>,
530 data: *mut T,
531 _p: PhantomData<T>,
532}
533
534unsafe impl<T: ?Sized + Send + Sync> Send for OwnedRwLockWriteGuard<T> {}
537unsafe impl<T: ?Sized + Send + Sync> Sync for OwnedRwLockWriteGuard<T> {}
539
540impl<T: ?Sized> OwnedRwLockWriteGuard<T> {
541 pub(super) fn new(lock: Arc<RwLock<T>>) -> Self {
542 Self {
543 data: lock.value.get(),
544 lock,
545 _p: PhantomData,
546 }
547 }
548
549 fn into_parts(self) -> (Arc<RwLock<T>>, *mut T) {
552 let spent = ManuallyDrop::new(self);
553 (unsafe { ptr::read(&spent.lock) }, spent.data)
556 }
557
558 #[inline]
564 pub fn map<F, U>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, U>
565 where
566 U: ?Sized,
567 F: FnOnce(&mut T) -> &mut U,
568 {
569 let data = ptr::from_mut(f(&mut *this));
570 let (lock, _) = this.into_parts();
571 OwnedRwLockMappedWriteGuard {
572 lock,
573 data,
574 _p: PhantomData,
575 }
576 }
577
578 #[inline]
584 pub fn downgrade_map<F, U>(this: Self, f: F) -> OwnedRwLockReadGuard<T, U>
585 where
586 U: ?Sized,
587 F: FnOnce(&T) -> &U,
588 {
589 let data = ptr::from_ref(f(&*this));
590 let (lock, _) = this.into_parts();
591 let guard = OwnedRwLockReadGuard {
592 lock,
593 data,
594 _p: PhantomData,
595 };
596 guard.lock.raw.downgrade();
597 guard
598 }
599
600 #[inline]
603 pub fn try_map<F, U>(mut this: Self, f: F) -> Result<OwnedRwLockMappedWriteGuard<T, U>, Self>
604 where
605 U: ?Sized,
606 F: FnOnce(&mut T) -> Option<&mut U>,
607 {
608 let data = match f(&mut *this) {
609 Some(data) => ptr::from_mut(data),
610 None => return Err(this),
611 };
612 let (lock, _) = this.into_parts();
613 Ok(OwnedRwLockMappedWriteGuard {
614 lock,
615 data,
616 _p: PhantomData,
617 })
618 }
619
620 #[inline]
624 pub fn try_downgrade_map<F, U>(this: Self, f: F) -> Result<OwnedRwLockReadGuard<T, U>, Self>
625 where
626 U: ?Sized,
627 F: FnOnce(&T) -> Option<&U>,
628 {
629 let data = match f(&*this) {
630 Some(data) => ptr::from_ref(data),
631 None => return Err(this),
632 };
633 let (lock, _) = this.into_parts();
634 let guard = OwnedRwLockReadGuard {
635 lock,
636 data,
637 _p: PhantomData,
638 };
639 guard.lock.raw.downgrade();
640 Ok(guard)
641 }
642
643 #[inline]
647 pub fn into_mapped(this: Self) -> OwnedRwLockMappedWriteGuard<T> {
648 Self::map(this, |me| me)
649 }
650
651 pub fn downgrade(self) -> OwnedRwLockReadGuard<T> {
654 let (lock, data) = self.into_parts();
655 let guard = OwnedRwLockReadGuard {
656 lock,
657 data,
658 _p: PhantomData,
659 };
660 guard.lock.raw.downgrade();
661 guard
662 }
663
664 #[inline]
666 pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
667 &this.lock
668 }
669}
670
671impl<T: ?Sized> Deref for OwnedRwLockWriteGuard<T> {
672 type Target = T;
673
674 fn deref(&self) -> &T {
675 unsafe { &*self.data }
677 }
678}
679
680impl<T: ?Sized> DerefMut for OwnedRwLockWriteGuard<T> {
681 fn deref_mut(&mut self) -> &mut T {
682 unsafe { &mut *self.data }
684 }
685}
686
687impl<T: ?Sized> Drop for OwnedRwLockWriteGuard<T> {
688 fn drop(&mut self) {
689 self.lock.raw.write_unlock();
690 }
691}
692
693impl<T: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockWriteGuard<T> {
694 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
695 fmt::Debug::fmt(&**self, f)
696 }
697}
698
699impl<T: ?Sized + fmt::Display> fmt::Display for OwnedRwLockWriteGuard<T> {
700 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
701 fmt::Display::fmt(&**self, f)
702 }
703}
704
705#[must_use = "dropping the guard releases the lock at once"]
709pub struct OwnedRwLockMappedWriteGuard<T: ?Sized, U: ?Sized = T> {
710 lock: Arc<RwLock<T>>,
711 data: *mut U,
712 _p: PhantomData<T>,
713}
714
715unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Send
717 for OwnedRwLockMappedWriteGuard<T, U>
718{
719}
720unsafe impl<T: ?Sized + Send + Sync, U: ?Sized + Send + Sync> Sync
722 for OwnedRwLockMappedWriteGuard<T, U>
723{
724}
725
726impl<T: ?Sized, U: ?Sized> OwnedRwLockMappedWriteGuard<T, U> {
727 fn into_lock(self) -> Arc<RwLock<T>> {
730 let spent = ManuallyDrop::new(self);
731 unsafe { ptr::read(&spent.lock) }
734 }
735
736 #[inline]
742 pub fn map<V, F>(mut this: Self, f: F) -> OwnedRwLockMappedWriteGuard<T, V>
743 where
744 V: ?Sized,
745 F: FnOnce(&mut U) -> &mut V,
746 {
747 let data = ptr::from_mut(f(&mut *this));
748 OwnedRwLockMappedWriteGuard {
749 lock: this.into_lock(),
750 data,
751 _p: PhantomData,
752 }
753 }
754
755 #[inline]
758 pub fn try_map<V, F>(mut this: Self, f: F) -> Result<OwnedRwLockMappedWriteGuard<T, V>, Self>
759 where
760 V: ?Sized,
761 F: FnOnce(&mut U) -> Option<&mut V>,
762 {
763 let data = match f(&mut *this) {
764 Some(data) => ptr::from_mut(data),
765 None => return Err(this),
766 };
767 Ok(OwnedRwLockMappedWriteGuard {
768 lock: this.into_lock(),
769 data,
770 _p: PhantomData,
771 })
772 }
773
774 #[inline]
776 pub fn rwlock(this: &Self) -> &Arc<RwLock<T>> {
777 &this.lock
778 }
779}
780
781impl<T: ?Sized, U: ?Sized> Deref for OwnedRwLockMappedWriteGuard<T, U> {
782 type Target = U;
783
784 fn deref(&self) -> &U {
785 unsafe { &*self.data }
788 }
789}
790
791impl<T: ?Sized, U: ?Sized> DerefMut for OwnedRwLockMappedWriteGuard<T, U> {
792 fn deref_mut(&mut self) -> &mut U {
793 unsafe { &mut *self.data }
795 }
796}
797
798impl<T: ?Sized, U: ?Sized> Drop for OwnedRwLockMappedWriteGuard<T, U> {
799 fn drop(&mut self) {
800 self.lock.raw.write_unlock();
801 }
802}
803
804impl<T: ?Sized, U: ?Sized + fmt::Debug> fmt::Debug for OwnedRwLockMappedWriteGuard<T, U> {
805 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
806 fmt::Debug::fmt(&**self, f)
807 }
808}
809
810impl<T: ?Sized, U: ?Sized + fmt::Display> fmt::Display for OwnedRwLockMappedWriteGuard<T, U> {
811 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
812 fmt::Display::fmt(&**self, f)
813 }
814}