Skip to main content

reft/
lib.rs

1//! `Ref` & `Mut` — Universal `AsRef`/`AsMut` bridges for _any_ reference.
2//!
3//! Turns `&T` into `AsRef<T>` and `&mut T` into `AsMut<T>` with zero cost.
4//! Perfect for generics that need trait bounds on foreign types.
5
6use core::{clone::Clone, marker::PhantomData};
7
8/// Immutable reference with guaranteed `AsRef<T>` impl.  
9/// Works on _any_ type, including those you don't control.
10pub struct Ref<'a, T: ?Sized>(*const T, PhantomData<&'a T>);
11
12/// Mutable reference with `AsRef<T>` + `AsMut<T>` impls.  
13/// Like `&mut T`, but plays nice with trait bounds.
14pub struct Mut<'a, T: ?Sized>(*mut T, PhantomData<&'a T>);
15
16/// Owned data with `AsRef<T>` + `AsMut<T>` impls.
17#[derive(Clone, Copy)]
18pub struct Own<T: Sized>(T);
19
20impl<'a, T: ?Sized> Ref<'a, T> {
21    /// Creates a new `Ref` from the provided reference.
22    #[inline(always)]
23    pub const fn new(inner: &'a T) -> Self {
24        Self(inner as *const T, PhantomData)
25    }
26
27    /// Returns the inner reference.
28    #[inline(always)]
29    pub const fn get(&self) -> &'a T {
30        unsafe { &*self.0 }
31    }
32
33    /// Returns the inner reference, consuming the wrapper.
34    #[inline(always)]
35    pub const fn into_inner(self) -> &'a T {
36        self.get()
37    }
38}
39
40impl<'a, T: ?Sized> Mut<'a, T> {
41    /// Creates a new `Mut` from the provided **mutable** reference.
42    #[inline(always)]
43    pub const fn new(inner: &'a mut T) -> Self {
44        Self(inner as *mut T, PhantomData)
45    }
46
47    /// Returns the inner reference.
48    #[inline(always)]
49    pub const fn get(&self) -> &'a T {
50        unsafe { &*self.0 }
51    }
52
53    /// Returns the inner **mutable** reference.
54    #[inline(always)]
55    pub const fn get_mut(&mut self) -> &'a mut T {
56        unsafe { &mut *self.0 }
57    }
58
59    /// Returns the inner **mutable** reference, consuming the wrapper.
60    #[inline(always)]
61    pub const fn into_inner(mut self) -> &'a mut T {
62        self.get_mut()
63    }
64}
65
66impl<T: Sized> Own<T> {
67    /// Creates a `Own` with the provided inner.
68    #[inline(always)]
69    pub const fn new(inner: T) -> Self {
70        Self(inner)
71    }
72
73    /// Returns an immutable reference to the inner data.
74    #[inline(always)]
75    pub const fn get(&self) -> &T {
76        &self.0
77    }
78
79    /// Returns a **mutable** reference to the inner data.
80    #[inline(always)]
81    pub const fn get_mut(&mut self) -> &mut T {
82        &mut self.0
83    }
84
85    /// Returns the inner data, consuming the wrapper.
86    #[inline(always)]
87    pub fn into_inner(self) -> T {
88        self.0
89    }
90}
91
92impl<'a, T: ?Sized> AsRef<T> for Ref<'a, T> {
93    #[inline(always)]
94    fn as_ref(&self) -> &T {
95        self.get()
96    }
97}
98
99impl<'a, T: ?Sized> Clone for Ref<'a, T> {
100    #[inline(always)]
101    fn clone(&self) -> Self {
102        Self::new(self.get())
103    }
104}
105
106impl<'a, T: ?Sized> AsRef<T> for Mut<'a, T> {
107    #[inline(always)]
108    fn as_ref(&self) -> &T {
109        self.get()
110    }
111}
112
113impl<'a, T: ?Sized> AsMut<T> for Mut<'a, T> {
114    #[inline(always)]
115    fn as_mut(&mut self) -> &mut T {
116        self.get_mut()
117    }
118}
119
120impl<T: Sized> AsRef<T> for Own<T> {
121    #[inline(always)]
122    fn as_ref(&self) -> &T {
123        self.get()
124    }
125}
126
127impl<T: Sized> AsMut<T> for Own<T> {
128    #[inline(always)]
129    fn as_mut(&mut self) -> &mut T {
130        self.get_mut()
131    }
132}