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
//! This module defines a marker trait implemented for pointer types that
//! indicate their pointee's can be safely aliased.
#![no_std]
#![deny(missing_debug_implementations, missing_docs)]

use core::ops::Deref;

/// Marker trait for a pointer type that is allowed to have its
/// pointee aliased (except when dropped).
pub unsafe trait AliasableDeref: Deref {}

/// Helper trait for converting non-aliasable types
/// into their aliasable counterparts.
pub trait IntoAliasable {
    /// The aliasable type to convert to.
    type Target: AliasableDeref;

    /// Convert into an aliasable pointer type.
    fn into_aliasable(self) -> Self::Target;
}

impl<T> IntoAliasable for T
where
    T: AliasableDeref,
{
    type Target = T;

    fn into_aliasable(self) -> Self::Target {
        self
    }
}

unsafe impl<'a, T: ?Sized> AliasableDeref for core::cell::Ref<'a, T> {}

unsafe impl<'a, T: ?Sized> AliasableDeref for core::cell::RefMut<'a, T> {}

#[cfg(any(feature = "std", feature = "alloc", test))]
unsafe impl<T: ?Sized> AliasableDeref for self::std::rc::Rc<T> {}

#[cfg(any(feature = "std", feature = "alloc", test))]
unsafe impl<T: ?Sized> AliasableDeref for self::std::sync::Arc<T> {}

#[cfg(any(feature = "std", test))]
unsafe impl<'a, T: ?Sized> AliasableDeref for self::std::sync::MutexGuard<'a, T> {}

#[cfg(any(feature = "std", test))]
unsafe impl<'a, T: ?Sized> AliasableDeref for self::std::sync::RwLockReadGuard<'a, T> {}

#[cfg(any(feature = "std", test))]
unsafe impl<'a, T: ?Sized> AliasableDeref for self::std::sync::RwLockWriteGuard<'a, T> {}

#[cfg(any(feature = "std", test))]
mod std {
    extern crate std;
    pub use std::{rc, sync};
}

#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
mod std {
    extern crate alloc;
    pub use alloc::{rc, sync};
}

#[cfg(test)]
mod tests {
    use core::{cell::RefCell, ops::Deref};

    use super::{
        std::{
            rc::Rc,
            sync::{Arc, Mutex, RwLock},
        },
        AliasableDeref, IntoAliasable,
    };

    #[test]
    fn test_rc() {
        let ptr = &Rc::new(()) as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_arc() {
        let ptr = &Arc::new(()) as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_cell_ref() {
        let ref_cell = RefCell::new(());
        let ptr = &ref_cell.borrow() as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_cell_ref_mut() {
        let ref_cell = RefCell::new(());
        let ptr = &ref_cell.borrow_mut() as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_mutex_guard() {
        let mutex = Mutex::new(());
        let ptr = &mutex.lock().unwrap() as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_rw_lock_read_guard() {
        let rw_lock = RwLock::new(());
        let ptr = &rw_lock.read().unwrap() as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_rw_lock_write_guard() {
        let rw_lock = RwLock::new(());
        let ptr = &rw_lock.write().unwrap() as &dyn AliasableDeref<Target = ()>;
        assert_eq!(ptr.deref(), &());
    }

    #[test]
    fn test_into_aliasable_owner() {
        let aliasable_ptr = Arc::new(()).into_aliasable();
        assert_eq!(aliasable_ptr.deref(), &());
    }
}