Smithay / calloop

A callback-based Event Loop
MIT License
177 stars 35 forks source link

Need clarification on why `Generic::get_mut` is unsafe #174

Closed mwcampbell closed 7 months ago

mwcampbell commented 7 months ago

I understand that dropping the underlying handle of a Generic source would lead to undefined behavior. But is it even possible to do that without requiring some unsafe code (besides the call to get_mut itself)? For example, File and OwnedFd don't have a close method, or anything equivalent as far as I can tell. An example illustrating why get_mut itself has to be unsafe, that is, how one would be able to drop the underlying handle without any other unsafe code, would be helpful.

ids1024 commented 7 months ago

It's possible to use mem::swap to exchange the referenced value with something of the same type. Then the original value could be dropped.

So it's at least possible in a somewhat contrived scenario.

mwcampbell commented 7 months ago

Ah, hadn't thought about mem::swap or equivalent. That makes sense.