Is there an Advantage of Pin<Arc<T>> over Pin<T>/ Pin<Box<T>> / Arc<T>

Hi there,

I'm wondering, asPin means (as far as I understood) the inner type is pinned in memory and not move-able which sounds to me that it would also be safe to use it across threads?! It implements Clone as Arc does.

Could anyone roughly elaborate what the high-level advantages and most suitable use cases are for the different variants:
Pin<Arc<T>>
Pin<Box<T>>
Arc<T>
?

My use case is that I'd like to pass the same structure into several closures that do execute somehow independently. This works fine with all three options - at least the compiler is fine :slight_smile: but I'm wondering what would be a "best practice" kind of thing from an expert point of view ...

So any light shedding is much appreciated :slight_smile:

Don't use pin unless you are writing a future, or something self referential. It's not what you need. Pin is a sharp tool designed to make self-referential types sound to use. These types come up naturally in async/await, when you hold borrows across await points.

Pin<P> takes a pointer and guarantees that the pointee will never change memory addresses till drop. For example, Pin<Box<T>> means that T is pinned. Types that are Unpin opt-out of this guarantee, and almost all self referential types are not Unpin

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.