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

Implement From<RawPool> for OwnedFd #480

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/shm/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -157,6 +158,12 @@ impl AsFd for RawPool {
}
}

impl From<RawPool> for OwnedFd {
fn from(pool: RawPool) -> Self {
pool.mem_file.into()
}
}

impl io::Write for RawPool {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::Write::write(&mut self.mem_file, buf)
Expand Down Expand Up @@ -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();
}
}

Expand Down
Loading