reffers/
lib.rs

1//! Wrappers around references, boxes or Arcs.
2//!
3//! Features: 
4//!
5//! * ARef: `OwningRef` with even further erasure of the owner.
6//!
7//! * rc::Strong/Weak/Ref/RefMut: An `Rc<RefCell<T>>` in just a few bytes of storage, and poisoning support.
8//! 
9//! * arc::Strong/Weak/Ref/RefMut: An `Arc<Spinlock<T>>`- the thread-safe version of the above.
10//!
11//! * RMBA: Wrap a `&T`, `&mut T`, `Box<T>` or `Arc<T>` within the size of a single pointer. 
12//!
13//! * Bx and Bxm: Boxes without DerefMove.
14
15// #![warn(missing_docs)]
16
17extern crate stable_deref_trait;
18
19use std::ops::{Deref, DerefMut};
20use stable_deref_trait::StableDeref;
21
22#[macro_use]
23mod rc_macros;
24
25pub mod aref;
26pub mod rmba;
27mod rc_bitmask;
28pub mod rc;
29pub mod arc;
30
31pub use crate::aref::ARef as ARef;
32pub use crate::aref::ARefs as ARefs;
33pub use crate::aref::ARefss as ARefss;
34pub use crate::rmba::RMBA as RMBA;
35
36/// Type aliases for an rc with 1 byte of overhead.
37pub mod rc1 {
38    pub type RCell<T> = crate::rc::RCell<T, u8>;
39    pub type Ref<T> = crate::rc::Ref<T, u8>;
40    pub type RefMut<T> = crate::rc::RefMut<T, u8>;
41    pub type Strong<T> = crate::rc::Strong<T, u8>;
42    pub type Weak<T> = crate::rc::Weak<T, u8>;
43}
44
45/// Type aliases for an rc with 2 bytes of overhead.
46pub mod rc2 {
47    pub type RCell<T> = crate::rc::RCell<T, u16>;
48    pub type Ref<T> = crate::rc::Ref<T, u16>;
49    pub type RefMut<T> = crate::rc::RefMut<T, u16>;
50    pub type Strong<T> = crate::rc::Strong<T, u16>;
51    pub type Weak<T> = crate::rc::Weak<T, u16>;
52}
53
54/// Type aliases for an rc with 4 bytes of overhead.
55pub mod rc4 {
56    pub type RCell<T> = crate::rc::RCell<T, u32>;
57    pub type Ref<T> = crate::rc::Ref<T, u32>;
58    pub type RefMut<T> = crate::rc::RefMut<T, u32>;
59    pub type Strong<T> = crate::rc::Strong<T, u32>;
60    pub type Weak<T> = crate::rc::Weak<T, u32>;
61}
62
63/// Type aliases for arc.
64pub mod arcu {
65    pub type Ref<T> = crate::arc::Ref<T, usize>;
66    pub type RefMut<T> = crate::arc::RefMut<T, usize>;
67    pub type Strong<T> = crate::arc::Strong<T, usize>;
68    pub type Weak<T> = crate::arc::Weak<T, usize>;
69}
70
71
72/// Typedefs for an rc with 8 bytes of overhead.
73pub mod rc8 {
74    pub type RCell<T> = crate::rc::RCell<T, u64>;
75    pub type Ref<T> = crate::rc::Ref<T, u64>;
76    pub type RefMut<T> = crate::rc::RefMut<T, u64>;
77    pub type Strong<T> = crate::rc::Strong<T, u64>;
78    pub type Weak<T> = crate::rc::Weak<T, u64>;
79}
80
81/// Typedefs for an rc with 16 bytes of overhead.
82pub mod rc16 {
83    pub type RCell<T> = crate::rc::RCell<T, u128>;
84    pub type Ref<T> = crate::rc::Ref<T, u128>;
85    pub type RefMut<T> = crate::rc::RefMut<T, u128>;
86    pub type Strong<T> = crate::rc::Strong<T, u128>;
87    pub type Weak<T> = crate::rc::Weak<T, u128>;
88}
89
90
91/// A simple wrapper around Box to avoid DerefMove.
92///
93/// There is no way to get to
94/// &Box<T>, just &T. This way you can return a Bx<T> in your API and still be
95/// sure the inner struct does not move in memory. (This might be helpful if you're
96/// dealing with FFI or unsafe code.)
97#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
98pub struct Bx<T: ?Sized>(Box<T>);
99
100impl<T> Bx<T> {
101    pub fn new(t: T) -> Bx<T> { Bx(Box::new(t)) }
102}
103
104impl<T: ?Sized> From<Box<T>> for Bx<T> {
105    fn from(t: Box<T>) -> Bx<T> { Bx(t) }
106}
107
108impl<T: ?Sized> Deref for Bx<T> {
109    type Target = T;
110    #[inline]
111    fn deref(&self) -> &T { &self.0 }
112}
113
114unsafe impl<T: ?Sized> StableDeref for Bx<T> {}
115
116/// A simple wrapper around Box to avoid DerefMove. Like Bx,
117/// but also allows mutable access to the interior of the box.
118///
119/// Note that this will allow callers to use e g `mem::swap` and
120/// `mem::replace` to move the box's contents to other memory
121/// locations.
122#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
123pub struct Bxm<T: ?Sized>(Box<T>);
124
125impl<T> Bxm<T> {
126    pub fn new(t: T) -> Bxm<T> { Bxm(Box::new(t)) }
127}
128
129impl<T: ?Sized> From<Box<T>> for Bxm<T> {
130    fn from(t: Box<T>) -> Bxm<T> { Bxm(t) }
131}
132
133impl<T: ?Sized> Deref for Bxm<T> {
134    type Target = T;
135    #[inline]
136    fn deref(&self) -> &T { &self.0 }
137}
138
139impl<T: ?Sized> DerefMut for Bxm<T> {
140    #[inline]
141    fn deref_mut(&mut self) -> &mut T { &mut self.0 }
142}
143
144unsafe impl<T: ?Sized> StableDeref for Bxm<T> {}
145