archery/shared_pointer/kind/mod.rs
1use core::fmt::Debug;
2
3/// Trait for [type constructors](https://en.wikipedia.org/wiki/Type_constructor) of
4/// reference-counting pointers.
5///
6/// # Type-parameter invariant
7///
8/// Each instance of a `SharedPointerKind` implementer is logically associated with a fixed inner
9/// type `T`, chosen when the instance is constructed via [`Self::new`], [`Self::from_box`], or
10/// [`Self::clone`]. All subsequent calls of the `unsafe` methods on that instance must be called
11/// with the same `T`. Callers of the `unsafe` methods are responsible for upholding this
12/// invariant.
13///
14/// # Wrapping in a safe API
15///
16/// Implementers of this trait do not know the `T` they hold, so their [`Send`]/[`Sync`] impls
17/// (if any) are unconditional. For example, [`ArcK`] is always [`Send`] + [`Sync`], but it is
18/// only actually safe to send or share across threads when the inner `T` is itself [`Send`] +
19/// [`Sync`].
20///
21/// A safe wrapper around a `SharedPointerKind` implementer must therefore gate its own
22/// [`Send`]/[`Sync`] impls on `T: Send + Sync`. [`SharedPointer<T, P>`][SharedPointer] achieves
23/// this by including a [`PhantomData<T>`][PhantomData] field, so the compiler only derives
24/// [`Send`]/[`Sync`] for `SharedPointer<T, P>` when both `T` and `P` are appropriate.
25///
26/// # Safety
27///
28/// `T` may be `!`[`Unpin`], and [`SharedPointer`] may be held in a pinned
29/// form ([`Pin`]`<SharedPointer<T, Self>>`).
30/// As such, the implementation of this trait must uphold the pinning invariants
31/// for `T` while it's held in `Self`. Specifically, this necessitates the
32/// following:
33///
34/// - `&mut T` is only exposed through the trait methods returning `&mut T`.
35///
36/// - The implementor must not move out the contained `T` unless the semantics
37/// of trait methods demands that.
38///
39/// - [`Self::drop`] drops `T` in place.
40///
41/// [SharedPointer]: crate::shared_pointer::SharedPointer
42/// [`SharedPointer`]: crate::shared_pointer::SharedPointer
43/// [`Pin`]: core::pin::Pin
44/// [PhantomData]: core::marker::PhantomData
45pub unsafe trait SharedPointerKind: Sized + Debug {
46 fn new<T>(v: T) -> Self;
47 fn from_box<T>(v: Box<T>) -> Self;
48
49 /// # Safety
50 ///
51 /// `Self` must have been constructed with the same `T`. See the
52 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
53 unsafe fn as_ptr<T>(&self) -> *const T;
54
55 /// # Safety
56 ///
57 /// `Self` must have been constructed with the same `T`. See the
58 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
59 unsafe fn deref<T>(&self) -> &T;
60
61 /// # Safety
62 ///
63 /// `Self` must have been constructed with the same `T`. See the
64 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
65 unsafe fn try_unwrap<T>(self) -> Result<T, Self>;
66
67 /// # Safety
68 ///
69 /// `Self` must have been constructed with the same `T`. See the
70 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
71 unsafe fn get_mut<T>(&mut self) -> Option<&mut T>;
72
73 /// # Safety
74 ///
75 /// `Self` must have been constructed with the same `T`. See the
76 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
77 unsafe fn make_mut<T: Clone>(&mut self) -> &mut T;
78
79 /// # Safety
80 ///
81 /// `Self` must have been constructed with the same `T`. See the
82 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
83 unsafe fn strong_count<T>(&self) -> usize;
84
85 /// The returned `Self` inherits the same type-parameter `T` as `self`.
86 ///
87 /// # Safety
88 ///
89 /// `Self` must have been constructed with the same `T`. See the
90 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
91 #[must_use]
92 unsafe fn clone<T>(&self) -> Self;
93
94 /// # Safety
95 ///
96 /// `Self` must have been constructed with the same `T`. See the
97 /// [type-parameter invariant](SharedPointerKind#type-parameter-invariant).
98 ///
99 /// This method must be called at most once per instance, when `Self` is being disposed of.
100 /// After the call, `Self` must not be used again.
101 unsafe fn drop<T>(&mut self);
102}
103
104mod arc;
105#[cfg(feature = "triomphe")]
106mod arct;
107mod erased_ptr;
108mod rc;
109
110use alloc::boxed::Box;
111#[doc(inline)]
112pub use arc::ArcK;
113#[cfg(feature = "triomphe")]
114#[doc(inline)]
115pub use arct::ArcTK;
116#[doc(inline)]
117pub use rc::RcK;