feat: accept FnMut closures in enumerate_directory#383
Open
revmischa wants to merge 4 commits into
Open
Conversation
SDL_EnumerateDirectory is synchronous, so the callback only needs to live for the duration of the call. Switching from a bare fn pointer to a generic FnMut lets callers capture state (push into a Vec, increment a counter, etc.) without any allocation - userdata is just &mut F. Removes the EnumerateCallback type alias; existing callers passing function names or non-capturing closures keep working unchanged.
A panic from the user callback unwinds through SDL's extern "C" frames, which is UB. Catch it in the trampoline, stash it, and resume_unwind after SDL_EnumerateDirectory returns. Pre-existing issue on the old fn-pointer signature too, but worth fixing now that closures make it easier to hit.
- Drop crate::sdl::init(); SDL_EnumerateDirectory is pure filesystem. - RAII TempDir guard so failed asserts don't leak temp directories. - Atomic counter for uniqueness instead of nanos timestamps. - Panic test now panics mid-iteration to exercise the state.panic.is_some() short-circuit in c_enumerate_directory.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the filesystem::enumerate_directory wrapper to accept capturing callbacks and to prevent Rust panics from unwinding through SDL’s extern "C" callback boundary.
Changes:
- Replace the
fn(&Path, &Path) -> EnumerationResultcallback with a genericF: FnMut(&Path, &Path) -> EnumerationResult. - Add
catch_unwind+ deferredresume_unwindto avoid unwinding across FFI while still propagating user panics. - Add unit tests covering state capture, early stop behavior, panic propagation, and nonexistent paths.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
97
to
+101
| pub use sys::filesystem::SDL_EnumerationResult as EnumerationResult; | ||
|
|
||
| pub type EnumerateCallback = fn(&Path, &Path) -> EnumerationResult; | ||
| struct EnumerateState<F> { | ||
| callback: F, | ||
| panic: Option<Box<dyn std::any::Any + Send + 'static>>, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SDL_EnumerateDirectory is synchronous, so the callback only needs to live for the duration of the call. Switching from
fn(&Path, &Path) -> EnumerationResultto a genericFnMut(&Path, &Path) -> EnumerationResultlets callers capture state (collect entries into aVec, count matches, close over a regex, etc.) with zero allocation.userdatais just&mut F.Also catches panics from the user callback so they don't unwind through SDL's extern "C" frames (UB).
Removes the
EnumerateCallbacktype alias. Existing callers passing function names or non-capturing closures (includingexamples/filesystem.rs) keep working unchanged.