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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#[macro_export] macro_rules! rental{ { mod $rent_mod:ident { $($items:tt)* } } => { mod $rent_mod { mod rent_impl { use ::std::ops::{Deref, DerefMut}; use ::std::ops::Drop; use ::std::result::Result; use ::std::mem; rental!{@ITEMS $($items)*} } rental!{@ALIASES $($items)*} } }; { @ITEMS pub struct $rent:ident<$([$lt:tt]),*$(,)* $($param:ident),*> [$($clause:tt)*] ( mut $owner_ty:ty, $rental_ty:ty$(,)* ); $($rest:tt)* } => { pub struct $rent<'owner $(, $lt)*, O: $crate::FixedDeref + DerefMut $(, $param)*> $($clause)* { owner: Option<O>, rental: Option<$rental_ty>, } impl<$($lt, )* $($param, )*> $rent<'static $(, $lt)*, $owner_ty $(, $param)*> { #[allow(dead_code)] pub fn new<F: for<'owner> FnOnce(&'owner mut <$owner_ty as Deref>::Target) -> $rental_ty>(mut owner: $owner_ty, f: F) -> $rent<'static $(, $lt)*, $owner_ty $(, $param)*> { $rent{ rental: unsafe { let ptr: *mut _ = &mut *owner; Some(mem::transmute(f(&mut *ptr))) }, owner: Some(owner), } } #[allow(dead_code)] pub fn try_new<E, F: for<'owner> FnOnce(&'owner mut <$owner_ty as Deref>::Target) -> Result<$rental_ty, E>>(mut owner: $owner_ty, f: F) -> Result<$rent<'static $(, $lt)*, $owner_ty $(, $param)*>, ($owner_ty, E)> { Ok($rent{ rental: unsafe { let ptr: *mut _ = &mut *owner; match f(&mut *ptr) { Ok(asset) => Some(mem::transmute(asset)), Err(err) => return Err((owner, err)), } }, owner: Some(owner), }) } #[allow(dead_code)] pub fn rental<'owner>(&'owner self) -> &'owner $rental_ty { self.rental.as_ref().unwrap() } #[allow(dead_code)] pub fn rental_mut<'owner>(&'owner mut self) -> &'owner mut $rental_ty { unsafe { mem::transmute(self.rental.as_mut().unwrap()) } } #[allow(dead_code)] pub fn into_owner(mut self) -> $owner_ty { self.owner.take().unwrap() } } impl<'owner $(, $lt)*, O: $crate::FixedDeref + DerefMut $(, $param)*> Drop for $rent<'owner $(, $lt)*, O $(, $param)*> { fn drop(&mut self) { mem::drop(self.rental.take()); mem::drop(self.owner.take()); } } rental!{@ITEMS $($rest)*} }; { @ITEMS pub struct $rent:ident<$([$lt:tt]),*$(,)* $($param:ident),*> [$($clause:tt)*] ( $owner_ty:ty, $rental_ty:ty$(,)* ); $($rest:tt)* } => { pub struct $rent<'owner $(, $lt)*, O: $crate::FixedDeref $(, $param)*> $($clause)* { owner: Option<O>, rental: Option<$rental_ty>, } impl<$($lt, )* $($param, )*> $rent<'static $(, $lt)*, $owner_ty $(, $param)*> { #[allow(dead_code)] pub fn new<F: for<'owner> FnOnce(&'owner <$owner_ty as Deref>::Target) -> $rental_ty>(owner: $owner_ty, f: F) -> $rent<'static $(, $lt)*, $owner_ty $(, $param)*> { $rent{ rental: unsafe { let ptr: *const _ = &*owner; Some(mem::transmute(f(&*ptr))) }, owner: Some(owner), } } #[allow(dead_code)] pub fn try_new<E, F: for<'owner> FnOnce(&'owner <$owner_ty as Deref>::Target) -> Result<$rental_ty, E>>(owner: $owner_ty, f: F) -> Result<$rent<'static $(, $lt)*, $owner_ty $(, $param)*>, ($owner_ty, E)> { Ok($rent{ rental: unsafe { let ptr: *const _ = &*owner; match f(&*ptr) { Ok(asset) => Some(mem::transmute(asset)), Err(err) => return Err((owner, err)), } }, owner: Some(owner), }) } #[allow(dead_code)] pub fn owner(&self) -> &$owner_ty { self.owner.as_ref().unwrap() } #[allow(dead_code)] pub fn rental<'owner>(&'owner self) -> &'owner $rental_ty { self.rental.as_ref().unwrap() } #[allow(dead_code)] pub fn into_owner(mut self) -> $owner_ty { self.owner.take().unwrap() } } impl<'owner $(, $lt)*, O: $crate::FixedDeref $(, $param)*> Drop for $rent<'owner $(, $lt)*, O $(, $param)*> { fn drop(&mut self) { mem::drop(self.rental.take()); mem::drop(self.owner.take()); } } rental!{@ITEMS $($rest)*} }; { @ITEMS } => { }; { @ALIASES pub struct $rent:ident<$([$lt:tt]),*$(,)* $($param:ident),*> [$($clause:tt)*] ( mut $owner_ty:ty, $rental_ty:ty$(,)* ); $($rest:tt)* } => { pub type $rent<$($lt, )* $($param, )*> = rent_impl::$rent<'static $(, $lt)*, $owner_ty $(, $param)*>; rental!{@ALIASES $($rest)*} }; { @ALIASES pub struct $rent:ident<$([$lt:tt]),*$(,)* $($param:ident),*> [$($clause:tt)*] ( $owner_ty:ty, $rental_ty:ty$(,)* ); $($rest:tt)* } => { pub type $rent<$($lt, )* $($param, )*> = rent_impl::$rent<'static $(, $lt)*, $owner_ty $(, $param)*>; rental!{@ALIASES $($rest)*} }; { @ALIASES } => { }; } pub unsafe trait FixedDeref: ::std::ops::Deref { } unsafe impl<'t, T: ?Sized> FixedDeref for &'t T { } unsafe impl<'t, T: ?Sized> FixedDeref for &'t mut T { } unsafe impl<T: ?Sized> FixedDeref for Box<T> { } unsafe impl<T> FixedDeref for Vec<T> { } unsafe impl FixedDeref for String { } unsafe impl<T: ?Sized> FixedDeref for ::std::rc::Rc<T> { } unsafe impl<T: ?Sized> FixedDeref for ::std::sync::Arc<T> { } unsafe impl<'t, T: ?Sized> FixedDeref for ::std::cell::Ref<'t, T> { } unsafe impl<'t, T: ?Sized> FixedDeref for ::std::cell::RefMut<'t, T> { } unsafe impl<'t, T: ?Sized> FixedDeref for ::std::sync::MutexGuard<'t, T> { } unsafe impl<'t, T: ?Sized> FixedDeref for ::std::sync::RwLockReadGuard<'t, T> { } unsafe impl<'t, T: ?Sized> FixedDeref for ::std::sync::RwLockWriteGuard<'t, T> { } #[cfg(test)] mod test { use ::std::cell::RefCell; rental!{ mod rental { pub struct RentRef<T> [where T: 'static] (Box<::std::cell::RefCell<T>>, Box<::std::cell::Ref<'owner, T>>); pub struct RentRefMut<T> [where T: 'static] (mut Box<::std::cell::RefCell<T>>, Box<::std::cell::RefMut<'owner, T>>); } } #[test] fn instantiate() { let _rent = rental::RentRef::new(Box::new(RefCell::new(5)), |r| Box::new(r.borrow())); let _rent_mut = rental::RentRefMut::new(Box::new(RefCell::new(12)), |r| Box::new(r.borrow_mut())); } }