Lines Matching defs:T

41 /// A reference-counted pointer to an instance of `T`.
44 /// when they are dropped. When the count reaches zero, the underlying `T` is also dropped.
81 /// Using `Arc<T>` as the type of `self`:
132 pub struct Arc<T: ?Sized> {
133 ptr: NonNull<ArcInner<T>>,
134 // NB: this informs dropck that objects of type `ArcInner<T>` may be used in `<Arc<T> as
135 // Drop>::drop`. Note that dropck already assumes that objects of type `T` may be used in
136 // `<Arc<T> as Drop>::drop` and the distinction between `T` and `ArcInner<T>` is not presently
142 _p: PhantomData<ArcInner<T>>,
147 struct ArcInner<T: ?Sized> {
149 data: T,
152 impl<T: ?Sized> ArcInner<T> {
159 unsafe fn container_of(ptr: *const T) -> NonNull<ArcInner<T>> {
167 // Pointer casts leave the metadata unchanged. This is okay because the metadata of `T` and
168 // `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as its last field.
172 let ptr = ptr as *const ArcInner<T>;
179 // SAFETY: The pointer can't be null since you can't have an `ArcInner<T>` value at the null
185 // This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
188 impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
190 // This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
192 impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
194 // SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
195 // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
196 // `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
197 // mutable reference when the reference count reaches zero and `T` is dropped.
198 unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
200 // SAFETY: It is safe to send `&Arc<T>` to another thread when the underlying `T` is `Sync`
201 // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
202 // it needs `T` to be `Send` because any thread that has a `&Arc<T>` may clone it and get an
203 // `Arc<T>` on that thread, so the thread may ultimately access `T` using a mutable reference when
204 // the reference count reaches zero and `T` is dropped.
205 unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
207 impl<T> InPlaceInit<T> for Arc<T> {
211 fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E>
219 fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
227 impl<T> Arc<T> {
228 /// Constructs a new reference counted instance of `T`.
229 pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {
245 impl<T: ?Sized> Arc<T> {
252 unsafe fn from_inner(inner: NonNull<ArcInner<T>>) -> Self {
263 pub fn into_raw(self) -> *const T {
271 pub fn as_ptr(this: &Self) -> *const T {
285 pub unsafe fn from_raw(ptr: *const T) -> Self {
300 pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
346 pub fn into_unique_or_drop(this: Self) -> Option<Pin<UniqueArc<T>>> {
371 // `KBox<ArcInner<T>>`, so that type is what determines the alignment.
372 unsafe impl<T: 'static> ForeignOwnable for Arc<T> {
373 const FOREIGN_ALIGN: usize = <KBox<ArcInner<T>> as ForeignOwnable>::FOREIGN_ALIGN;
375 type Borrowed<'a> = ArcBorrow<'a, T>;
385 let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
393 unsafe fn borrow<'a>(ptr: *mut c_void) -> ArcBorrow<'a, T> {
396 let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
403 unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> ArcBorrow<'a, T> {
410 impl<T: ?Sized> Deref for Arc<T> {
411 type Target = T;
420 impl<T: ?Sized> AsRef<T> for Arc<T> {
421 fn as_ref(&self) -> &T {
445 impl<T: ?Sized> Borrow<T> for Arc<T> {
446 fn borrow(&self) -> &T {
451 impl<T: ?Sized> Clone for Arc<T> {
463 impl<T: ?Sized> Drop for Arc<T> {
478 impl<T: ?Sized> From<UniqueArc<T>> for Arc<T> {
479 fn from(item: UniqueArc<T>) -> Self {
484 impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
485 fn from(item: Pin<UniqueArc<T>>) -> Self {
494 /// to use just `&T`, which we can trivially get from an [`Arc<T>`] instance.
496 /// However, when one may need to increment the refcount, it is preferable to use an `ArcBorrow<T>`
497 /// over `&Arc<T>` because the latter results in a double-indirection: a pointer (shared reference)
498 /// to a pointer ([`Arc<T>`]) to the object (`T`). An [`ArcBorrow`] eliminates this double
499 /// indirection while still allowing one to increment the refcount and getting an [`Arc<T>`] when/if
526 /// Using `ArcBorrow<T>` as the type of `self`:
548 pub struct ArcBorrow<'a, T: ?Sized + 'a> {
549 inner: NonNull<ArcInner<T>>,
553 // This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
556 impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
557 for ArcBorrow<'_, T>
561 impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
567 impl<T: ?Sized> Copy for ArcBorrow<'_, T> {}
569 impl<T: ?Sized> ArcBorrow<'_, T> {
577 unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
595 pub unsafe fn from_raw(ptr: *const T) -> Self {
607 impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
608 fn from(b: ArcBorrow<'_, T>) -> Self {
618 impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
619 type Target = T;
702 pub struct UniqueArc<T: ?Sized> {
703 inner: Arc<T>,
706 impl<T> InPlaceInit<T> for UniqueArc<T> {
710 fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E>
718 fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
726 impl<T> InPlaceWrite<T> for UniqueArc<MaybeUninit<T>> {
727 type Initialized = UniqueArc<T>;
729 fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
738 fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
748 impl<T> UniqueArc<T> {
750 pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> {
758 pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
763 data <- pin_init::uninit::<T, AllocError>(),
775 impl<T> UniqueArc<MaybeUninit<T>> {
776 /// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it.
777 pub fn write(mut self, value: T) -> UniqueArc<T> {
789 pub unsafe fn assume_init(self) -> UniqueArc<T> {
793 // dropped). The types are compatible because `MaybeUninit<T>` is compatible with `T`.
799 pub fn init_with<E>(mut self, init: impl Init<T, E>) -> core::result::Result<UniqueArc<T>, E> {
811 init: impl PinInit<T, E>,
812 ) -> core::result::Result<Pin<UniqueArc<T>>, E> {
823 impl<T: ?Sized> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
824 fn from(obj: UniqueArc<T>) -> Self {
825 // SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueArc<T>>` (unless `T`
826 // is `Unpin`), so it is ok to convert it to `Pin<UniqueArc<T>>`.
831 impl<T: ?Sized> Deref for UniqueArc<T> {
832 type Target = T;
839 impl<T: ?Sized> DerefMut for UniqueArc<T> {
867 impl<T: ?Sized> Borrow<T> for UniqueArc<T> {
868 fn borrow(&self) -> &T {
892 impl<T: ?Sized> BorrowMut<T> for UniqueArc<T> {
893 fn borrow_mut(&mut self) -> &mut T {
898 impl<T: fmt::Display + ?Sized> fmt::Display for UniqueArc<T> {
904 impl<T: fmt::Display + ?Sized> fmt::Display for Arc<T> {
910 impl<T: fmt::Debug + ?Sized> fmt::Debug for UniqueArc<T> {
916 impl<T: fmt::Debug + ?Sized> fmt::Debug for Arc<T> {