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

Add Arc::from_thin_raw. #54

Open
conradludgate opened this issue Jan 28, 2023 · 1 comment
Open

Add Arc::from_thin_raw. #54

conradludgate opened this issue Jan 28, 2023 · 1 comment

Comments

@conradludgate
Copy link

I have a setup where I want to do

let ptr = thin_arc.with_arc(Arc::clone).into_raw();

// store the raw pointer somewhere. then later

let thin_arc = ThinArc::from_data_raw(ptr);

I would go through Arc::from_raw(ptr).into_thin() but that doesn't work because from_raw needs T: Sized.

What I'm imagining is in this impl there is something like

    /// Reconstruct the Arc<HeaderSliceWithLength<H, [T]>> from a raw pointer obtained from into_raw()
    #[inline]
    pub unsafe fn from_thin_raw(ptr: *const HeaderSliceWithLength<H, [T]>) -> ThinArc<H, T> {
        let data_ptr = ptr as *const [u8] as *const u8;
        let thin_ptr = data_ptr.sub(offset_of!(ArcInner<HeaderSliceWithLength<H, [T; 0]>>, data));
        ThinArc {
            ptr: unsafe {
                ptr::NonNull::new_unchecked(
                    thin_ptr as *mut ArcInner<HeaderSliceWithLength<H, [T; 0]>>,
                )
            },
            phantom: PhantomData,
        }
    }

For context, I was trying to move some of the functionality from https://github.com/conradludgate/futures-buffered/blob/main/src/arc_slice.rs into triomphe.

The core idea is that I have ThinArc<H, Slot> where each Slot stores it's index in the slice.
Given a *const Slot, I can count back the index and get back the original ThinArc<H, Slot>. Almost everything was working, except for the drop() impl of the RawWaker, since I couldn't construct the arc with the correct size to deallocate.

@Manishearth
Copy link
Owner

That seems like a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants