Skip to content

std::sync::Weak::strong_count #50158

Closed
Closed
@SoniEx2

Description

@SoniEx2

I'd like to test if a Weak is done for without potentially incrementing an atomic refcount, because doing so is slow.

I want a faster alternative to Weak.upgrade().is_some().

also currently have this in my code: https://play.rust-lang.org/?gist=339c2c518a2d42aee7e5a585133427eb&version=stable

// since Weaks are completely useless...
// this checks if the arc and weak point to the same value without attempting to upgrade the weak.
// this is "safer" than trying to get the strong count
fn compare_arc_weak<T>(arc: &Arc<T>, weak: &Weak<T>) -> bool {
    unsafe {
        // we assume arc and weak have same size and alignment as usize
        let arcusize: usize = mem::transmute_copy(arc);
        let weakusize: usize = mem::transmute_copy(weak);
        arcusize == weakusize
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions