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