contiguous-mem 0.4.2

A contiguous memory storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Returned reference types and read/write guards.
//!
//! See [`ContiguousMemoryStorage::push`](crate::ContiguousMemoryStorage::push)
//! for information on implementation specific return values.

use core::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

use crate::{
    details::{ImplConcurrent, ImplDefault, ImplDetails, StorageDetails},
    error::{LockSource, LockingError, RegionBorrowedError},
    range::ByteRange,
    types::*,
};

/// A synchronized (thread-safe) reference to `T` data stored in a
/// [`ContiguousMemoryStorage`](crate::ContiguousMemoryStorage) structure.
pub struct SyncContiguousEntryRef<T: ?Sized> {
    pub(crate) inner: Arc<ReferenceState<T, ImplConcurrent>>,
    #[cfg(feature = "ptr_metadata")]
    pub(crate) metadata: <T as Pointee>::Metadata,
    #[cfg(not(feature = "ptr_metadata"))]
    pub(crate) _phantom: PhantomData<T>,
}

/// A shorter type name for [`SyncContiguousEntryRef`].
pub type SCERef<T> = SyncContiguousEntryRef<T>;

impl<T: ?Sized> SyncContiguousEntryRef<T> {
    /// Returns a byte range within container memory this reference points to.
    pub fn range(&self) -> ByteRange {
        self.inner.range
    }

