Voultapher/self_cell

Convert self_cell struct back into owner

Closed this issue · 4 comments

Hi,
rental has a way to convert a struct back into the owner (I think it’s calling into_head()). This can be used to e.g. reuse a Vec<u8> after the self-referencing struct is not needed anymore.
Could self_cell add something similar?
I hope implementing something like impl From<$StructName> for $Owner {} is safe to do.

Hi, I've thought about something like into_owner(self) -> $Owner before. And it should be possible. A tricky question how to do this in a way that doesn't introduce a branch into the drop impl. But maybe a mem::forget solution in into_owner could work.

Regarding your second question:

I hope implementing something like impl From<$StructName> for $Owner {} is safe to do.

The library is designend such that literally every kind of use and interaction not marked unsafe should be safe and sound. If you discover a flaw please report it.

@Flakebi I've pushed a commit please let me know if this addresses your use case. You can try out the new version by referencing it like this in your Cargo.toml:

[dependencies]
self_cell = { git = "https://github.com/Voultapher/self_cell" }

Thanks, that is exactly what I need!

The newly released version 0.9.0 https://github.com/Voultapher/self_cell/releases/tag/v0.9.0 contains into_owner