-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Documentation of Arc::from_raw
is unnecessarily restrictive
#106124
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Isn't this code pattern you want documented as valid unnecessarily dangerous, like Why do you want to pass around an |
@rustbot claim |
Beware of the footgun: To call |
I was able to find a different solution using |
Yea, you are absolutely right with that but by passing around ManuallyDrop<Arc> this can be largely avoided (except if one manually drops the |
Why would you need to do I can't imagine avoiding a single atomic increment brings measurable benefit. |
If you want to you can perform measurements, this is the repo: https://github.com/terrarier2111/SwapArc |
Could you use something like |
I can't because that would mean returning a type that's not just |
Couldn't you put an associated type on impl<T: Send + Sync> DataPtrConvert<T> for Arc<T> {
type Weak = sync::sync::Weak<T>;
fn as_weak(&self) -> Self::Weak { todo!() }
} |
Uhm maybe that could work, i am not really experienced in working with associated types. |
I also having the exact use case of this. I'm in the process of migrating the C++ application to Rust and what I did is building a Rust crate as a static library and do something like this: #[no_mangle]
pub unsafe extern "C" fn run(
argc: c_int,
argv: *mut *mut c_char,
cpp: unsafe extern "C" fn(*const App, c_int, *mut *mut c_char) -> c_int
) -> c_int {
let app = Arc::new(App {});
let code = cpp(Arc::as_ptr(&app), argc, argv);
// Graceful shutdown code here.
code
} The |
Seems like what I actually need is something like |
Closing this as this isn't bug or there's anything to be done here |
I think what needs to be done is update |
Uh oh!
There was an error while loading. Please reload this page.
Location
Arc::from_raw
Summary
Arc::from_raw
mentions that the provided pointer has to have been returned fromArc::into_raw
but it doesn't mentionArc::as_ptr
at all. This disallows many usages that depend on not modifying the reference counter while passingArc
around in form of a pointer and converting it back by callingArc::from_raw
on the pointer and wrapping the result inside aManuallyDrop
(as after one conversion round ofArc::into_raw
->ManuallyDrop<Arc::from_raw>
->Arc::as_ptr
would disallow recreating theArc
throughArc::from_raw
)The text was updated successfully, but these errors were encountered: