lmonninger
lmonninger

Reputation: 961

When should I use Pin<Arc<T>> in Rust?

I'm pretty new to Rust and have a couple different implementations of a method that includes a closure referencing self. To use the reference in the closure effectively, I've been using Arc<Self> (I am multithreading) and Pin<Arc<Self>>.

I would like to make this method as generally memory efficient as possible. I assume pinning the Arc in memory would help with this. However, (a) I've read that Arcs are pinned and (b) it seems like Pin<Arc<T>> may require additional allocations.

What is Pin<Arc<T>> good for?

Upvotes: 0

Views: 1475

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43842

Adding Pin around some pointer type does not change the behavior of the program. It only adds a restriction on what further code you can write (and even that, only if the T in Pin<Arc<T>> is not Unpin, which most types are).

Therefore, there is no "memory efficiency" to be gained by adding Pin.

The only use of Pin is to allow working with types that require they be pinned to use them, such as Futures.

Upvotes: 3

Related Questions