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

Make CFRunLoopMode a safe wrapper with a lifetime #650

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kevinmehall
Copy link

Fixes #648: the safe methods that take CFRunLoopMode are currently unsound because it is a raw pointer that is not necessarily valid.

As a bonus, this allows use of kCFRunLoopCommonModes and kCFRunLoopDefaultMode constants without unsafe.

I considered just making mode a &CFString, but it seems that the implementation compares the pointer values, so this way ensures that the constants are passed as the pointers it expects. I don't understand the purpose or usage of mode very well, so correct me if this is not ideal.

@waywardmonkeys
Copy link
Collaborator

Hello! Sorry for the long delay.

Would you be able to rebase this forward and fix the merge conflicts? If so, we'd like to review it and see about landing it.

@kevinmehall
Copy link
Author

Rebased

@kevinmehall
Copy link
Author

Clippy doesn't like methods called default:

error: method `default` can be confused for the standard trait method `std::default::Default::default`
  --> core-foundation/src/runloop.rs:37:5
   |
37 | /     pub fn default() -> CFRunLoopMode<'static> {
38 | |         unsafe { CFRunLoopMode(kCFRunLoopDefaultMode, PhantomData) }
39 | |     }
   | |_____^
   |
   = help: consider implementing the trait `std::default::Default` or choosing a less ambiguous method name
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
   = note: `-D clippy::should-implement-trait` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(clippy::should_implement_trait)]`

so I renamed the constructors to CFRunLoopMode::default_mode() and CFRunLoopMode::common_modes(). But if you think that's too redundant, we could also ignore the clippy lint.

@waywardmonkeys
Copy link
Collaborator

I think the naming, while redundant, is fine.

@kevinmehall
Copy link
Author

Pushed again to fix rustfmt on a line that default_mode pushed over the length limit.

(FYI, there's a GitHub Actions setting for "Require approval for first-time contributors who are new to GitHub" that is less annoying than the default "Require approval for first-time contributors ")

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

Successfully merging this pull request may close these issues.

Safe CFRunLoop methods accept and dereference a raw pointer
2 participants