Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Rc::clone_raw (and for Arc) #48108

Open
Diggsey opened this issue Feb 9, 2018 · 4 comments
Open

Feature: Rc::clone_raw (and for Arc) #48108

Diggsey opened this issue Feb 9, 2018 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Diggsey
Copy link
Contributor

Diggsey commented Feb 9, 2018

When using from_raw/into_raw functions with Rc, you often want to obtain a new reference to a raw pointer, without taking ownership. At the moment you have to do this dance:

fn clone_raw<T>(ptr: *const T) -> Rc<T> {
    let result = unsafe { Rc::from_raw(ptr) };
    ::std::mem::forget(result.clone());
    result
}

This is quite error prone and makes little sense to anyone trying to read the code. It would be better if the standard library had clone_raw built in for Rc and Arc, and possibly for their weak variants.

@Mark-Simulacrum Mark-Simulacrum added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Feb 9, 2018
@zakarumych
Copy link
Contributor

This looks like a simple and useful feature, but seems forgotten.

@ast-ral
Copy link
Contributor

ast-ral commented Dec 12, 2020

I think a potentially easy solution to this is to just have a function to increment the strong count directly from the pointer, and then have the user make two Rcs from it later, using Rc::from_raw. The current code to increment the strong count on a raw Rc ptr is:

unsafe fn increment_count<T>(ptr: *const T) {
    let rc = Rc::from_raw(ptr);
    std::mem::forget(rc.clone());
    std::mem::forget(rc);
}

Which seems a bit silly to just increment one value.

@Mark-Simulacrum
Copy link
Member

#79285 stabilizes the increment/decrement operators.

@ast-ral
Copy link
Contributor

ast-ral commented Dec 12, 2020

That unfortunately only seems to apply to Arc. (Edit: Upon further review, Rc is mentioned as a follow-up change, sorry to bother.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants