From c43c6aa562c5222477f66d50179e368ca0f25a9b Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 15 Nov 2021 20:55:30 +0100 Subject: turn Fd into proper wrapper --- src/lib.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 47f650d..f8bef99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,9 +2,29 @@ use libc::ioctl; use std::{convert::TryFrom, fmt, io::{self, BufWriter, Error, Write}, + os::unix::io::{AsRawFd, RawFd}, path::{Path, PathBuf}}; -pub type Fd = libc::c_int; +#[derive(Debug)] +pub struct Fd(libc::c_int); + +impl From for Fd +{ + fn from(fd: libc::c_int) -> Self { Self(fd) } +} + +impl AsRawFd for Fd +{ + fn as_raw_fd(&self) -> RawFd { self.0 } +} + +impl fmt::Display for Fd +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result + { + write!(f, "Fd({})", self.0) + } +} const PALETTE_SIZE: usize = 16; const PALETTE_BYTES: usize = PALETTE_SIZE * 3; /* 16 * sizeof(int) */ @@ -493,11 +513,11 @@ impl From<&RawPalette<'_>> for Palette } } -pub fn ioctl_pio_cmap(fd: Fd, pal: &Palette) -> io::Result<()> +pub fn ioctl_pio_cmap(fd: &F, pal: &Palette) -> io::Result<()> { if unsafe { ioctl( - fd, + fd.as_raw_fd(), PIO_CMAP, std::mem::transmute::<&Palette, *const libc::c_void>(&pal), ) @@ -509,13 +529,13 @@ pub fn ioctl_pio_cmap(fd: Fd, pal: &Palette) -> io::Result<()> } } -pub fn ioctl_gio_cmap(fd: Fd) -> io::Result +pub fn ioctl_gio_cmap(fd: &F) -> io::Result { let mut pal = Palette::new(); if unsafe { ioctl( - fd, + fd.as_raw_fd(), GIO_CMAP, std::mem::transmute::<&mut Palette, *mut libc::c_void>(&mut pal), ) -- cgit v1.2.3