#What is a Pin

25 messages · Page 1 of 1 (latest)

minor hornet
minor hornet
#

I'm reading that

Pin works in tandem with the Unpin marker. Pinning makes it possible to guarantee that an object implementing !Unpin won't ever be moved

But why would an object move? 🤔

#

Someone has an example ?

prime stratus
#

The explanation there is pretty good ^

hoary plover
#

This is not actually what pin is for, though.

minor hornet
hoary plover
#

The idea is as follows:

  • there are cases, usually around self-referential types, where it would be really bad if a value moved around in memory.
  • as long as all code that manipulates these values is unsafe, this is fine, it can just be careful and manipulate them by pointer.
  • but we want to let safe code manipulate these values too.
  • so, we need a way to prevent safe code holding a reference (or another kind of pointer) to one from Doing Bad Things
  • thus, we created the notion of pinning
minor hornet
#

Alright, I've read a lot of Pinning regarding Future and now Generator

#

It is the same use case ?

hoary plover
#

Yes. Futures need this because they're implemented as generators, more or less.

#

You'll probably find more information by looking into futures though

minor hornet
#

I mean, do we want to pin a Future because, we don't want it to move, or we don't want the inner values to not be moved ?

hoary plover
#

Particularly, into the desugaring of async fn

minor hornet
#

I'll look into it

hoary plover
minor hornet
#

Alright, like a struct ?

#

Thanks both of you @prime stratus @hoary plover 🙏

hoary plover
minor hornet
#

I'll check

#

How do I close the chann ?

#

Just closed or mark as resolved somewhere ?