Skip to content

Commit

Permalink
device: Use OwnedFd type and bump MSRV to 1.66
Browse files Browse the repository at this point in the history
The first new commit 22a2365 on `next` switches to the `RawFd` type from
the `std::os::fd` module which is only available since Rust 1.66.  This
module also provides an `OwnedFd` type which already has a `close()`
call in the `Drop` implementation, and implements the desired `AsRawFd`
(and `AsFd` and `IntoRawFd`) traits.

Note that these types were already stabilized in Rust 1.63 under
`std::os::unix::io`, which is now merely a reexport of `std::os::fd`.

This also replaces the forgotten `.fd()` calls with `.as_raw_fd()` from
from commit 17c2871.
  • Loading branch information
MarijnS95 committed Sep 6, 2023
1 parent 384e7fb commit 23b9792
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
license = "MIT"
readme = "README.md"
repository= "https://github.com/raymanfx/libv4l-rs"
rust-version = "1.66"

[dependencies]
bitflags = "1.2.1"
Expand Down
45 changes: 24 additions & 21 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::TryFrom;
use std::path::Path;
use std::{
convert::TryFrom,
io, mem,
os::fd::{AsRawFd, RawFd},
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
path::Path,
sync::Arc,
};

Expand Down Expand Up @@ -361,14 +361,14 @@ impl io::Write for Device {
/// Device handle for low-level access.
///
/// Acquiring a handle facilitates (possibly mutating) interactions with the device.
#[derive(Debug, Clone)]
pub struct Handle(RawFd);
#[derive(Debug)]
pub struct Handle(OwnedFd);

impl Handle {
/// Wraps an existing file descriptor
///
/// The caller must ensure that `fd` is a valid, open file descriptor for a V4L device.
pub unsafe fn new(fd: RawFd) -> Self {
pub unsafe fn new(fd: OwnedFd) -> Self {
Self(fd)
}

Expand All @@ -385,7 +385,7 @@ impl Handle {
return Err(io::Error::last_os_error());
}

Ok(Handle(fd))
Ok(Handle(unsafe { OwnedFd::from_raw_fd(fd) }))
}

/// Polls the file descriptor for I/O events
Expand All @@ -400,35 +400,38 @@ impl Handle {
pub fn poll(&self, events: i16, timeout: i32) -> io::Result<i32> {
match unsafe {
libc::poll(
[libc::pollfd {
fd: self.0,
&mut libc::pollfd {
fd: self.as_raw_fd(),
events,
revents: 0,
}]
.as_mut_ptr(),
},
1,
timeout,
)
} {
-1 => Err(io::Error::last_os_error()),
ret => {
// A return value of zero means that we timed out. A positive value signifies the
// number of fds with non-zero revents fields (aka I/O activity).
assert!(ret == 0 || ret == 1);
Ok(ret)
}
// A return value of zero means that we timed out. A positive value signifies the
// number of fds with non-zero revents fields (aka I/O activity).
ret @ 0..=1 => Ok(ret),
ret => panic!("Invalid return value {}", ret),
}
}
}

impl Drop for Handle {
fn drop(&mut self) {
let _ = v4l2::close(self.0);
impl AsFd for Handle {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

impl AsRawFd for Handle {
fn as_raw_fd(&self) -> RawFd {
self.0
self.0.as_raw_fd()
}
}

impl IntoRawFd for Handle {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
}
}
19 changes: 10 additions & 9 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
marker::PhantomData,
mem,
ops::{Index, IndexMut},
os::fd::AsRawFd,
ptr, slice,
sync::Arc,
};
Expand Down Expand Up @@ -65,7 +66,7 @@ impl<B, S> Queue<B, S> {

unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_REQBUFS,
&mut v4l2_reqbufs as *mut _ as *mut std::os::raw::c_void,
)?;
Expand All @@ -88,7 +89,7 @@ impl<B, S> Queue<B, S> {

unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_QUERYBUF,
&mut buf as *mut _ as *mut std::os::raw::c_void,
)?;
Expand All @@ -101,7 +102,7 @@ impl<B, S> Queue<B, S> {
fn streamon(&mut self) -> io::Result<()> {
unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_STREAMON,
&mut self.buf_type as *mut _ as *mut std::os::raw::c_void,
)
Expand All @@ -112,7 +113,7 @@ impl<B, S> Queue<B, S> {
fn streamoff(&mut self) -> io::Result<()> {
unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_STREAMOFF,
&mut self.buf_type as *mut _ as *mut std::os::raw::c_void,
)
Expand All @@ -125,7 +126,7 @@ impl<B, S> Queue<B, S> {

unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_QBUF,
buf as *mut _ as *mut std::os::raw::c_void,
)
Expand All @@ -138,7 +139,7 @@ impl<B, S> Queue<B, S> {

unsafe {
v4l2::ioctl(
self.handle.fd(),
self.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_DQBUF,
buf as *mut _ as *mut std::os::raw::c_void,
)
Expand Down Expand Up @@ -244,7 +245,7 @@ impl Queue<Mmap<'_>, queue::Idle> {

let mapping = unsafe {
v4l2::ioctl(
queue.handle.fd(),
queue.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_QUERYBUF,
&mut v4l2_buf as *mut _ as *mut std::os::raw::c_void,
)?;
Expand All @@ -254,7 +255,7 @@ impl Queue<Mmap<'_>, queue::Idle> {
v4l2_buf.length as usize,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_SHARED,
queue.handle.fd(),
queue.handle.as_raw_fd(),
v4l2_buf.m.offset as libc::off_t,
)?;

Expand Down Expand Up @@ -322,7 +323,7 @@ impl Queue<UserPtr, queue::Idle> {
};
unsafe {
v4l2::ioctl(
queue.handle.fd(),
queue.handle.as_raw_fd(),
v4l2::vidioc::VIDIOC_G_FMT,
&mut v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
)?;
Expand Down

0 comments on commit 23b9792

Please sign in to comment.