smol-rs / polling

Portable interface to epoll, kqueue, event ports, and wepoll
Apache License 2.0
551 stars 68 forks source link

Add a way to add a source that outlives the Poller #161

Closed notgull closed 10 months ago

notgull commented 1 year ago

If a specific file descriptor or source outlives the Poller, it can be added to it without any concerns of whether or not it will be dropped. It would be nice to have an add_outlives() function that takes a source that is guaranteed to outlive the Poller, through lifetimes. While this doesn't work for the common use cases (async-io and calloop), for simple, shorter scale use cases this could be used to avoid unsafe code (alacritty).

cc https://github.com/psychon/x11rb/issues/891

psychon commented 1 year ago

I don't think this is possible.

I can just lie in my AsRawSource implementation and always return a random number. Just because my struct is alive does not mean the underlying FD did not change.

Another random idea would be to break this with e.g. a Cell<File>. I can change the FD without any unsafe code even though the containing struct is still borrowed.

notgull commented 1 year ago

While you can safely lie in AsRawSource, it is unsound to lie in AsSource, which is what I would use for this method.

notgull commented 10 months ago

See https://github.com/smol-rs/polling/pull/171#issuecomment-1869185012. I don't think this is possible safely, without a breaking change.