Pinning and immutability (misunderstanding)

I have experienced some behaviour that I cannot explain. I imagine I have misunderstood something, but here is my question:

struct TaskState {
   waker: Waker,
impl TaskState {
    fn set_waker(self: Pin<&Self>, waker: Waker) {
        self.waker = waker; // **Why does this work as self is not Pin<&mut self>?**

My question as in the comment in the code.

I apologise as I accidentally deleted my previous attempt to ask this question while trying to edit it.

That shouldn't work, can you link to a playground example that shows this behavior?

1 Like

Ah, I see - the problem in my original code was that rustc was issuing a diagnostic on something lower in the file (I had not finished a function, so had the wrong return type in a separate impl). So, it didn't give a diagnostic on the code higher up. I was expecting errors for all faults in the code.

Thank you for help. BTW, do you know why it behaves this way?

While where are syntax errors, it's impossible to meaningfully emit type errors, since the code might not have sense at all. Similarly, while where are type errors, it's largely impossible to meaningfully emit borrow checker errors, since, again, you can't be sure that the type borrow checker sees (or would see) is the right one.


That makes sense - I would have thought each function was type checked separately, but I can understand why it wouldn't. Still getting used to Rust, coming from C++ and C#.

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