    /// Returns a reference to data at its current location or returns a
    /// [`LockingError::Poisoned`](crate::error::LockingError::Poisoned) error
    /// if the Mutex holding the `base` address pointer has been poisoned.
    ///
    /// If the data is mutably accessed, this method will block the current
    /// thread until it becomes available.
    pub fn get(&self) -> Result<MemoryReadGuard<'_, T, ImplConcurrent>, LockingError>
    where
        T: RefSizeReq,
    {
        let guard = self.inner.borrow_kind.read_named(LockSource::Reference)?;

        unsafe {
            let base = ImplConcurrent::get_base(&self.inner.state.base)?;
            let pos = base.add(self.inner.range.0);

            Ok(MemoryReadGuard {
                state: self.inner.clone(),
                guard,
                #[cfg(not(feature = "ptr_metadata"))]
                value: &*(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &*core::ptr::from_raw_parts(pos as *const (), self.metadata),
            })
        }
    }

    /// Returns a reference to data at its current location or returns a
    /// [`LockingError::Poisoned`](crate::error::LockingError::Poisoned) error
    /// if the Mutex holding the `base` address pointer has been poisoned.
    ///
    /// If the data is mutably accessed, this method returns a
    /// [`LockingError::WouldBlock`](crate::error::LockingError::WouldBlock)
    /// error.
    pub fn try_get(&self) -> Result<MemoryReadGuard<'_, T, ImplConcurrent>, LockingError>
    where
        T: RefSizeReq,
    {
        let guard = self
            .inner
            .borrow_kind
            .try_read_named(LockSource::Reference)?;

        unsafe {
            let base = ImplConcurrent::get_base(&self.inner.state.base)?;
            let pos = base.add(self.inner.range.0);

            Ok(MemoryReadGuard {
                state: self.inner.clone(),
                guard,
                #[cfg(not(feature = "ptr_metadata"))]
                value: &*(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &*core::ptr::from_raw_parts(pos as *const (), self.metadata),
            })
        }
    }

    /// Returns or write guard to referenced data at its current location a
    /// [`LockingError::Poisoned`] error if the Mutex holding the base address
    /// pointer or the Mutex holding concurrent mutable access flag has been
    /// poisoned.
    pub fn get_mut(&mut self) -> Result<MemoryWriteGuard<'_, T, ImplConcurrent>, LockingError>
    where
        T: RefSizeReq,
    {
        let guard = self.inner.borrow_kind.write_named(LockSource::Reference)?;
        unsafe {
            let base = ImplConcurrent::get_base(&self.inner.state.base)?;
            let pos = base.add(self.inner.range.0);
            Ok(MemoryWriteGuard {
                state: self.inner.clone(),
                guard,
                #[cfg(not(feature = "ptr_metadata"))]
                value: &mut *(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &mut *core::ptr::from_raw_parts_mut::<T>(pos as *mut (), self.metadata),
            })
        }
    }

    /// Returns a write guard to referenced data at its current location or a
    /// `LockingError` if that isn't possible.
    ///
    /// # Errors
    ///
    /// This function can return the following errors:
    ///
    /// - [`LockingError::Poisoned`] error if the Mutex holding the base address
    ///   pointer or the Mutex holding mutable access exclusion flag has been
    ///   poisoned.
    ///
    /// - [`LockingError::WouldBlock`] error if accessing referenced data chunk
    ///   would be blocking.
    pub fn try_get_mut(&mut self) -> Result<MemoryWriteGuard<'_, T, ImplConcurrent>, LockingError>
    where
        T: RefSizeReq,
    {
        let guard = self
            .inner
            .borrow_kind
            .try_write_named(LockSource::Reference)?;
        unsafe {
            let base = ImplConcurrent::try_get_base(&self.inner.state.base)?;
            let pos = base.add(self.inner.range.0);
            Ok(MemoryWriteGuard {
                state: self.inner.clone(),
                guard,
                #[cfg(not(feature = "ptr_metadata"))]
                value: &mut *(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &mut *core::ptr::from_raw_parts_mut::<T>(pos as *mut (), self.metadata),
            })
        }
    }

    /// Casts this reference into a dynamic type `R`.
    #[cfg(feature = "ptr_metadata")]
    pub fn into_dyn<R: ?Sized>(self) -> SyncContiguousEntryRef<R>
    where
        T: Sized + Unsize<R>,
    {
        unsafe {
            SyncContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata: static_metadata::<T, R>(),
            }
        }
    }

    /// Tries downcasting this dynamic reference into a discrete type `R`,
    /// returns None if `R` drop handler doesn't match the original one.
    #[cfg(feature = "ptr_metadata")]
    pub fn downcast_dyn<R: Unsize<T>>(self) -> Option<SyncContiguousEntryRef<R>> {
        if self.inner.drop_fn != drop_fn::<R>() {
            return None;
        }
        unsafe {
            Some(SyncContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata: (),
            })
        }
    }

    /// Transmutes this reference to type `R` with provided `metadata`.
    ///
    /// [`static_metadata`](crate::static_metadata) function may be used to
    /// statically construct metadata for a struct-trait pair.
    ///
    /// # Safety
    ///
    /// See: [`ContiguousEntryRef::with_metadata`]
    #[cfg(feature = "ptr_metadata")]
    pub unsafe fn with_metadata<R: ?Sized>(
        self,
        metadata: <R as Pointee>::Metadata,
    ) -> ContiguousEntryRef<R> {
        unsafe {
            ContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata,
            }
        }
    }

    /// Creates an immutable pointer to underlying data, blocking the current
    /// thread until base address can be read.
    ///
    /// This function can return a [`LockingError::Poisoned`] error if the Mutex
    /// holding the base address pointer has been poisoned.
    ///
    /// # Safety
    ///
    /// See: [`ContiguousEntryRef::as_ptr`]
    pub unsafe fn as_ptr(&self) -> Result<*const T, LockingError>
    where
        T: RefSizeReq,
    {
        self.as_ptr_mut().map(|it| it as *const T)
    }

    /// Creates a mutable pointer to underlying data, blocking the current
    /// thread until base address can be read.
    ///
    /// This function can return a [`LockingError::Poisoned`] error if the Mutex
    /// holding the base address pointer has been poisoned.
    ///
    /// # Safety
    ///
    /// See: [`ContiguousEntryRef::as_ptr_mut`]
    pub unsafe fn as_ptr_mut(&self) -> Result<*mut T, LockingError>
    where
        T: RefSizeReq,
    {
        let base = ImplConcurrent::get_base(&self.inner.state.base)?;
        let pos = base.add(self.inner.range.0);
        #[cfg(not(feature = "ptr_metadata"))]
        {
            Ok(pos as *mut T)
        }
        #[cfg(feature = "ptr_metadata")]
        {
            Ok(core::ptr::from_raw_parts_mut::<T>(
                pos as *mut (),
                self.metadata,
            ))
        }
    }

    /// Creates an immutable pointer to underlying data while also preventing
    /// the occupied memory region from being marked as free, blocking the
    /// current thread until base address can be read
    ///
    /// This function can return a [`LockingError::Poisoned`] error if the Mutex
    /// holding the base address pointer has been poisoned.
    ///
    /// # Safety
    ///
    /// See: [`ContiguousEntryRef::into_ptr`]
    pub unsafe fn into_ptr(self) -> Result<*const T, LockingError>
    where
        T: RefSizeReq,
    {
        self.into_ptr_mut().map(|it| it as *const T)
    }

    /// Creates a mutable pointer to underlying data while also preventing
    /// the occupied memory region from being marked as free, blocking the
    /// current thread until base address can be read
    ///
    /// This function can return a [`LockingError::Poisoned`] error if the Mutex
    /// holding the base address pointer has been poisoned.
    ///
    /// # Safety
    ///
    /// See: [`ContiguousEntryRef::into_ptr_mut`]
    pub unsafe fn into_ptr_mut(self) -> Result<*mut T, LockingError>
    where
        T: RefSizeReq,
    {
        let result = self.as_ptr_mut();
        let inner: *mut ReferenceState<T, ImplConcurrent> = self.inner.as_ref()
            as *const ReferenceState<T, ImplConcurrent>
            as *mut ReferenceState<T, ImplConcurrent>;
        core::ptr::drop_in_place(&mut (*inner).state);
        core::mem::forget(self.inner);
        result
    }
}

impl<T: ?Sized> EntryRef for SyncContiguousEntryRef<T> {}

impl<T: ?Sized> Clone for SyncContiguousEntryRef<T> {
    fn clone(&self) -> Self {
        SyncContiguousEntryRef {
            inner: self.inner.clone(),
            #[cfg(feature = "ptr_metadata")]
            metadata: self.metadata,
            #[cfg(not(feature = "ptr_metadata"))]
            _phantom: PhantomData,
        }
    }
}

#[cfg(feature = "debug")]
impl<T: ?Sized> core::fmt::Debug for SyncContiguousEntryRef<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SyncContiguousEntryRef")
            .field("inner", &self.inner)
            .finish()
    }
}

