From 0932ad11b57a1c2b42771d6c970329d29dd00f60 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Fri, 13 Feb 2026 11:09:07 +0100 Subject: [PATCH 1/2] all: use run-on-drop crate --- Cargo.lock | 1 + Cargo.toml | 1 + src/backends/metal/video.rs | 15 ++++---- src/gfx_apis/vulkan/blend_buffer.rs | 4 +-- src/gfx_apis/vulkan/bo_allocator.rs | 11 +++--- src/gfx_apis/vulkan/buffer_cache.rs | 14 ++++---- src/gfx_apis/vulkan/device.rs | 5 +-- src/gfx_apis/vulkan/dmabuf_buffer.rs | 6 ++-- src/gfx_apis/vulkan/image.rs | 10 +++--- src/gfx_apis/vulkan/instance.rs | 8 ++--- src/gfx_apis/vulkan/pipeline.rs | 12 +++---- src/gfx_apis/vulkan/shm_image.rs | 5 +-- src/gfx_apis/vulkan/staging.rs | 11 +++--- src/ifs/wl_seat.rs | 4 +-- src/io_uring/ops/read_write_no_cancel.rs | 4 +-- src/utils.rs | 1 - src/utils/clone3.rs | 5 +-- src/utils/on_drop.rs | 46 ------------------------ src/xwayland.rs | 7 ++-- 19 files changed, 60 insertions(+), 110 deletions(-) delete mode 100644 src/utils/on_drop.rs diff --git a/Cargo.lock b/Cargo.lock index 637364fd..f72bf09d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -682,6 +682,7 @@ dependencies = [ "rand 0.10.0", "regex", "repc", + "run-on-drop", "rustc-demangle", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 702b3375..2a7f6bf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ cfg-if = "1.0.0" opera = "1.0.1" with_builtin_macros = "0.1.0" blake3 = "1.8.2" +run-on-drop = "1.0.0" [build-dependencies] repc = "0.1.1" diff --git a/src/backends/metal/video.rs b/src/backends/metal/video.rs index 8423555f..9b10c509 100644 --- a/src/backends/metal/video.rs +++ b/src/backends/metal/video.rs @@ -41,7 +41,7 @@ use { asyncevent::AsyncEvent, binary_search_map::BinarySearchMap, bitflags::BitflagsExt, cell_ext::CellExt, clonecell::CloneCell, copyhashmap::CopyHashMap, errorfmt::ErrorFmt, geometric_decay::GeometricDecay, numcell::NumCell, on_change::OnChange, - on_drop::OnDrop2, opaque_cell::OpaqueCell, ordered_float::F64, oserror::OsError, + opaque_cell::OpaqueCell, ordered_float::F64, oserror::OsError, }, video::{ INVALID_MODIFIER, Modifier, @@ -62,6 +62,7 @@ use { indexmap::{IndexMap, IndexSet, indexset}, isnt::std_1::collections::IsntHashMapExt, jay_config::video::GfxApi, + run_on_drop::on_drop, std::{ cell::{Cell, RefCell}, collections::hash_map::Entry, @@ -2735,7 +2736,7 @@ impl MetalBackend { let Some(dev_gfx_format) = dev_gfx_formats.get(&format.drm) else { return Err(ScanoutBufferErrorKind::SodUnsupportedFormat); }; - let send_dev_gfx_write_modifiers = OnDrop2::new(|| { + let send_dev_gfx_write_modifiers = on_drop(|| { *dbg_dev_gfx_write_modifiers = Some(dev_gfx_format.write_modifiers.keys().copied().collect()) }); @@ -2745,7 +2746,7 @@ impl MetalBackend { .filter(|(m, _)| plane_modifiers.contains(*m)) .map(|(m, v)| (*m, v)) .collect(); - let send_dev_modifiers_possible = OnDrop2::new(|| { + let send_dev_modifiers_possible = on_drop(|| { *dbg_dev_modifiers_possible = Some(possible_modifiers.keys().copied().collect()) }); if possible_modifiers.is_empty() { @@ -2804,7 +2805,7 @@ impl MetalBackend { }; (None, render_tex, None, None) } else { - send_render_dev_name = Some(OnDrop2::new(|| { + send_render_dev_name = Some(on_drop(|| { *dbg_render_name = Some(render_ctx.devnode.as_bytes().as_bstr().to_string()); })); // Create a _bridge_ BO in the render device @@ -2813,11 +2814,11 @@ impl MetalBackend { None => return Err(ScanoutBufferErrorKind::RenderUnsupportedFormat), Some(f) => f, }; - send_render_gfx_write_modifiers = Some(OnDrop2::new(|| { + send_render_gfx_write_modifiers = Some(on_drop(|| { *dbg_render_gfx_write_modifiers = Some(render_gfx_format.write_modifiers.keys().copied().collect()) })); - send_dev_gfx_read_modifiers = Some(OnDrop2::new(|| { + send_dev_gfx_read_modifiers = Some(on_drop(|| { *dbg_dev_gfx_read_modifiers = Some(dev_gfx_format.read_modifiers.clone()); })); render_possible_modifiers = render_gfx_format @@ -2826,7 +2827,7 @@ impl MetalBackend { .filter(|(m, _)| dev_gfx_format.read_modifiers.contains(*m)) .map(|(m, v)| (*m, v)) .collect(); - send_render_possible_modifiers = Some(OnDrop2::new(|| { + send_render_possible_modifiers = Some(on_drop(|| { *dbg_render_modifiers_possible = Some(render_possible_modifiers.keys().copied().collect()) })); diff --git a/src/gfx_apis/vulkan/blend_buffer.rs b/src/gfx_apis/vulkan/blend_buffer.rs index 9bc25f98..c88b202c 100644 --- a/src/gfx_apis/vulkan/blend_buffer.rs +++ b/src/gfx_apis/vulkan/blend_buffer.rs @@ -7,13 +7,13 @@ use { image::{QueueFamily, QueueState, VulkanImage, VulkanImageMemory}, renderer::VulkanRenderer, }, - utils::on_drop::OnDrop, }, ash::vk::{ Extent3D, ImageAspectFlags, ImageCreateInfo, ImageLayout, ImageSubresourceRange, ImageTiling, ImageType, ImageViewCreateInfo, ImageViewType, SampleCountFlags, SharingMode, }, gpu_alloc::UsageFlags, + run_on_drop::on_drop, std::{cell::Cell, collections::hash_map::Entry, rc::Rc}, }; @@ -60,7 +60,7 @@ impl VulkanRenderer { .usage(usage); let image = unsafe { self.device.device.create_image(&create_info, None) }; let image = image.map_err(VulkanError::CreateImage)?; - let destroy_image = OnDrop(|| unsafe { self.device.device.destroy_image(image, None) }); + let destroy_image = on_drop(|| unsafe { self.device.device.destroy_image(image, None) }); let memory_requirements = unsafe { self.device.device.get_image_memory_requirements(image) }; let allocation = diff --git a/src/gfx_apis/vulkan/bo_allocator.rs b/src/gfx_apis/vulkan/bo_allocator.rs index ccee4310..e8905df2 100644 --- a/src/gfx_apis/vulkan/bo_allocator.rs +++ b/src/gfx_apis/vulkan/bo_allocator.rs @@ -10,7 +10,7 @@ use { device::VulkanDevice, format::VulkanFormat, renderer::image_barrier, staging::VulkanStagingBuffer, }, - utils::{errorfmt::ErrorFmt, on_drop::OnDrop}, + utils::errorfmt::ErrorFmt, video::{ Modifier, dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec}, @@ -33,6 +33,7 @@ use { PipelineStageFlags2, QUEUE_FAMILY_FOREIGN_EXT, SampleCountFlags, SharingMode, SubmitInfo2, SubresourceLayout, }, + run_on_drop::on_drop, std::{rc::Rc, slice}, uapi::OwnedFd, }; @@ -140,7 +141,7 @@ impl VulkanBoAllocator { let res = unsafe { data.device.device.create_image(&create_info, None) }; res.map_err(VulkanError::CreateImage)? }; - let destroy_image = OnDrop(|| unsafe { data.device.device.destroy_image(image, None) }); + let destroy_image = on_drop(|| unsafe { data.device.device.destroy_image(image, None) }); let modifier = { let mut props = ImageDrmFormatModifierPropertiesEXT::default(); unsafe { @@ -187,7 +188,7 @@ impl VulkanBoAllocator { }; memory.map_err(VulkanError::AllocateMemory)? }; - let destroy_memory = OnDrop(|| unsafe { data.device.device.free_memory(memory, None) }); + let destroy_memory = on_drop(|| unsafe { data.device.device.free_memory(memory, None) }); unsafe { data.device .device @@ -326,7 +327,7 @@ impl VulkanBoAllocator { let res = unsafe { data.device.device.create_image(&create_info, None) }; res.map_err(VulkanError::CreateImage)? }; - let destroy_image = OnDrop(|| unsafe { data.device.device.destroy_image(image, None) }); + let destroy_image = on_drop(|| unsafe { data.device.device.destroy_image(image, None) }); let num_device_memories = match disjoint { true => dmabuf.planes.len(), false => 1, @@ -397,7 +398,7 @@ impl VulkanBoAllocator { let device_memory = device_memory.map_err(VulkanError::AllocateMemory)?; fd.unwrap(); device_memories.push(device_memory); - free_device_memories.push(OnDrop(move || unsafe { + free_device_memories.push(on_drop(move || unsafe { data.device.device.free_memory(device_memory, None) })); } diff --git a/src/gfx_apis/vulkan/buffer_cache.rs b/src/gfx_apis/vulkan/buffer_cache.rs index 58d43a01..780c1c3e 100644 --- a/src/gfx_apis/vulkan/buffer_cache.rs +++ b/src/gfx_apis/vulkan/buffer_cache.rs @@ -1,17 +1,15 @@ use { - crate::{ - gfx_apis::vulkan::{ - VulkanError, - allocator::{VulkanAllocation, VulkanAllocator}, - device::VulkanDevice, - }, - utils::on_drop::OnDrop, + crate::gfx_apis::vulkan::{ + VulkanError, + allocator::{VulkanAllocation, VulkanAllocator}, + device::VulkanDevice, }, ash::vk::{ Buffer, BufferCreateInfo, BufferDeviceAddressInfo, BufferUsageFlags, DeviceAddress, DeviceSize, }, gpu_alloc::UsageFlags, + run_on_drop::on_drop, std::{cell::RefCell, mem::ManuallyDrop, ops::Deref, rc::Rc}, uapi::Packed, }; @@ -119,7 +117,7 @@ impl VulkanBufferCache { .map_err(VulkanError::CreateBuffer)? } }; - let destroy_buffer = OnDrop(|| unsafe { self.device.device.destroy_buffer(buffer, None) }); + let destroy_buffer = on_drop(|| unsafe { self.device.device.destroy_buffer(buffer, None) }); let mut memory_requirements = unsafe { self.device.device.get_buffer_memory_requirements(buffer) }; memory_requirements.alignment = memory_requirements.alignment.max(self.min_alignment); diff --git a/src/gfx_apis/vulkan/device.rs b/src/gfx_apis/vulkan/device.rs index 955dfb79..33debdfc 100644 --- a/src/gfx_apis/vulkan/device.rs +++ b/src/gfx_apis/vulkan/device.rs @@ -10,7 +10,7 @@ use { map_extension_properties, }, }, - utils::{bitflags::BitflagsExt, on_drop::OnDrop}, + utils::bitflags::BitflagsExt, video::{ dmabuf::DmaBufIds, drm::{Drm, sync_obj::SyncObjCtx}, @@ -45,6 +45,7 @@ use { }, }, isnt::std_1::collections::IsntHashMapExt, + run_on_drop::on_drop, std::{ cell::Cell, ffi::{CStr, CString}, @@ -447,7 +448,7 @@ impl VulkanInstance { Ok(d) => d, Err(e) => return Err(VulkanError::CreateDevice(e)), }; - let destroy_device = OnDrop(|| unsafe { device.destroy_device(None) }); + let destroy_device = on_drop(|| unsafe { device.destroy_device(None) }); let blend_limits = self.load_blend_format_limits(phy_dev)?; let formats = self.load_formats(phy_dev)?; let supports_xrgb8888 = formats diff --git a/src/gfx_apis/vulkan/dmabuf_buffer.rs b/src/gfx_apis/vulkan/dmabuf_buffer.rs index 91f8aaae..5178c660 100644 --- a/src/gfx_apis/vulkan/dmabuf_buffer.rs +++ b/src/gfx_apis/vulkan/dmabuf_buffer.rs @@ -3,7 +3,6 @@ use { format::Format, gfx_api::GfxBuffer, gfx_apis::vulkan::{VulkanError, device::VulkanDevice}, - utils::on_drop::OnDrop, }, ash::{ Device, @@ -13,6 +12,7 @@ use { MemoryDedicatedAllocateInfo, MemoryFdPropertiesKHR, MemoryPropertyFlags, }, }, + run_on_drop::on_drop, std::{any::Any, rc::Rc}, uapi::OwnedFd, }; @@ -61,7 +61,7 @@ impl VulkanDevice { .map_err(VulkanError::CreateBuffer)? } }; - let destroy_buffer = OnDrop(|| unsafe { self.device.destroy_buffer(buffer, None) }); + let destroy_buffer = on_drop(|| unsafe { self.device.destroy_buffer(buffer, None) }); let requirements = unsafe { self.device.get_buffer_memory_requirements(buffer) }; let memory_type = self.find_memory_type( MemoryPropertyFlags::HOST_VISIBLE, @@ -89,7 +89,7 @@ impl VulkanDevice { } }; fd.unwrap(); - let free_memory = OnDrop(|| unsafe { self.device.free_memory(memory, None) }); + let free_memory = on_drop(|| unsafe { self.device.free_memory(memory, None) }); unsafe { self.device .bind_buffer_memory(buffer, memory, 0) diff --git a/src/gfx_apis/vulkan/image.rs b/src/gfx_apis/vulkan/image.rs index 45da6861..e4397d02 100644 --- a/src/gfx_apis/vulkan/image.rs +++ b/src/gfx_apis/vulkan/image.rs @@ -15,7 +15,6 @@ use { }, rect::Region, theme::Color, - utils::on_drop::OnDrop, video::dmabuf::{DmaBuf, PlaneVec}, }, ash::vk::{ @@ -31,6 +30,7 @@ use { SubresourceLayout, }, gpu_alloc::UsageFlags, + run_on_drop::on_drop, std::{ cell::Cell, fmt::{Debug, Formatter}, @@ -349,7 +349,7 @@ impl VulkanDmaBufImageTemplate { let image = unsafe { device.device.create_image(&create_info, None) }; image.map_err(VulkanError::CreateImage)? }; - let destroy_image = OnDrop(|| unsafe { device.device.destroy_image(image, None) }); + let destroy_image = on_drop(|| unsafe { device.device.destroy_image(image, None) }); let num_device_memories = match self.disjoint { true => self.dmabuf.planes.len(), false => 1, @@ -419,7 +419,7 @@ impl VulkanDmaBufImageTemplate { let device_memory = device_memory.map_err(VulkanError::AllocateMemory)?; fd.unwrap(); device_memories.push(device_memory); - free_device_memories.push(OnDrop(move || unsafe { + free_device_memories.push(on_drop(move || unsafe { device.device.free_memory(device_memory, None) })); } @@ -440,7 +440,7 @@ impl VulkanDmaBufImageTemplate { if for_rendering && self.render_needs_bridge { let (bridge_image, allocation) = self.create_bridge()?; primary_image = bridge_image; - destroy_bridge_image = Some(OnDrop(|| unsafe { + destroy_bridge_image = Some(on_drop(|| unsafe { device.device.destroy_image(primary_image, None) })); bridge = Some(VulkanFramebufferBridge { @@ -496,7 +496,7 @@ impl VulkanDmaBufImageTemplate { let image = unsafe { self.renderer.device.device.create_image(&create_info, None) }; let image = image.map_err(VulkanError::CreateImage)?; let destroy_image = - OnDrop(|| unsafe { self.renderer.device.device.destroy_image(image, None) }); + on_drop(|| unsafe { self.renderer.device.device.destroy_image(image, None) }); let memory_requirements = unsafe { self.renderer .device diff --git a/src/gfx_apis/vulkan/instance.rs b/src/gfx_apis/vulkan/instance.rs index 53b76223..98852381 100644 --- a/src/gfx_apis/vulkan/instance.rs +++ b/src/gfx_apis/vulkan/instance.rs @@ -1,8 +1,5 @@ use { - crate::{ - gfx_apis::vulkan::{VULKAN_VALIDATION, VulkanError}, - utils::on_drop::OnDrop, - }, + crate::gfx_apis::vulkan::{VULKAN_VALIDATION, VulkanError}, ahash::{AHashMap, AHashSet}, ash::{ Entry, Instance, LoadingError, @@ -18,6 +15,7 @@ use { isnt::std_1::collections::IsntHashMapExt, log::Level, once_cell::sync::Lazy, + run_on_drop::on_drop, std::{ ffi::{CStr, CString, c_void}, fmt::{Display, Formatter}, @@ -108,7 +106,7 @@ impl VulkanInstance { Ok(i) => i, Err(e) => return Err(VulkanError::CreateInstance(e)), }; - let destroy_instance = OnDrop(|| unsafe { instance.destroy_instance(None) }); + let destroy_instance = on_drop(|| unsafe { instance.destroy_instance(None) }); let debug_utils = debug_utils::Instance::new(entry, &instance); let messenger = unsafe { debug_utils.create_debug_utils_messenger(&debug_info, None) }; let messenger = match messenger { diff --git a/src/gfx_apis/vulkan/pipeline.rs b/src/gfx_apis/vulkan/pipeline.rs index 564e4a55..3e446eb2 100644 --- a/src/gfx_apis/vulkan/pipeline.rs +++ b/src/gfx_apis/vulkan/pipeline.rs @@ -1,10 +1,7 @@ use { - crate::{ - gfx_apis::vulkan::{ - VulkanError, descriptor::VulkanDescriptorSetLayout, device::VulkanDevice, - shaders::VulkanShader, - }, - utils::on_drop::OnDrop, + crate::gfx_apis::vulkan::{ + VulkanError, descriptor::VulkanDescriptorSetLayout, device::VulkanDevice, + shaders::VulkanShader, }, arrayvec::ArrayVec, ash::{ @@ -21,6 +18,7 @@ use { SpecializationInfo, SpecializationMapEntry, }, }, + run_on_drop::on_drop, std::{rc::Rc, slice}, }; @@ -77,7 +75,7 @@ impl VulkanDevice { layout.map_err(VulkanError::CreatePipelineLayout)? }; let destroy_layout = - OnDrop(|| unsafe { self.device.destroy_pipeline_layout(pipeline_layout, None) }); + on_drop(|| unsafe { self.device.destroy_pipeline_layout(pipeline_layout, None) }); let mut frag_spec_data = ArrayVec::<_, { 5 * 4 }>::new(); let mut frag_spec_entries = ArrayVec::<_, 5>::new(); let mut frag_spec_entry = |data: &[u8]| { diff --git a/src/gfx_apis/vulkan/shm_image.rs b/src/gfx_apis/vulkan/shm_image.rs index ef7e472c..37ba16ff 100644 --- a/src/gfx_apis/vulkan/shm_image.rs +++ b/src/gfx_apis/vulkan/shm_image.rs @@ -14,7 +14,7 @@ use { transfer::{TransferType, VulkanShmImageAsyncData}, }, rect::Rect, - utils::{errorfmt::ErrorFmt, on_drop::OnDrop}, + utils::errorfmt::ErrorFmt, }, ash::vk::{ AccessFlags2, Buffer, BufferImageCopy2, BufferMemoryBarrier2, CommandBufferBeginInfo, @@ -26,6 +26,7 @@ use { }, gpu_alloc::UsageFlags, isnt::std_1::primitive::IsntSliceExt, + run_on_drop::on_drop, std::{cell::Cell, mem, ptr, rc::Rc, slice}, }; @@ -407,7 +408,7 @@ impl VulkanRenderer { .usage(usage); let image = unsafe { self.device.device.create_image(&create_info, None) }; let image = image.map_err(VulkanError::CreateImage)?; - let destroy_image = OnDrop(|| unsafe { self.device.device.destroy_image(image, None) }); + let destroy_image = on_drop(|| unsafe { self.device.device.destroy_image(image, None) }); let memory_requirements = unsafe { self.device.device.get_image_memory_requirements(image) }; let allocation = diff --git a/src/gfx_apis/vulkan/staging.rs b/src/gfx_apis/vulkan/staging.rs index 7d8c6e36..9b342ffc 100644 --- a/src/gfx_apis/vulkan/staging.rs +++ b/src/gfx_apis/vulkan/staging.rs @@ -8,16 +8,14 @@ use { device::VulkanDevice, renderer::VulkanRenderer, }, - utils::{ - clonecell::CloneCell, - on_drop::{OnDrop, OnDrop2}, - }, + utils::clonecell::CloneCell, }, ash::{ Device, vk::{Buffer, BufferCreateInfo, BufferUsageFlags}, }, gpu_alloc::UsageFlags, + run_on_drop::on_drop, std::{any::Any, cell::Cell, rc::Rc}, }; @@ -55,7 +53,7 @@ impl VulkanDevice { ) -> Result { let (vk_usage, usage) = get_usage(upload, download, transient); let buffer = self.create_buffer(size, vk_usage)?; - let destroy_buffer = OnDrop(|| unsafe { self.device.destroy_buffer(buffer, None) }); + let destroy_buffer = on_drop(|| unsafe { self.device.destroy_buffer(buffer, None) }); let memory_requirements = unsafe { self.device.get_buffer_memory_requirements(buffer) }; let allocation = allocator.alloc(&memory_requirements, usage, true)?; { @@ -85,8 +83,7 @@ impl VulkanDevice { let buffer = self.create_buffer(shell.size, vk_usage)?; let memory_requirements = unsafe { self.device.get_buffer_memory_requirements(buffer) }; let slf = self.clone(); - let destroy_buffer = - OnDrop2::new(move || unsafe { slf.device.destroy_buffer(buffer, None) }); + let destroy_buffer = on_drop(move || unsafe { slf.device.destroy_buffer(buffer, None) }); let slf = self.clone(); let finish_allocation = move |res| { let allocation: VulkanAllocation = res?; diff --git a/src/ifs/wl_seat.rs b/src/ifs/wl_seat.rs index 27ced5da..ec5d0431 100644 --- a/src/ifs/wl_seat.rs +++ b/src/ifs/wl_seat.rs @@ -94,7 +94,6 @@ use { event_listener::{EventListener, EventSource}, linkedlist::{LinkedList, LinkedNode, NodeRef}, numcell::NumCell, - on_drop::OnDrop, rc_eq::{rc_eq, rc_weak_eq}, smallmap::SmallMap, }, @@ -111,6 +110,7 @@ use { keyboard::syms::{KeySym, SYM_Escape}, }, kbvm::Keycode, + run_on_drop::on_drop, smallvec::SmallVec, std::{ cell::{Cell, RefCell}, @@ -901,7 +901,7 @@ impl WlSeatGlobal { return; }; self.focus_history_rotate.fetch_add(1); - let _reset = OnDrop(|| { + let _reset = on_drop(|| { self.focus_history_rotate.fetch_sub(1); }); if !visible { diff --git a/src/io_uring/ops/read_write_no_cancel.rs b/src/io_uring/ops/read_write_no_cancel.rs index bbce28b9..a152d752 100644 --- a/src/io_uring/ops/read_write_no_cancel.rs +++ b/src/io_uring/ops/read_write_no_cancel.rs @@ -9,8 +9,8 @@ use { sys::{IORING_OP_READ, IORING_OP_WRITE, io_uring_sqe}, }, time::Time, - utils::on_drop::OnDrop, }, + run_on_drop::on_drop, uapi::{Fd, c}, }; @@ -86,7 +86,7 @@ impl IoUring { self.schedule_timeout_link(time); } } - let panic = OnDrop(|| panic!("Operation cannot be cancelled from userspace")); + let panic = on_drop(|| panic!("Operation cannot be cancelled from userspace")); cancel(id.id); let res = Ok(pr.await.map(|v| v as usize)).merge(); panic.forget(); diff --git a/src/utils.rs b/src/utils.rs index b8144425..275faa64 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -33,7 +33,6 @@ pub mod nonblock; pub mod num_cpus; pub mod numcell; pub mod on_change; -pub mod on_drop; pub mod on_drop_event; pub mod once; pub mod opaque; diff --git a/src/utils/clone3.rs b/src/utils/clone3.rs index 7382271d..7b1fe539 100644 --- a/src/utils/clone3.rs +++ b/src/utils/clone3.rs @@ -2,8 +2,9 @@ use { crate::{ forker::ForkerError, pr_caps::drop_all_pr_caps, - utils::{errorfmt::ErrorFmt, on_drop::OnDrop, process_name::set_process_name}, + utils::{errorfmt::ErrorFmt, process_name::set_process_name}, }, + run_on_drop::on_drop, std::{env, mem::MaybeUninit, process, slice, str::FromStr}, uapi::{Msghdr, MsghdrMut, OwnedFd, c}, }; @@ -47,7 +48,7 @@ pub fn double_fork() -> Result, ForkerError> { match fork_with_pidfd(false)? { Forked::Parent { pid, .. } => { drop(c); - let _wait = OnDrop(|| { + let _wait = on_drop(|| { let _ = uapi::waitpid(pid, 0); }); recv_pidfd(&p).map(Some) diff --git a/src/utils/on_drop.rs b/src/utils/on_drop.rs deleted file mode 100644 index f7d7644d..00000000 --- a/src/utils/on_drop.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::{mem, mem::ManuallyDrop}; - -pub struct OnDrop(pub F) -where - F: FnMut() + Copy; - -impl OnDrop { - pub fn forget(self) { - mem::forget(self); - } -} - -impl Drop for OnDrop { - fn drop(&mut self) { - (self.0)(); - } -} - -pub struct OnDrop2 -where - F: FnOnce(), -{ - f: ManuallyDrop, -} - -impl OnDrop2 { - pub fn new(f: F) -> Self { - Self { - f: ManuallyDrop::new(f), - } - } - - pub fn forget(mut self) { - unsafe { - ManuallyDrop::drop(&mut self.f); - } - mem::forget(self); - } -} - -impl Drop for OnDrop2 { - fn drop(&mut self) { - let f = unsafe { ManuallyDrop::take(&mut self.f) }; - f(); - } -} diff --git a/src/xwayland.rs b/src/xwayland.rs index 84b4ea54..acd935b4 100644 --- a/src/xwayland.rs +++ b/src/xwayland.rs @@ -15,9 +15,7 @@ use { security_context_acceptor::AcceptorMetadata, state::State, user_session::import_environment, - utils::{ - buf::Buf, errorfmt::ErrorFmt, line_logger::log_lines, on_drop::OnDrop, oserror::OsError, - }, + utils::{buf::Buf, errorfmt::ErrorFmt, line_logger::log_lines, oserror::OsError}, wire::WlSurfaceId, xcon::XconError, xwayland::{ @@ -26,6 +24,7 @@ use { }, }, bstr::ByteSlice, + run_on_drop::on_drop, std::{num::ParseIntError, rc::Rc}, thiserror::Error, uapi::{OwnedFd, c, pipe2}, @@ -190,7 +189,7 @@ async fn run( state.ring.readable(&Rc::new(dfdread)).await?; state.xwayland.queue.clear(); state.xwayland.pidfd.set(Some(pidfd.clone())); - let _remove_pidfd = OnDrop(|| { + let _remove_pidfd = on_drop(|| { state.xwayland.pidfd.take(); }); { From b3c46bf52d459b8ff86aca5e0ca7ca104f443a61 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Fri, 13 Feb 2026 11:13:37 +0100 Subject: [PATCH 2/2] all: replace debug_fn with fmt::from_fn --- src/cli/idle.rs | 6 ++--- src/cli/randr.rs | 8 +++---- src/cmm/cmm_transform.rs | 5 ++-- src/config/handler.rs | 4 ++-- src/ei/ei_client.rs | 5 ++-- src/format.rs | 8 ++++--- src/ifs/wl_seat/text_input/simple_im.rs | 6 ++--- src/pipewire/pw_pod/pw_debug.rs | 31 ++++++++++++++----------- src/tasks/connector.rs | 7 +++--- src/utils.rs | 1 - src/utils/debug_fn.rs | 30 ------------------------ 11 files changed, 41 insertions(+), 70 deletions(-) delete mode 100644 src/utils/debug_fn.rs diff --git a/src/cli/idle.rs b/src/cli/idle.rs index 3fe6443c..9bc190ce 100644 --- a/src/cli/idle.rs +++ b/src/cli/idle.rs @@ -2,11 +2,11 @@ use { crate::{ cli::{GlobalArgs, IdleArgs, duration::parse_duration}, tools::tool_client::{Handle, ToolClient, with_tool_client}, - utils::{debug_fn::debug_fn, stack::Stack}, + utils::stack::Stack, wire::{JayIdleId, WlSurfaceId, jay_compositor, jay_idle}, }, clap::{Args, Subcommand}, - std::{cell::Cell, rc::Rc}, + std::{cell::Cell, fmt, rc::Rc}, }; #[derive(Subcommand, Debug, Default)] @@ -105,7 +105,7 @@ impl Idle { }); tc.round_trip().await; let interval = |iv: u64| { - debug_fn(move |f| { + fmt::from_fn(move |f| { let minutes = iv / 60; let seconds = iv % 60; if minutes == 0 && seconds == 0 { diff --git a/src/cli/randr.rs b/src/cli/randr.rs index 3981b04a..b07ab20e 100644 --- a/src/cli/randr.rs +++ b/src/cli/randr.rs @@ -7,9 +7,7 @@ use { ifs::wl_output::BlendSpace, scale::Scale, tools::tool_client::{Handle, ToolClient, with_tool_client}, - utils::{ - debug_fn::debug_fn, errorfmt::ErrorFmt, ordered_float::F64, transform_ext::TransformExt, - }, + utils::{errorfmt::ErrorFmt, ordered_float::F64, transform_ext::TransformExt}, wire::{JayRandrId, jay_compositor, jay_randr}, }, clap::{ @@ -21,7 +19,7 @@ use { linearize::LinearizeExt, std::{ cell::RefCell, - fmt::{Display, Formatter}, + fmt::{self, Display, Formatter}, rc::Rc, str::FromStr, time::Duration, @@ -1067,7 +1065,7 @@ impl Randr { if let Some(p) = &o.native_gamut { println!( " native gamut:{}", - debug_fn(|f| { + fmt::from_fn(|f| { if o.use_native_gamut { f.write_str(" (used for default color space)")?; } diff --git a/src/cmm/cmm_transform.rs b/src/cmm/cmm_transform.rs index 5b8a8537..2c2816a7 100644 --- a/src/cmm/cmm_transform.rs +++ b/src/cmm/cmm_transform.rs @@ -2,9 +2,10 @@ use { crate::{ cmm::{cmm_eotf::Eotf, cmm_primaries::Primaries}, theme::Color, - utils::{debug_fn::debug_fn, ordered_float::F64}, + utils::ordered_float::F64, }, std::{ + fmt, fmt::{Debug, Formatter}, hash::{Hash, Hasher}, marker::PhantomData, @@ -52,7 +53,7 @@ impl Debug for ColorMatrix { } fn format_matrix<'a>(m: &'a [[F64; 4]; 3]) -> impl Debug + use<'a> { - debug_fn(move |f| { + fmt::from_fn(move |f| { let iter = m .iter() .copied() diff --git a/src/config/handler.rs b/src/config/handler.rs index d32f608b..a7290d80 100644 --- a/src/config/handler.rs +++ b/src/config/handler.rs @@ -35,7 +35,6 @@ use { utils::{ asyncevent::AsyncEvent, copyhashmap::CopyHashMap, - debug_fn::debug_fn, errorfmt::ErrorFmt, numcell::NumCell, oserror::OsError, @@ -83,6 +82,7 @@ use { regex::Regex, std::{ cell::Cell, + fmt, hash::Hash, ops::Deref, rc::{Rc, Weak}, @@ -273,7 +273,7 @@ impl ConfigProxyHandler { LogLevel::Debug => Level::Debug, LogLevel::Trace => Level::Trace, }; - let debug = debug_fn(|fmt| { + let debug = fmt::from_fn(|fmt| { if let Some(file) = file { write!(fmt, "{}", file)?; if let Some(line) = line { diff --git a/src/ei/ei_client.rs b/src/ei/ei_client.rs index 185604d3..c5c1d85c 100644 --- a/src/ei/ei_client.rs +++ b/src/ei/ei_client.rs @@ -16,7 +16,6 @@ use { asyncevent::AsyncEvent, buffd::{EiMsgFormatter, EiMsgParser, EiMsgParserError, OutBufferSwapchain}, clonecell::CloneCell, - debug_fn::debug_fn, errorfmt::ErrorFmt, numcell::NumCell, pid_info::{PidInfo, get_pid_info, get_socket_creds}, @@ -27,7 +26,7 @@ use { std::{ cell::{Cell, RefCell}, error::Error, - fmt::Debug, + fmt::{self, Debug}, mem, ops::DerefMut, rc::Rc, @@ -128,7 +127,7 @@ impl EiClients { log::info!( "Client {} connected{:?}", data.id, - debug_fn(|fmt| { + fmt::from_fn(|fmt| { if let Some(p) = &data.pid_info { write!(fmt, ", pid: {}, uid: {}, comm: {:?}", p.pid, p.uid, p.comm)?; } diff --git a/src/format.rs b/src/format.rs index 300c25ef..9b752cf0 100644 --- a/src/format.rs +++ b/src/format.rs @@ -9,13 +9,15 @@ use { SPA_VIDEO_FORMAT_UNKNOWN, SPA_VIDEO_FORMAT_xBGR_210LE, SPA_VIDEO_FORMAT_xRGB_210LE, SpaVideoFormat, }, - utils::debug_fn::debug_fn, }, ahash::AHashMap, ash::vk, jay_config::video::Format as ConfigFormat, once_cell::sync::Lazy, - std::fmt::{Debug, Write}, + std::{ + fmt, + fmt::{Debug, Write}, + }, }; #[derive(Copy, Clone, Debug)] @@ -134,7 +136,7 @@ const fn fourcc_code(a: char, b: char, c: char, d: char) -> u32 { #[expect(dead_code)] pub fn debug(fourcc: u32) -> impl Debug { - debug_fn(move |fmt| { + fmt::from_fn(move |fmt| { fmt.write_char(fourcc as u8 as char)?; fmt.write_char((fourcc >> 8) as u8 as char)?; fmt.write_char((fourcc >> 16) as u8 as char)?; diff --git a/src/ifs/wl_seat/text_input/simple_im.rs b/src/ifs/wl_seat/text_input/simple_im.rs index 6a3a905b..917e3312 100644 --- a/src/ifs/wl_seat/text_input/simple_im.rs +++ b/src/ifs/wl_seat/text_input/simple_im.rs @@ -11,7 +11,7 @@ use { wl_surface::zwp_input_popup_surface_v2::ZwpInputPopupSurfaceV2, }, keyboard::KeyboardState, - utils::{clonecell::CloneCell, debug_fn::debug_fn, smallmap::SmallMap}, + utils::{clonecell::CloneCell, smallmap::SmallMap}, wire::ZwpInputPopupSurfaceV2Id, }, kbvm::{ @@ -24,7 +24,7 @@ use { }, std::{ cell::{Cell, RefCell}, - fmt::Write, + fmt::{self, Write}, rc::Rc, }, }; @@ -154,7 +154,7 @@ impl UnicodeInput { let _ = write!(self.text, "U+{:x}", self.cp); self.cursor = self.text.len() as _; if let Some(char) = char::from_u32(self.cp) { - let s = debug_fn(|f| { + let s = fmt::from_fn(|f| { if char == '\n' { f.write_str("\\n") } else { diff --git a/src/pipewire/pw_pod/pw_debug.rs b/src/pipewire/pw_pod/pw_debug.rs index ddda7fb4..feb98420 100644 --- a/src/pipewire/pw_pod/pw_debug.rs +++ b/src/pipewire/pw_pod/pw_debug.rs @@ -31,9 +31,12 @@ use { SpaVideoTransferFunction, }, }, - utils::{debug_fn::debug_fn, errorfmt::ErrorFmt}, + utils::errorfmt::ErrorFmt, + }, + std::{ + fmt, + fmt::{Debug, DebugList, Formatter, Write}, }, - std::fmt::{Debug, DebugList, Formatter, Write}, }; trait PwPodObjectDebugger: Sync { @@ -62,7 +65,7 @@ where s.field("flags", &value.flags) .field( "pod", - &debug_fn(|f| (self.debug_pod)(value.key, f, value.pod)), + &fmt::from_fn(|f| (self.debug_pod)(value.key, f, value.pod)), ) .finish() } @@ -83,16 +86,16 @@ where .field("flags", &c.flags) .field( "elements", - &debug_fn(|fmt| { + &fmt::from_fn(|fmt| { array_body_debug(fmt, c.elements, |l, p| { match p.read_pod_body_packed(ty, c.elements.child_len) { Ok(p) => { - l.entry(&debug_fn(|fmt| f(fmt, p))); + l.entry(&fmt::from_fn(|fmt| f(fmt, p))); true } Err(e) => { let e = ErrorFmt(e); - l.entry(&debug_fn(|fmt| { + l.entry(&fmt::from_fn(|fmt| { write!(fmt, "Could not read choice element: {}", e) })); false @@ -151,7 +154,7 @@ where } Err(e) => { let e = ErrorFmt(e); - l.entry(&debug_fn(|f| write!(f, "Could not read id: {}", e))); + l.entry(&fmt::from_fn(|f| write!(f, "Could not read id: {}", e))); false } }) @@ -334,18 +337,18 @@ impl<'a> Debug for PwPodObject<'a> { s.field("id", id); s.field( "props", - &debug_fn(|f| { + &fmt::from_fn(|f| { let mut l = f.debug_list(); let mut parser = self.probs; while parser.len() > 0 { match parser.read_prop() { Ok(p) => match debugger { - Some(d) => l.entry(&debug_fn(|fmt| d.debug_property(fmt, p))), + Some(d) => l.entry(&fmt::from_fn(|fmt| d.debug_property(fmt, p))), _ => l.entry(&p), }, Err(e) => { let e = ErrorFmt(e); - l.entry(&debug_fn(|f| { + l.entry(&fmt::from_fn(|f| { write!(f, "Could not read object property: {}", &e) })); break; @@ -365,7 +368,7 @@ impl<'a> Debug for PwPodSequence<'a> { s.field("unit", &self.unit); s.field( "controls", - &debug_fn(|f| { + &fmt::from_fn(|f| { let mut l = f.debug_list(); let mut parser = self.controls; while parser.len() > 0 { @@ -373,7 +376,7 @@ impl<'a> Debug for PwPodSequence<'a> { Ok(c) => l.entry(&c), Err(e) => { let e = ErrorFmt(e); - l.entry(&debug_fn(|f| { + l.entry(&fmt::from_fn(|f| { write!(f, "Could not read control element: {}", &e) })); break; @@ -404,7 +407,7 @@ impl<'a> Debug for PwPodStruct<'a> { let e = ErrorFmt(e); s.field( &field, - &debug_fn(|f| write!(f, "Could not parse struct field: {}", &e)), + &fmt::from_fn(|f| write!(f, "Could not parse struct field: {}", &e)), ); break; } @@ -423,7 +426,7 @@ impl<'a> Debug for PwPodArray<'a> { Ok(e) => list.entry(&e), Err(e) => { let e = ErrorFmt(e); - list.entry(&debug_fn(|f| { + list.entry(&fmt::from_fn(|f| { write!(f, "Could not parse array element: {}", &e) })); break; diff --git a/src/tasks/connector.rs b/src/tasks/connector.rs index 85753771..9ecf51be 100644 --- a/src/tasks/connector.rs +++ b/src/tasks/connector.rs @@ -15,12 +15,11 @@ use { state::{ConnectorData, OutputData, State}, tree::{OutputNode, WsMoveConfig, move_ws_to_output}, utils::{ - asyncevent::AsyncEvent, clonecell::CloneCell, debug_fn::debug_fn, - hash_map_ext::HashMapExt, rc_eq::RcEq, + asyncevent::AsyncEvent, clonecell::CloneCell, hash_map_ext::HashMapExt, rc_eq::RcEq, }, }, jay_config::video::Transform, - std::{cell::Cell, collections::VecDeque, rc::Rc}, + std::{cell::Cell, collections::VecDeque, fmt, rc::Rc}, }; pub fn handle(state: &Rc, connector: &Rc) { @@ -475,7 +474,7 @@ impl ConnectorHandler { } fn create_description(info: &MonitorInfo) -> String { - debug_fn(|f| { + fmt::from_fn(|f| { let mut needs_space = false; let id = &info.output_id; for s in [&id.manufacturer, &id.model, &id.serial_number] { diff --git a/src/utils.rs b/src/utils.rs index 275faa64..ea3ba668 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,7 +14,6 @@ pub mod clone3; pub mod clonecell; pub mod compat; pub mod copyhashmap; -pub mod debug_fn; pub mod double_buffered; pub mod double_click_state; pub mod errorfmt; diff --git a/src/utils/debug_fn.rs b/src/utils/debug_fn.rs deleted file mode 100644 index 7f09a9f3..00000000 --- a/src/utils/debug_fn.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::fmt::{Debug, Display, Formatter}; - -pub fn debug_fn(f: F) -> Printable -where - F: Fn(&mut Formatter<'_>) -> std::fmt::Result, -{ - Printable { f } -} - -pub struct Printable { - f: F, -} - -impl Debug for Printable -where - F: Fn(&mut Formatter<'_>) -> std::fmt::Result, -{ - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - (self.f)(f) - } -} - -impl Display for Printable -where - F: Fn(&mut Formatter<'_>) -> std::fmt::Result, -{ - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - (self.f)(f) - } -}