danielhenrymantilla / stackbox.rs

`&own` pointers in Stable Rust / `RefOwn` / `&mut` with move semantics (and thus drop)
Apache License 2.0
19 stars 2 forks source link

How about adding an `into_inner` alternative that gives back a pointer to the `Slot` as well? #2

Open steffahn opened 3 years ago

steffahn commented 3 years ago

I’m not sure of what to name it. What I mean is a function with the signature

// T: Sized
fn foo(self: StackBox<'frame, T>) -> (T, &'frame mut Slot<T>)

so basically the inverse of new_in.


Perhaps slightly unrelated, but I think that a function with this signature could be pretty useful, too:

fn bar<R, F: FnOnce(StackBox<'_, T>) -> R>(self: StackBox<'frame, T>, f: F) -> (R, &'frame mut Slot<T>)
// remember that
// FnOnce(StackBox<'_, T>) -> R
// is the same as
// for<'a> FnOnce(StackBox<'a, T>) -> R


on a second thought... foo can even be implemented using bar

x.foo() === x.bar(|x| x.into_inner())
danielhenrymantilla commented 3 years ago

I had thought of foo (was gonna call it .take()), but since the Slot is available unless using some of the macro sugar, I didn't feel like it was paramount: the moment the initial StackBox<'frame, _> is dropped, the lifetime 'frame can end, so the &'frame mut borrow of the Slot can end, and it can thus be reused.

That being said, the presence of sugar whereby the initial Slot is unnameable could justify adding .take(), and … .take_with() (I suspect the point of using .take_with() over .take() is for the T : ?Sized case)

danielhenrymantilla commented 3 years ago

🤔 I have marked this a good first issue, although it's implementation requires unsafe. Still, anybody can feel free to submit a PR for this, and I will be around to review the unsafe 🙂 (otherwise I'll probably tackle this this week-end).

HeroicKatora commented 3 years ago

The name StackBox::take sounds appropriate to me, as an associated method.

KillingSpark commented 2 years ago

I'd like to submit

fn unpack(self) -> (T, &'frame mut Slot<T>)

Because that's what you do with boxes when you take out the contents and are left with both the empty box and the stuff