| Adrian Taylor | 9f7ff2e | 2020-10-12 17:29:25 -0700 | [diff] [blame] | 1 | use crate::kind::Trivial; |
| David Tolnay | bac2582 | 2020-12-12 23:13:51 -0800 | [diff] [blame] | 2 | use crate::string::CxxString; |
| 3 | use crate::vector::{self, CxxVector, VectorElement}; |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 4 | use crate::ExternType; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 5 | use core::ffi::c_void; |
| 6 | use core::fmt::{self, Debug, Display}; |
| 7 | use core::marker::PhantomData; |
| 8 | use core::mem; |
| David Tolnay | 300ef4a | 2020-11-19 20:26:34 -0800 | [diff] [blame] | 9 | use core::ops::{Deref, DerefMut}; |
| David Tolnay | a417065 | 2020-11-15 17:00:59 -0800 | [diff] [blame] | 10 | use core::pin::Pin; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 11 | use core::ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 12 | |
| 13 | /// Binding to C++ `std::unique_ptr<T, std::default_delete<T>>`. |
| 14 | #[repr(C)] |
| 15 | pub struct UniquePtr<T> |
| 16 | where |
| 17 | T: UniquePtrTarget, |
| 18 | { |
| 19 | repr: *mut c_void, |
| 20 | ty: PhantomData<T>, |
| 21 | } |
| 22 | |
| 23 | impl<T> UniquePtr<T> |
| 24 | where |
| 25 | T: UniquePtrTarget, |
| 26 | { |
| 27 | /// Makes a new UniquePtr wrapping a null pointer. |
| 28 | /// |
| 29 | /// Matches the behavior of default-constructing a std::unique\_ptr. |
| 30 | pub fn null() -> Self { |
| 31 | UniquePtr { |
| 32 | repr: T::__null(), |
| 33 | ty: PhantomData, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Allocates memory on the heap and makes a UniquePtr pointing to it. |
| Adrian Taylor | 9f7ff2e | 2020-10-12 17:29:25 -0700 | [diff] [blame] | 38 | pub fn new(value: T) -> Self |
| 39 | where |
| David Tolnay | ca0f9da | 2020-10-16 13:16:17 -0700 | [diff] [blame] | 40 | T: ExternType<Kind = Trivial>, |
| 41 | { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 42 | UniquePtr { |
| 43 | repr: T::__new(value), |
| 44 | ty: PhantomData, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Checks whether the UniquePtr does not own an object. |
| 49 | /// |
| 50 | /// This is the opposite of [std::unique_ptr\<T\>::operator bool](https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool). |
| 51 | pub fn is_null(&self) -> bool { |
| 52 | let ptr = unsafe { T::__get(self.repr) }; |
| 53 | ptr.is_null() |
| 54 | } |
| 55 | |
| 56 | /// Returns a reference to the object owned by this UniquePtr if any, |
| 57 | /// otherwise None. |
| 58 | pub fn as_ref(&self) -> Option<&T> { |
| 59 | unsafe { T::__get(self.repr).as_ref() } |
| 60 | } |
| 61 | |
| David Tolnay | 6fdeeeb | 2020-11-16 22:34:50 -0800 | [diff] [blame] | 62 | /// Returns a mutable pinned reference to the object owned by this UniquePtr |
| 63 | /// if any, otherwise None. |
| 64 | pub fn as_mut(&mut self) -> Option<Pin<&mut T>> { |
| 65 | unsafe { |
| 66 | let mut_reference = (T::__get(self.repr) as *mut T).as_mut()?; |
| 67 | Some(Pin::new_unchecked(mut_reference)) |
| 68 | } |
| David Tolnay | 1a61b16 | 2020-04-09 23:20:05 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| David Tolnay | 09a3086 | 2020-11-22 12:01:57 -0800 | [diff] [blame] | 71 | /// Returns a mutable pinned reference to the object owned by this |
| 72 | /// UniquePtr. |
| 73 | /// |
| 74 | /// # Panics |
| 75 | /// |
| 76 | /// Panics if the UniquePtr holds a null pointer. |
| 77 | pub fn pin_mut(&mut self) -> Pin<&mut T> { |
| 78 | match self.as_mut() { |
| 79 | Some(target) => target, |
| 80 | None => panic!("called pin_mut on a null UniquePtr<{}>", T::__NAME), |
| 81 | } |
| 82 | } |
| 83 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 84 | /// Consumes the UniquePtr, releasing its ownership of the heap-allocated T. |
| 85 | /// |
| 86 | /// Matches the behavior of [std::unique_ptr\<T\>::release](https://en.cppreference.com/w/cpp/memory/unique_ptr/release). |
| 87 | pub fn into_raw(self) -> *mut T { |
| 88 | let ptr = unsafe { T::__release(self.repr) }; |
| 89 | mem::forget(self); |
| 90 | ptr |
| 91 | } |
| 92 | |
| 93 | /// Constructs a UniquePtr retaking ownership of a pointer previously |
| 94 | /// obtained from `into_raw`. |
| 95 | /// |
| 96 | /// # Safety |
| 97 | /// |
| 98 | /// This function is unsafe because improper use may lead to memory |
| 99 | /// problems. For example a double-free may occur if the function is called |
| 100 | /// twice on the same raw pointer. |
| 101 | pub unsafe fn from_raw(raw: *mut T) -> Self { |
| 102 | UniquePtr { |
| 103 | repr: T::__raw(raw), |
| 104 | ty: PhantomData, |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | unsafe impl<T> Send for UniquePtr<T> where T: Send + UniquePtrTarget {} |
| 110 | unsafe impl<T> Sync for UniquePtr<T> where T: Sync + UniquePtrTarget {} |
| 111 | |
| 112 | impl<T> Drop for UniquePtr<T> |
| 113 | where |
| 114 | T: UniquePtrTarget, |
| 115 | { |
| 116 | fn drop(&mut self) { |
| 117 | unsafe { T::__drop(self.repr) } |
| 118 | } |
| 119 | } |
| 120 | |
| David Tolnay | d20032a | 2020-04-09 23:21:53 -0700 | [diff] [blame] | 121 | impl<T> Deref for UniquePtr<T> |
| 122 | where |
| 123 | T: UniquePtrTarget, |
| 124 | { |
| 125 | type Target = T; |
| 126 | |
| 127 | fn deref(&self) -> &Self::Target { |
| 128 | match self.as_ref() { |
| 129 | Some(target) => target, |
| David Tolnay | 9f318a1 | 2020-04-09 23:44:02 -0700 | [diff] [blame] | 130 | None => panic!("called deref on a null UniquePtr<{}>", T::__NAME), |
| David Tolnay | d20032a | 2020-04-09 23:21:53 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| David Tolnay | 300ef4a | 2020-11-19 20:26:34 -0800 | [diff] [blame] | 135 | impl<T> DerefMut for UniquePtr<T> |
| 136 | where |
| 137 | T: UniquePtrTarget + Unpin, |
| 138 | { |
| 139 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 140 | match self.as_mut() { |
| 141 | Some(target) => Pin::into_inner(target), |
| 142 | None => panic!("called deref_mut on a null UniquePtr<{}>", T::__NAME), |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 147 | impl<T> Debug for UniquePtr<T> |
| 148 | where |
| 149 | T: Debug + UniquePtrTarget, |
| 150 | { |
| 151 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 152 | match self.as_ref() { |
| 153 | None => formatter.write_str("nullptr"), |
| 154 | Some(value) => Debug::fmt(value, formatter), |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | impl<T> Display for UniquePtr<T> |
| 160 | where |
| 161 | T: Display + UniquePtrTarget, |
| 162 | { |
| 163 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 164 | match self.as_ref() { |
| 165 | None => formatter.write_str("nullptr"), |
| 166 | Some(value) => Display::fmt(value, formatter), |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Methods are private; not intended to be implemented outside of cxxbridge |
| 172 | // codebase. |
| 173 | pub unsafe trait UniquePtrTarget { |
| 174 | #[doc(hidden)] |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 175 | const __NAME: &'static dyn Display; |
| David Tolnay | ad26677 | 2020-04-09 23:42:29 -0700 | [diff] [blame] | 176 | #[doc(hidden)] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 177 | fn __null() -> *mut c_void; |
| 178 | #[doc(hidden)] |
| David Tolnay | 5383891 | 2020-04-09 20:56:44 -0700 | [diff] [blame] | 179 | fn __new(value: Self) -> *mut c_void |
| 180 | where |
| 181 | Self: Sized, |
| 182 | { |
| 183 | // Opaque C types do not get this method because they can never exist by |
| 184 | // value on the Rust side of the bridge. |
| 185 | let _ = value; |
| 186 | unreachable!() |
| 187 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 188 | #[doc(hidden)] |
| 189 | unsafe fn __raw(raw: *mut Self) -> *mut c_void; |
| 190 | #[doc(hidden)] |
| 191 | unsafe fn __get(repr: *mut c_void) -> *const Self; |
| 192 | #[doc(hidden)] |
| 193 | unsafe fn __release(repr: *mut c_void) -> *mut Self; |
| 194 | #[doc(hidden)] |
| 195 | unsafe fn __drop(repr: *mut c_void); |
| 196 | } |
| 197 | |
| 198 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 199 | #[link_name = "cxxbridge1$unique_ptr$std$string$null"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 200 | fn unique_ptr_std_string_null(this: *mut *mut c_void); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 201 | #[link_name = "cxxbridge1$unique_ptr$std$string$raw"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 202 | fn unique_ptr_std_string_raw(this: *mut *mut c_void, raw: *mut CxxString); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 203 | #[link_name = "cxxbridge1$unique_ptr$std$string$get"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 204 | fn unique_ptr_std_string_get(this: *const *mut c_void) -> *const CxxString; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 205 | #[link_name = "cxxbridge1$unique_ptr$std$string$release"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 206 | fn unique_ptr_std_string_release(this: *mut *mut c_void) -> *mut CxxString; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 207 | #[link_name = "cxxbridge1$unique_ptr$std$string$drop"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 208 | fn unique_ptr_std_string_drop(this: *mut *mut c_void); |
| 209 | } |
| 210 | |
| 211 | unsafe impl UniquePtrTarget for CxxString { |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 212 | const __NAME: &'static dyn Display = &"CxxString"; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 213 | fn __null() -> *mut c_void { |
| 214 | let mut repr = ptr::null_mut::<c_void>(); |
| David Tolnay | 2c4b35f | 2020-10-04 21:22:30 -0700 | [diff] [blame] | 215 | unsafe { |
| 216 | unique_ptr_std_string_null(&mut repr); |
| 217 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 218 | repr |
| 219 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 220 | unsafe fn __raw(raw: *mut Self) -> *mut c_void { |
| 221 | let mut repr = ptr::null_mut::<c_void>(); |
| 222 | unique_ptr_std_string_raw(&mut repr, raw); |
| 223 | repr |
| 224 | } |
| 225 | unsafe fn __get(repr: *mut c_void) -> *const Self { |
| 226 | unique_ptr_std_string_get(&repr) |
| 227 | } |
| 228 | unsafe fn __release(mut repr: *mut c_void) -> *mut Self { |
| 229 | unique_ptr_std_string_release(&mut repr) |
| 230 | } |
| 231 | unsafe fn __drop(mut repr: *mut c_void) { |
| 232 | unique_ptr_std_string_drop(&mut repr); |
| 233 | } |
| 234 | } |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 235 | |
| 236 | unsafe impl<T> UniquePtrTarget for CxxVector<T> |
| 237 | where |
| 238 | T: VectorElement + 'static, |
| 239 | { |
| David Tolnay | bac2582 | 2020-12-12 23:13:51 -0800 | [diff] [blame] | 240 | const __NAME: &'static dyn Display = &vector::TypeName::<T>::new(); |
| David Tolnay | 3b40b6f | 2020-04-24 17:58:24 -0700 | [diff] [blame] | 241 | fn __null() -> *mut c_void { |
| 242 | T::__unique_ptr_null() |
| 243 | } |
| 244 | unsafe fn __raw(raw: *mut Self) -> *mut c_void { |
| 245 | T::__unique_ptr_raw(raw) |
| 246 | } |
| 247 | unsafe fn __get(repr: *mut c_void) -> *const Self { |
| 248 | T::__unique_ptr_get(repr) |
| 249 | } |
| 250 | unsafe fn __release(repr: *mut c_void) -> *mut Self { |
| 251 | T::__unique_ptr_release(repr) |
| 252 | } |
| 253 | unsafe fn __drop(repr: *mut c_void) { |
| 254 | T::__unique_ptr_drop(repr); |
| 255 | } |
| 256 | } |