SabrinaJewson / sabrinajewson.github.io

My website
https://sabrinajewson.org
7 stars 0 forks source link

Strange diff at "Null Lifetime" #2

Closed safinaskar closed 1 year ago

safinaskar commented 1 year ago

Hi. Thanks a lot for post https://sabrinajewson.org/blog/null-lifetime . It contains the following very strange diff:

-   let self_ref = SelfRef {
+   let self_ref = SelfRef {
        this: Cell::new(None),
    };
-   drop(self_ref);
+   &mut self_ref.this;

First two lines are identical, so there is no need to mark them as changed

safinaskar commented 1 year ago

Also, you say "The resulting struct exhibits the same behaviour we talk about later in this section". Unfortunately, this struct is not invariant. And it seems you rely on it being invariant

SabrinaJewson commented 1 year ago

Ah thanks, that was a typo — it was meant to have a let mut in there. I’ve fixed it now.

Unfortunately, this struct is not invariant. And it seems you rely on it being invariant

Right, the struct itself is not invariant. However, the argument still applies because the equivalent of the uwu function would take a unique reference to the struct, making it invariant in the inner lifetime:

struct SelfRef<'this> {
    a: i32,
    b: &'this i32,
}
fn main() {
    let mut self_ref = SelfRef { a: 37, b: &0 };
    uwu(&mut self_ref);
    drop(self_ref); // Comment this line to have it compile
}
fn uwu<'a: 'b, 'b>(self_ref: &'a mut SelfRef<'b>) {
    self_ref.b = &self_ref.a;
}