The Why, What, and How of Pinning in Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 38

  • @mageprometheus
    @mageprometheus 5 ปีที่แล้ว +55

    I wrote my first program in the late 70s so I’ve been learning new languages for quite a while. Your advanced videos are great because the pain seems to drive my old brain to become more plastic. I suppose its the masochistic method of understanding. Thank you so much for this resource Jon.

  • @samuelhurel6402
    @samuelhurel6402 5 ปีที่แล้ว +33

    Sound is gorgeous! Thanks a lot to whoever gifted you this microphone!

  • @xoomayose
    @xoomayose 5 ปีที่แล้ว +33

    I think that an easier way to see why Pin is important is with a simple struct like
    struct Packet {
    skb: [u8; 512],
    header: &'skb [u8],
    body: &'skb [u8],
    }
    `header` and `body` are slices to bytes in `skb`.
    Using futures as an example of self-referential structs can be confusing, because they contain much more details.

    • @matthiask1637
      @matthiask1637 5 ปีที่แล้ว +4

      So skb needs to be pinned?

    • @jonhoo
      @jonhoo  5 ปีที่แล้ว +14

      So, this is what I was trying to get at with the Parsed example :) But more examples is helpful!

    • @a_commenter
      @a_commenter ปีที่แล้ว +5

      @@jonhoo I liked the Parsed example, but I feel like you moved off of it too quickly before you really got into the meat of Pin

  • @alexwhite6133
    @alexwhite6133 5 ปีที่แล้ว +26

    Wow, what a guy.
    Enormous knowledge paired with understandable explanation.
    Now I know what it really means to be a programmer!
    Jon, thanks for the videos and your time.
    Unbelievable...

  • @iwikal
    @iwikal 4 ปีที่แล้ว +14

    2:12:10 Automatically implementing Unpin for async blocks and functions that don't need pinning is probably something that could cause problems for API stability. If a library author exposes an async function that gets impl Unpin automatically because it doesn't currently have any self-references, if they want to change the implementation to something that isn't Unpin, that would be a breaking change.

    • @bigdog-t3k
      @bigdog-t3k ปีที่แล้ว +3

      That’s a pretty great point about API stability. This actually does cause problems with Send and Sync because they are automatically implemented for async blocks depending on the block’s content. So the API contract of an async function’s return type can change purely based on its body.
      I’ve encountered this before in a backend project where I used an itertools group_by function, which for some reason isn’t Send, in an async function. Didn’t hit any compiler errors while I was working on that crate, then when I tried to run the server there I got a gnarly error because the fully composed futures returned from the server’s router functions weren’t Send and that’s no bueno with tokio.
      And additionally, even if you don’t hold those !Send types across await points, the async block generated is still !Send. That’s what’ll really trip you up.

  • @DHorse
    @DHorse 5 หลายเดือนก่อน

    Thanks Jon. The implementation was quite challenging but an essential topic. You pinned it! ... I mean nailed it!

  • @encody
    @encody 2 ปีที่แล้ว

    Dude, thank you so much for this video. I work with Rust professionally and I'm in awe of your expertise.

  • @SouravSingh-zr6dx
    @SouravSingh-zr6dx ปีที่แล้ว +3

    Hi Jon , Just want to say thank you for sharing all this knowledge with us. You are amazing man🙏

  • @matthiasdebernardini3388
    @matthiasdebernardini3388 2 ปีที่แล้ว

    this video is so good, thanks so much for sharing - its just what I needed!

  • @wayneashleyberry
    @wayneashleyberry 5 ปีที่แล้ว +6

    This new microphone sounds fantastic!

  • @Mrgreatestfreakout
    @Mrgreatestfreakout 3 ปีที่แล้ว +1

    Hey Jon! Always appreciate your videos hehe...question about your real-time compile messages(?) that you get in vim. What did you install? I'm using jetbrain rust plugin but you only get detailed error only when you compile

  • @ezengondolkozom3700
    @ezengondolkozom3700 5 ปีที่แล้ว +4

    God your vim looks orgasmically good.

  • @swfsql
    @swfsql 3 ปีที่แล้ว +1

    In 1:26:20, shouldn't this only be unsound if you actually called a _poll()_ on _x_ after you created it? Because if you pin and still _mem::replace()_ it, you didn't yet actually depended on that pinning right?
    btw you often explicitly inserted _poll()_ calls around some _mem::replace()_ after pinning, so I wondered if in this case you didn't?
    (Thx for the explanation!! XD)

  • @dbrgn
    @dbrgn 4 ปีที่แล้ว +1

    Thanks Jon, this is really really helpful.

  • @jeffg4686
    @jeffg4686 3 ปีที่แล้ว +1

    Good stuff, thanks for sharing. How do you know when you should "take" a value from an option rather than just unwrapping it. I know it's about ownership, but don't really understand why the necessity. Is this basically saying that the function call the taken value is passed to is a pass by value semantics, and can't take a borrow/ref so is required in this case, or is there more to it? I see this occasionally and always confused me. In this video, referencing code at 46:37 - self.value.take().unwrap() . Thanks for any thoughts.

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +1

      You use take if you want to leave None behind in the Option that you're calling take on. unwrap() on the other hand consumers the Option, and makes it so that it cannot be used by anyone ever again.

  • @benmuker6300
    @benmuker6300 5 ปีที่แล้ว +2

    from my understanding, the only reason, why pinning is neccessary is, because it is forbidden to move a struct, that contains a pointer to a field of itself, because that move does not change the value of the pointer and so the pointer points to the old memory location ando nobody knows about its contents anymore.
    But if i used a relative pointer instead, would that get rid of the problem, because when i move the struckt with the (relative) pointer to a field of itself, the relative pointer now still points to the currect location, because the layout of the struct does not change.
    I think that I overlook someting so please clarify me

    • @jonhoo
      @jonhoo  5 ปีที่แล้ว +1

      Ah, so, in the specific case where the target of the pointer is stored within the struct, that's fine, but in the general case, relative pointers are not sufficient. For example, consider a struct that holds a Vec (so a heap-allocated vector) and a type with pointers into that Vec. There's no relative pointer you can use there to make moves okay

    • @XiaoZhang-x1a0
      @XiaoZhang-x1a0 4 ปีที่แล้ว

      @@jonhoo Hi. If the Vec is on heap, moving the struct will not move the Vec so the pointer should be still valid?

    • @iwikal
      @iwikal 4 ปีที่แล้ว

      @@XiaoZhang-x1a0 In the case of a Vec, relative pointers hurt rather than help because when you move the struct, the Vec's buffer stays in place on the heap, so relative pointers into it are no longer valid. If you used absolute pointers, mutating the Vec risks invalidating them, because it might reallocate or simply drop entries you're pointing to.

  • @astrolemonade349
    @astrolemonade349 5 ปีที่แล้ว +2

    What linux distro are you using ? How do you make the menus and everything be at bottom ?

    • @jonhoo
      @jonhoo  5 ปีที่แล้ว

      I have a video on that over at th-cam.com/video/ycMiMDHopNc/w-d-xo.html&lc=Ugw7mC8ZYKfWnJwL9Tx4AaABAg :)

  • @zaooo
    @zaooo 5 ปีที่แล้ว +1

    Caution for viewers with ears, there's a loud mic thud at 2:26:09.

    • @jonhoo
      @jonhoo  5 ปีที่แล้ว +2

      Yeah, I've added a shock mount to my wish list to deal with some of those kinds of issues. The new mic also picks up keyboard sounds more than the old one did, which the shock mount should also help with.

    • @DHorse
      @DHorse 5 ปีที่แล้ว

      Thanks, no problem. The lid on the jar is tightly sealed.

  • @bocckoka
    @bocckoka 3 ปีที่แล้ว

    moving a Pin refers to passing on ownership or relocating in memory?

    • @jadonbelezos2583
      @jadonbelezos2583 3 ปีที่แล้ว

      as i understand it which maybe wrong your passing ownership. the actual self referential struct hasnt been moved. just the owning pointer/mutable reference has!

  • @xthebumpx
    @xthebumpx 5 ปีที่แล้ว

    But the real important question is: how you get the address bar/tabs on firefox at the bottom of the window?

    • @jonhoo
      @jonhoo  5 ปีที่แล้ว +3

      I actually go through my whole desktop setup in th-cam.com/video/ycMiMDHopNc/w-d-xo.html :)

  • @kshitijjain8544
    @kshitijjain8544 3 ปีที่แล้ว

    At 55:44, you say because FooFuture is not DerefMut. Did you mean to say FooFuture is not Unpin?

  • @lukekhamilton
    @lukekhamilton 4 ปีที่แล้ว

    What's your vimrc config bro? Feel things I want to check out here! Thanks :)

    • @jonhoo
      @jonhoo  4 ปีที่แล้ว

      I go through my setup in a decent amount of details here: th-cam.com/video/ycMiMDHopNc/w-d-xo.html. My full configs are here: github.com/jonhoo/configs

  • @theodimopoulos8039
    @theodimopoulos8039 5 ปีที่แล้ว

    Thx vm!