summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs30
1 files changed, 25 insertions, 5 deletions
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<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),
)