/// A thread-unsafe reference to `T` data stored in
/// [`ContiguousMemoryStorage`](crate::ContiguousMemoryStorage) structure.
pub struct ContiguousEntryRef<T: ?Sized> {
    pub(crate) inner: Rc<ReferenceState<T, ImplDefault>>,
    #[cfg(feature = "ptr_metadata")]
    pub(crate) metadata: <T as Pointee>::Metadata,
    #[cfg(not(feature = "ptr_metadata"))]
    pub(crate) _phantom: PhantomData<T>,
}

/// A shorter type name for [`ContiguousEntryRef`].
pub type CERef<T> = ContiguousEntryRef<T>;

impl<T: ?Sized> ContiguousEntryRef<T> {
    /// Returns a byte range within container memory this reference points to.
    pub fn range(&self) -> ByteRange {
        self.inner.range
    }

    /// Returns a reference to data at its current location and panics if the
    /// represented memory region is mutably borrowed.
    pub fn get(&self) -> MemoryReadGuard<'_, T, ImplDefault>
    where
        T: RefSizeReq,
    {
        ContiguousEntryRef::<T>::try_get(self).expect("mutably borrowed")
    }

    /// Returns a reference to data at its current location or a
    /// [`RegionBorrowedError`] error if the represented memory region is
    /// mutably borrowed.
    pub fn try_get(&self) -> Result<MemoryReadGuard<'_, T, ImplDefault>, RegionBorrowedError>
    where
        T: RefSizeReq,
    {
        let state = self.inner.borrow_kind.get();
        if let BorrowState::Read(count) = state {
            self.inner.borrow_kind.set(BorrowState::Read(count + 1));
        } else {
            return Err(RegionBorrowedError {
                range: self.inner.range,
            });
        }

        unsafe {
            let base = ImplDefault::get_base(&self.inner.state.base);
            let pos = base.add(self.inner.range.0);

            Ok(MemoryReadGuard {
                state: self.inner.clone(),
                guard: (),
                #[cfg(not(feature = "ptr_metadata"))]
                value: &*(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &*core::ptr::from_raw_parts_mut::<T>(pos as *mut (), self.metadata),
            })
        }
    }

    /// Returns a mutable reference to data at its current location and panics
    /// if the reference has already been borrowed.
    pub fn get_mut(&mut self) -> MemoryWriteGuard<'_, T, ImplDefault>
    where
        T: RefSizeReq,
    {
        ContiguousEntryRef::<T>::try_get_mut(self).expect("mutably borrowed")
    }

    /// Returns a mutable reference to data at its current location or a
    /// [`RegionBorrowedError`] error if the represented memory region is
    /// already borrowed.
    pub fn try_get_mut(
        &mut self,
    ) -> Result<MemoryWriteGuard<'_, T, ImplDefault>, RegionBorrowedError>
    where
        T: RefSizeReq,
    {
        if self.inner.borrow_kind.get() != BorrowState::Read(0) {
            return Err(RegionBorrowedError {
                range: self.inner.range,
            });
        } else {
            self.inner.borrow_kind.set(BorrowState::Write);
        }

        unsafe {
            let base = ImplDefault::get_base(&self.inner.state.base);
            let pos = base.add(self.inner.range.0);

            Ok(MemoryWriteGuard {
                state: self.inner.clone(),
                guard: (),
                #[cfg(not(feature = "ptr_metadata"))]
                value: &mut *(pos as *mut T),
                #[cfg(feature = "ptr_metadata")]
                value: &mut *core::ptr::from_raw_parts_mut::<T>(pos as *mut (), self.metadata),
            })
        }
    }

    /// Casts this reference into a dynamic type `R`.
    #[cfg(feature = "ptr_metadata")]
    pub fn into_dyn<R: ?Sized>(self) -> ContiguousEntryRef<R>
    where
        T: Sized + Unsize<R>,
    {
        unsafe {
            ContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata: static_metadata::<T, R>(),
            }
        }
    }

