diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2021-11-15 20:55:30 +0100 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2021-11-15 20:14:17 +0100 |
commit | c43c6aa562c5222477f66d50179e368ca0f25a9b (patch) | |
tree | 7630730b7214fbe933413705c9820245cb4ddb2a /src/lib.rs | |
parent | 1a8e9bba39ed97634ae61ad146653071da666143 (diff) | |
download | vtcol-c43c6aa562c5222477f66d50179e368ca0f25a9b.tar.gz |
turn Fd into proper wrapper
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 30 |
1 files changed, 25 insertions, 5 deletions
@@ -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<libc::c_int> 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<F: AsRawFd>(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<Palette> +pub fn ioctl_gio_cmap<F: AsRawFd>(fd: &F) -> io::Result<Palette> { 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), ) |