-
Notifications
You must be signed in to change notification settings - Fork 66
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
device: Use OwnedFd
type and bump MSRV to 1.66
#75
Open
MarijnS95
wants to merge
2
commits into
raymanfx:next
Choose a base branch
from
MarijnS95:ownedfd
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−44
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
|
||
|
@@ -72,7 +72,7 @@ impl Device { | |
unsafe { | ||
let mut v4l2_caps: v4l2_capability = mem::zeroed(); | ||
v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_QUERYCAP, | ||
&mut v4l2_caps as *mut _ as *mut std::os::raw::c_void, | ||
)?; | ||
|
@@ -91,7 +91,7 @@ impl Device { | |
v4l2_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; | ||
v4l2_ctrl.id |= V4L2_CTRL_FLAG_NEXT_COMPOUND; | ||
match v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_QUERY_EXT_CTRL, | ||
&mut v4l2_ctrl as *mut _ as *mut std::os::raw::c_void, | ||
) { | ||
|
@@ -114,7 +114,7 @@ impl Device { | |
..mem::zeroed() | ||
}; | ||
let res = v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_QUERYMENU, | ||
&mut v4l2_menu as *mut _ as *mut std::os::raw::c_void, | ||
); | ||
|
@@ -169,7 +169,7 @@ impl Device { | |
..mem::zeroed() | ||
}; | ||
v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_QUERY_EXT_CTRL, | ||
&mut queryctrl as *mut _ as *mut std::os::raw::c_void, | ||
)?; | ||
|
@@ -188,7 +188,7 @@ impl Device { | |
..mem::zeroed() | ||
}; | ||
v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_G_EXT_CTRLS, | ||
&mut v4l2_ctrls as *mut _ as *mut std::os::raw::c_void, | ||
)?; | ||
|
@@ -276,19 +276,19 @@ impl Device { | |
} | ||
control::Value::CompoundU8(ref val) => { | ||
control.__bindgen_anon_1.p_u8 = val.as_ptr() as *mut u8; | ||
control.size = (val.len() * std::mem::size_of::<u8>()) as u32; | ||
control.size = std::mem::size_of_val(val.as_slice()) as u32; | ||
} | ||
control::Value::CompoundU16(ref val) => { | ||
control.__bindgen_anon_1.p_u16 = val.as_ptr() as *mut u16; | ||
control.size = (val.len() * std::mem::size_of::<u16>()) as u32; | ||
control.size = std::mem::size_of_val(val.as_slice()) as u32; | ||
} | ||
control::Value::CompoundU32(ref val) => { | ||
control.__bindgen_anon_1.p_u32 = val.as_ptr() as *mut u32; | ||
control.size = (val.len() * std::mem::size_of::<u32>()) as u32; | ||
control.size = std::mem::size_of_val(val.as_slice()) as u32; | ||
} | ||
control::Value::CompoundPtr(ref val) => { | ||
control.__bindgen_anon_1.ptr = val.as_ptr() as *mut std::os::raw::c_void; | ||
control.size = (val.len() * std::mem::size_of::<u8>()) as u32; | ||
control.size = std::mem::size_of_val(val.as_slice()) as u32; | ||
} | ||
}; | ||
|
||
|
@@ -311,7 +311,7 @@ impl Device { | |
}; | ||
|
||
v4l2::ioctl( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
v4l2::vidioc::VIDIOC_S_EXT_CTRLS, | ||
&mut controls as *mut _ as *mut std::os::raw::c_void, | ||
) | ||
|
@@ -323,7 +323,7 @@ impl io::Read for Device { | |
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
unsafe { | ||
let ret = libc::read( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
buf.as_mut_ptr() as *mut std::os::raw::c_void, | ||
buf.len(), | ||
); | ||
|
@@ -339,7 +339,7 @@ impl io::Write for Device { | |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
unsafe { | ||
let ret = libc::write( | ||
self.handle().as_raw_fd(), | ||
self.as_raw_fd(), | ||
buf.as_ptr() as *const std::os::raw::c_void, | ||
buf.len(), | ||
); | ||
|
@@ -358,17 +358,29 @@ impl io::Write for Device { | |
} | ||
} | ||
|
||
impl AsFd for Device { | ||
fn as_fd(&self) -> BorrowedFd<'_> { | ||
self.handle.as_fd() | ||
} | ||
} | ||
|
||
impl AsRawFd for Device { | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.handle.as_raw_fd() | ||
} | ||
} | ||
|
||
/// 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 fn new(fd: OwnedFd) -> Self { | ||
Self(fd) | ||
Comment on lines
380
to
+383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These docs, while still true, are a bit less relevant now (see also the removal of |
||
} | ||
|
||
|
@@ -385,7 +397,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 | ||
|
@@ -400,35 +412,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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type always wrongly derived
Clone
, causingdrop()
close()
the same fd multiple times...Instead I'd argue this trait should be available on
Device
which holds on to anArc<Handle>
exactly to allow sharing of thefd
without cloning it.Additionally
Handle
could providetry_clone()
to duplicate the file descriptor?Also,
Device
currently has anfn handle()
to clone thisArc<Handle>
but thus far seems to mostly be used to distribute the Fd around. Hence:As(Raw)Fd
forDevice
so that it's easier to borrow its (raw) file descriptor?io::Queue
store aDevice
directly so that it's more obvious that it's not just a wrapper aroundOwnedFd
but really a wrapper around aDevice
?