    /// Tries downcasting this dynamic reference into a discrete type `R`,
    /// returns None if `R` drop handler doesn't match the original one.
    #[cfg(feature = "ptr_metadata")]
    pub fn downcast_dyn<R: Unsize<T>>(self) -> Option<ContiguousEntryRef<R>> {
        if self.inner.drop_fn != drop_fn::<R>() {
            return None;
        }
        unsafe {
            Some(ContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata: (),
            })
        }
    }

    /// Transmutes this reference to type `R` with provided `metadata`.
    ///
    /// [`static_metadata`](crate::static_metadata) function may be used to
    /// statically construct metadata for a struct-trait pair.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it assumes any `T` to implement `R`,
    /// as the original type of stored data can be erased through
    /// [`into_dyn`](ContiguousEntryRef::into_dyn) it's impossible to check
    /// whether the initial struct actually implements `R`.
    ///
    /// Calling methods from an incorrect vtable will cause undefined behavior.
    #[cfg(feature = "ptr_metadata")]
    pub unsafe fn with_metadata<R: ?Sized>(
        self,
        metadata: <R as Pointee>::Metadata,
    ) -> ContiguousEntryRef<R> {
        unsafe {
            ContiguousEntryRef {
                inner: core::mem::transmute(self.inner),
                metadata,
            }
        }
    }

    /// Creates an immutable pointer to underlying data.
    ///
    /// # Safety
    ///
    /// This function returns a pointer that may become invalid if the
    /// container's memory is resized to a capacity which requires the memory
    /// segment to be moved.
    ///
    /// When the reference goes out of scope, its region will be marked as free
    /// which means that a subsequent call to [`ContiguousMemoryStorage::push`]
    /// or friends can cause undefined behavior when dereferencing the pointer.
    ///
    /// [`ContiguousMemoryStorage::push`]: crate::ContiguousMemoryStorage::push
    pub unsafe fn as_ptr(&self) -> *const T
    where
        T: RefSizeReq,
    {
        self.as_ptr_mut() as *const T
    }

    /// Creates a mutable pointer to underlying data.
    ///
    /// # Safety
    ///
    /// In addition to concerns noted in [`ContiguousEntryRef::as_ptr`],
    /// this function also provides mutable access to the underlying data
    /// allowing potential data races.
    pub unsafe fn as_ptr_mut(&self) -> *mut T
    where
        T: RefSizeReq,
    {
        let base = ImplDefault::get_base(&self.inner.state.base);
        let pos = base.add(self.inner.range.0);

        #[cfg(not(feature = "ptr_metadata"))]
        {
            pos as *mut T
        }
        #[cfg(feature = "ptr_metadata")]
        {
            core::ptr::from_raw_parts_mut::<T>(pos as *mut (), self.metadata)
        }
    }

    /// Creates an immutable pointer to underlying data while also preventing
    /// the occupied memory region from being marked as free.
    ///
    /// # Safety
    ///
    /// This function returns a pointer that may become invalid if the
    /// container's memory is resized to a capacity which requires the memory
    /// segment to be moved.
    pub unsafe fn into_ptr(self) -> *const T
    where
        T: RefSizeReq,
    {
        self.into_ptr_mut() as *const T
    }

    /// Creates a mutable pointer to underlying data while also preventing
    /// the occupied memory region from being marked as free.
    ///
    /// # Safety
    ///
    /// In addition to concerns noted in
    /// [`ContiguousEntryRef::into_ptr`], this function also provides
    /// mutable access to the underlying data allowing potential data races.
    pub unsafe fn into_ptr_mut(self) -> *mut T
    where
        T: RefSizeReq,
    {
        let result = self.as_ptr_mut();
        let inner: *mut ReferenceState<T, ImplDefault> = self.inner.as_ref()
            as *const ReferenceState<T, ImplDefault>
            as *mut ReferenceState<T, ImplDefault>;
        core::ptr::drop_in_place(&mut (*inner).state);
        core::mem::forget(self.inner);
        result
    }
}

