Skip to content

Commit b8bb3d2

Browse files
committed
kernel: sync: Add "Examples" section for Arc::into_unique_or_drop()
These examples provide better documentation and can serve as unit tests as well, so add them. Signed-off-by: Boqun Feng <[email protected]> Reviewed-by: Alice Ryhl <[email protected]>
1 parent e9a17e8 commit b8bb3d2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

rust/kernel/sync/arc.rs

+30
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,36 @@ impl<T: ?Sized> Arc<T> {
296296
///
297297
/// When this destroys the `Arc`, it does so while properly avoiding races. This means that
298298
/// this method will never call the destructor of the value.
299+
///
300+
/// # Examples
301+
///
302+
/// ```
303+
/// use kernel::sync::{Arc, UniqueArc};
304+
///
305+
/// let arc = Arc::try_new(42)?;
306+
/// let unique_arc = arc.into_unique_or_drop();
307+
///
308+
/// // The above conversion should succeed since refcount of `arc` is 1.
309+
/// assert!(unique_arc.is_some());
310+
///
311+
/// assert_eq!(*(unique_arc.unwrap()), 42);
312+
///
313+
/// # Ok::<(), Error>(())
314+
/// ```
315+
///
316+
/// ```
317+
/// use kernel::sync::{Arc, UniqueArc};
318+
///
319+
/// let arc = Arc::try_new(42)?;
320+
/// let another = arc.clone();
321+
///
322+
/// let unique_arc = arc.into_unique_or_drop();
323+
///
324+
/// // The above conversion should fail since refcount of `arc` is >1.
325+
/// assert!(unique_arc.is_none());
326+
///
327+
/// # Ok::<(), Error>(())
328+
/// ```
299329
pub fn into_unique_or_drop(self) -> Option<Pin<UniqueArc<T>>> {
300330
// We will manually manage the refcount in this method, so we disable the destructor.
301331
let me = ManuallyDrop::new(self);

0 commit comments

Comments
 (0)