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

[wgpu-hal] Change the DropCallback API to use FnOnce instead of FnMut #6482

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

jerzywilczek
Copy link
Contributor

Connections
This refines the change I made in #6164

Description
I initially made the DropCallback API use FnMut. This seemed like a good choice at the time, since we only get &mut self access to the DropGuard in its Drop implementation. After some time it became apparent to me that at least a part of the users of this API will want to supply a closure which just holds some kind of a reference-counted wrapper around the corresponding resource that would drop it when called, thus freeing the resource. With the current API they have to do weird stuff to achieve this behavior, because FnMut cannot consume its captures:

let mut vulkan_device_clone: Option<Arc<VulkanDevice>> = Some(vulkan_device.clone());
let wgpu_device = unsafe {
    wgpu_adapter.adapter.device_from_raw(
        ...
        Some(Box::new(move || { vulkan_device_clone.take(); })),
        ...
    )?
};

With this change they could consume the capture:

let vulkan_device_clone: Arc<VulkanDevice> = vulkan_device.clone();
let wgpu_device = unsafe {
    wgpu_adapter.adapter.device_from_raw(
        ...
        Some(Box::new(move || { vulkan_device_clone; })),
        ...
    )?
};
// or more explicitly:
let wgpu_device = unsafe {
    wgpu_adapter.adapter.device_from_raw(
        ...
        Some(Box::new(move || { drop(vulkan_device_clone); })),
        ...
    )?
};

This seems a lot more intuitive.

Testing
Things still compile and there is no difference in test passes.

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
      This just fails
  • Run cargo xtask test to run tests.
    129 tests fail on my machine, with or without the changes
  • Add change to CHANGELOG.md. See simple instructions inside file.

@jerzywilczek jerzywilczek requested a review from a team as a code owner October 31, 2024 13:02
@jerzywilczek jerzywilczek changed the title Change the DropCallback API to use FnOnce instead of FnMut [wgpu-hal] Change the DropCallback API to use FnOnce instead of FnMut Oct 31, 2024
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Nice!

@cwfitzgerald cwfitzgerald merged commit 4e47ba4 into gfx-rs:trunk Oct 31, 2024
27 checks passed
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.

2 participants