Skip to content

The documentation of Arc's Weak::upgrade does not mention dissosiation #154936

@quixoticaxis

Description

@quixoticaxis

Location (URL)

https://doc.rust-lang.org/std/sync/struct.Weak.html#method.upgrade

Summary

The documentation says:

Attempts to upgrade the Weak pointer to an Arc, delaying dropping of the inner value if successful.
Returns None if the inner value has since been dropped.

It is possible that the value is not dropped, but the Weak reference is dissosiated due to the call to Arc::make_mute and upgrade would still return None:

use std::sync::Arc;

fn main() {
    let mut r = Arc::new(S { num: 5 });
    
    println!("Having {}", r.num);

    let wr = Arc::downgrade(&r);

    let s = Arc::make_mut(&mut r);

    s.num += 1;

    assert!(wr.upgrade().is_none());
}

#[derive(Debug)]
pub struct S {
    pub num: i32,
}

impl Clone for S {
    fn clone(&self) -> Self {
        Self { num: self.num }
    }
}

impl Drop for S {
    fn drop(&mut self) {
        println!("Dropping {}", self.num);
    }
}

Should it be mentioned in the documentation of Weak::upgrade that it may return None for dessosiated references which does not necessarily imply dropping?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions