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
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/// ```
258pub struct ArcBorrow<'a, T: ?Sized + 'a> {278pub 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`.
284impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
285
263impl<T: ?Sized> Clone for ArcBorrow<'_, T> {286impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
264 fn clone(&self) -> Self {287 fn clone(&self) -> Self {
265 *self288 *self