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 add_outlives method to Poller #171

Closed

Conversation

BrankoKrstic
Copy link

resolves #161

The previous implementation only implemented unsafe methods for registering file descriptors with Poller. This implements an add_outlives method that can guarantee safety by accepting a reference to an AsSource which outlives the Poller. Since file descriptors are guaranteed to be valid through the lifetime of the Poller, there is no need to ever delete to maintain soundness.

Please advise if I should change anything.

The previous implementation only implemented unsafe methods for registering file descriptors with Poller. This implements an add_outlives method that can guarantee safety by accepting a reference to an AsSource which outlives the Poller.
Since file descriptors are guaranteed to be valid through the lifetime of the Poller, there is no need to ever delete to maintain soundness.
Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, as Poller now has a lifetime parameter. I was envisioning something like this:

pub fn add_outlives<'poll, 'src, T: AsSource>(&'poll self, source: &'src T, interest: Event) -> io::Result<()>
where 'src: 'poll
 { /* ... */ }

This way the reference to the source is guaranteed to outlive the Poller.

@BrankoKrstic
Copy link
Author

I tried this initially, but I don't think it will work if the source lifetime is not somehow tied to the Poller struct. The borrow checker will just shorten the poller lifetime inside add_outlives.

With the above example this compiles:

let poller = Poller::new().unwrap();

{
    let file = File::open("./file.txt").unwrap();
    let _ = poller.add_outlives(&file, Event::all(7));
    drop(file);
}
println!("{:?}", poller);

@notgull
Copy link
Member

notgull commented Dec 26, 2023

That's disappointing. I was under the impression that the borrow checker would catch this.

Still, thanks for checking!

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

Successfully merging this pull request may close these issues.

Add a way to add a source that outlives the Poller
2 participants