impl<T: ?Sized> EntryRef for ContiguousEntryRef<T> {}

impl<T: ?Sized> Clone for ContiguousEntryRef<T> {
    fn clone(&self) -> Self {
        ContiguousEntryRef {
            inner: self.inner.clone(),
            #[cfg(feature = "ptr_metadata")]
            metadata: self.metadata,
            #[cfg(not(feature = "ptr_metadata"))]
            _phantom: PhantomData,
        }
    }
}

#[cfg(feature = "debug")]
impl<T: ?Sized> core::fmt::Debug for ContiguousEntryRef<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ContiguousEntryRef")
            .field("inner", &self.inner)
            .finish()
    }
}

pub(crate) mod sealed {
    use super::*;

    pub trait EntryRef {}

    /// Internal state of [`ContiguousEntryRef`] and [`SyncContiguousEntryRef`].
    pub struct ReferenceState<T: ?Sized, Impl: ImplDetails> {
        pub state: Impl::StorageState,
        pub range: ByteRange,
        pub borrow_kind: Impl::BorrowLock,
        pub drop_fn: DropFn,
        pub _phantom: PhantomData<T>,
    }

    #[cfg(feature = "debug")]
    impl<T: ?Sized, Impl: ImplDetails> core::fmt::Debug for ReferenceState<T, Impl>
    where
        Impl::StorageState: core::fmt::Debug,
        Impl::BorrowLock: core::fmt::Debug,
    {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            f.debug_struct("ReferenceState")
                .field("state", &self.state)
                .field("range", &self.range)
                .field("borrow_kind", &self.borrow_kind)
                .finish()
        }
    }

    impl<T: ?Sized, Impl: ImplDetails> Drop for ReferenceState<T, Impl> {
        fn drop(&mut self) {
            let base = Impl::get_base(&Impl::deref_state(&self.state).base);
            let tracker = Impl::get_allocation_tracker(&mut self.state);
            if let Some(it) = Impl::free_region(tracker, base, self.range) {
                (self.drop_fn)(it);
            };
        }
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum BorrowKind {
        Read,
        Write,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum BorrowState {
        Read(usize),
        Write,
    }
}
use sealed::*;

/// A smart reference wrapper responsible for tracking and managing a flag
/// that indicates whether the memory segment is actively being written to.
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct MemoryWriteGuard<'a, T: ?Sized, Impl: ImplDetails> {
    state: Impl::RefState<T>,
    #[allow(unused)]
    guard: Impl::WriteGuard<'a>,
    value: &'a mut T,
}

impl<'a, T: ?Sized, Impl: ImplDetails> Deref for MemoryWriteGuard<'a, T, Impl> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, T: ?Sized, Impl: ImplDetails> DerefMut for MemoryWriteGuard<'a, T, Impl> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

impl<'a, T: ?Sized, Impl: ImplDetails> Drop for MemoryWriteGuard<'a, T, Impl> {
    fn drop(&mut self) {
        Impl::unborrow_ref::<T>(&self.state, BorrowKind::Write);
    }
}

/// A smart reference wrapper responsible for tracking and managing a flag
/// that indicates whether the memory segment is actively being read from.
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct MemoryReadGuard<'a, T: ?Sized, Impl: ImplDetails> {
    state: Impl::RefState<T>,
    #[allow(unused)]
    guard: Impl::ReadGuard<'a>,
    value: &'a T,
}

impl<'a, T: ?Sized, Impl: ImplDetails> Deref for MemoryReadGuard<'a, T, Impl> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, T: ?Sized, Impl: ImplDetails> Drop for MemoryReadGuard<'a, T, Impl> {
    fn drop(&mut self) {
        Impl::unborrow_ref::<T>(&self.state, BorrowKind::Read);
    }
}