From 847ec6d5cdccf74205b78baf60ab498c7499528c Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 18 Dec 2024 12:03:01 -0800 Subject: [PATCH] Implement `From` for `OwnedFd` --- src/shm/raw.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/shm/raw.rs b/src/shm/raw.rs index 138842071..9629f8547 100644 --- a/src/shm/raw.rs +++ b/src/shm/raw.rs @@ -10,6 +10,7 @@ use rustix::{ use std::{ fs::File, io, + ops::Deref, os::unix::prelude::{AsFd, BorrowedFd, OwnedFd}, sync::Arc, time::{SystemTime, UNIX_EPOCH}, @@ -33,7 +34,7 @@ use super::CreatePoolError; /// This pool does not release buffers. If you need this, use one of the higher level pools. #[derive(Debug)] pub struct RawPool { - pool: wl_shm_pool::WlShmPool, + pool: DestroyOnDropPool, len: usize, mem_file: File, mmap: MmapMut, @@ -57,7 +58,7 @@ impl RawPool { .unwrap_or_else(|_| Proxy::inert(shm.backend().clone())); let mmap = unsafe { MmapMut::map_mut(&mem_file)? }; - Ok(RawPool { pool, len, mem_file, mmap }) + Ok(RawPool { pool: DestroyOnDropPool(pool), len, mem_file, mmap }) } /// Resizes the memory pool, notifying the server the pool has changed in size. @@ -157,6 +158,12 @@ impl AsFd for RawPool { } } +impl From for OwnedFd { + fn from(pool: RawPool) -> Self { + pool.mem_file.into() + } +} + impl io::Write for RawPool { fn write(&mut self, buf: &[u8]) -> io::Result { io::Write::write(&mut self.mem_file, buf) @@ -251,9 +258,20 @@ impl RawPool { } } -impl Drop for RawPool { +#[derive(Debug)] +struct DestroyOnDropPool(wl_shm_pool::WlShmPool); + +impl Deref for DestroyOnDropPool { + type Target = wl_shm_pool::WlShmPool; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Drop for DestroyOnDropPool { fn drop(&mut self) { - self.pool.destroy(); + self.0.destroy(); } }