History

rust: sync: allow type of `self` to be `ArcBorrow<T>`

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.

Wedson Almeida Filho committed 3 years ago

Commit 92a655a1 parent 17f67161 file · +23 −0Browse files

Showing the changes to rust/kernel (1 of 1 file). Show all

Unified · Split

rust/kernel/sync/arc.rs+23−0
@@ -255,11 +255,34 @@ impl<T: ?Sized> Drop for Arc<T> { · 254 unmodified lines
255255 /// // Assert that both `obj` and `cloned` point to the same underlying object.
256256 /// assert!(core::ptr::eq(&*obj, &*cloned));
257257 /// ```
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+/// ```
258278 pub struct ArcBorrow<'a, T: ?Sized + 'a> {
259279 inner: NonNull<ArcInner<T>>,
260280 _p: PhantomData<&'a ()>,
261281 }
262282
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+
263286 impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
264287 fn clone(&self) -> Self {
265288 *self