This allows associated functions whose `self` argument has `ArcBorrow<T>` as their type. This, in turn, allows callers to use the dot syntax to make calls.
Showing the changes to rust/kernel (1 of 1 file). Show all
Unified · Split
| @@ -255,11 +255,34 @@ impl<T: ?Sized> Drop for Arc<T> { · 254 unmodified lines | |||
| 255 | /// // Assert that both `obj` and `cloned` point to the same underlying object. | 255 | /// // Assert that both `obj` and `cloned` point to the same underlying object. |
| 256 | /// assert!(core::ptr::eq(&*obj, &*cloned)); | 256 | /// assert!(core::ptr::eq(&*obj, &*cloned)); |
| 257 | /// ``` | 257 | /// ``` |
| 258 | /// | ||
| 259 | /// Using `ArcBorrow<T>` as the type of `self`: | ||
| 260 | /// | ||
| 261 | /// ``` | ||
| 262 | /// use crate::sync::{Arc, ArcBorrow}; | ||
| 263 | /// | ||
| 264 | /// struct Example { | ||
| 265 | /// a: u32, | ||
| 266 | /// b: u32, | ||
| 267 | /// } | ||
| 268 | /// | ||
| 269 | /// impl Example { | ||
| 270 | /// fn use_reference(self: ArcBorrow<'_, Self>) { | ||
| 271 | /// // ... | ||
| 272 | /// } | ||
| 273 | /// } | ||
| 274 | /// | ||
| 275 | /// let obj = Arc::try_new(Example { a: 10, b: 20 })?; | ||
| 276 | /// obj.as_arc_borrow().use_reference(); | ||
| 277 | /// ``` | ||
| 258 | pub struct ArcBorrow<'a, T: ?Sized + 'a> { | 278 | pub struct ArcBorrow<'a, T: ?Sized + 'a> { |
| 259 | inner: NonNull<ArcInner<T>>, | 279 | inner: NonNull<ArcInner<T>>, |
| 260 | _p: PhantomData<&'a ()>, | 280 | _p: PhantomData<&'a ()>, |
| 261 | } | 281 | } |
| 262 | 282 | ||
| 283 | // This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`. | ||
| 284 | impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {} | ||
| 285 | |||
| 263 | impl<T: ?Sized> Clone for ArcBorrow<'_, T> { | 286 | impl<T: ?Sized> Clone for ArcBorrow<'_, T> { |
| 264 | fn clone(&self) -> Self { | 287 | fn clone(&self) -> Self { |
| 265 | *self | 288 | *self |