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

Change EventFn to take FnMut #333

Merged
merged 3 commits into from Jun 9, 2021
Merged

Change EventFn to take FnMut #333

merged 3 commits into from Jun 9, 2021

Conversation

erickt
Copy link
Contributor

@erickt erickt commented Jun 8, 2021

This changes EventFn to work with FnMut, rather than Fn. The advantage of this is that this allows callers to use state in the EventFn, rather than having to use interior mutability. This is helpful particularly for getting notify to work with async rust, since futures::channel::mpsc's methods all take &mut self.

The downside of this change is that it limits the ability of notify to share EventFn across multiple threads, but all the backends either guarantee only a single background thread, or they already use an Arc<Mutex<...>> to protect EventFn.

This changes EventFn to work with FnMut, rather than Fn. The advantage of
this is that this allows callers to use state in the EventFn, rather than
having to use interior mutability. This is helpful particularly for
getting notify to work with async rust, since `futures::channel::mpsc`'s
methods all take `&mut self`.

The downside of this change is that it limits the ability of notify to
share `EventFn` across multiple threads, but all the backends either
guarantee only a single background thread, or they already use an
`Arc<Mutex<...>>` to protect `EventFn`.
This adds a new example on how to lift events from the synchronous code
into async rust.
Copy link
Member

@0xpr03 0xpr03 left a comment

Choose a reason for hiding this comment

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

but all the backends either guarantee only a single background thread, or they already use an Arc<Mutex<...>> to protect EventFn

That looks fine but can/do we somehow guarantee/note that we now expect only a single Fn caller at any point in time in the backends?

@0xpr03
Copy link
Member

0xpr03 commented Jun 8, 2021

Also thanks for all the changes. Based on your contributions, do you maybe want to be part of the team to maintain this crate, especially since you seem to have an apple device to test the fsevent part ?

@erickt
Copy link
Contributor Author

erickt commented Jun 8, 2021

That looks fine but can/do we somehow guarantee/note that we now expect only a single Fn caller at any point in time in the backends?

That should be implied by using FnMut, which will guarantee only one caller unless we make a mistake with unsafe and alias the function pointer. I looked through our use of EventFn, and it looks like all watchers but INotifyWatcher wrap their event_fn in Arc<Mutex<...>>. INotifyWatcher can get away with it only ever uses one background thread, so it can transfer ownership of the EventFn into it. We could change all the other watchers to use this pattern, which I think would allow us to get rid of the mutex.

Also thanks for all the changes. Based on your contributions, do you maybe want to be part of the team to maintain this crate, especially since you seem to have an apple device to test the fsevent part ?

Thanks for the offer! I can try to help here and there, but I don't think I can commit to maintenance at this time. I think with these last couple changes addresses all my needs, and I can unblock my actual use of notify :)

@0xpr03
Copy link
Member

0xpr03 commented Jun 9, 2021

We could change all the other watchers to use this pattern, which I think would allow us to get rid of the mutex.

I think we'll leave that for a later PR and merge this one in the meantime.

I think with these last couple changes addresses all my needs, and I can unblock my actual use of notify

No problem, let's see when your next issue comes up ;)

@0xpr03 0xpr03 merged commit 6f42766 into notify-rs:main Jun 9, 2021
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.

None yet

2 participants