Skip to content

Commit

Permalink
ioctl: remove traits equivalent to From<v4l2_type>
Browse files Browse the repository at this point in the history
Some of our ioctl traits are unnecessary since they are equivalent to
From<v4l2_type>. Replace them by the latter as some of the more recently
implemented ioctls already do.
  • Loading branch information
Gnurou committed Oct 12, 2023
1 parent 110fd77 commit da3c5d0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 83 deletions.
24 changes: 7 additions & 17 deletions lib/src/ioctl/enum_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Safe wrapper for the `VIDIOC_ENUM_FMT` ioctl.
use super::string_from_cstr;
use crate::bindings;
use crate::bindings::v4l2_fmtdesc;
use crate::{PixelFormat, QueueType};
use bitflags::bitflags;
use log::error;
Expand All @@ -10,17 +11,6 @@ use std::mem;
use std::os::unix::io::AsRawFd;
use thiserror::Error;

/// Implementors can receive the result from the `enum_fmt` ioctl.
pub trait EnumFmt {
fn from(fmtdesc: bindings::v4l2_fmtdesc) -> Self;
}

impl EnumFmt for bindings::v4l2_fmtdesc {
fn from(fmtdesc: bindings::v4l2_fmtdesc) -> Self {
fmtdesc
}
}

bitflags! {
/// Flags returned by the `VIDIOC_ENUM_FMT` ioctl into the `flags` field of
/// `struct v4l2_fmtdesc`.
Expand All @@ -31,8 +21,8 @@ bitflags! {
}
}
/// Quickly get the Fourcc code of a format.
impl EnumFmt for PixelFormat {
fn from(fmtdesc: bindings::v4l2_fmtdesc) -> Self {
impl From<v4l2_fmtdesc> for PixelFormat {
fn from(fmtdesc: v4l2_fmtdesc) -> Self {
fmtdesc.pixelformat.into()
}
}
Expand Down Expand Up @@ -61,8 +51,8 @@ impl fmt::Display for FmtDesc {
}
}

impl EnumFmt for FmtDesc {
fn from(fmtdesc: bindings::v4l2_fmtdesc) -> Self {
impl From<v4l2_fmtdesc> for FmtDesc {
fn from(fmtdesc: v4l2_fmtdesc) -> Self {
FmtDesc {
flags: FormatFlags::from_bits_truncate(fmtdesc.flags),
description: string_from_cstr(&fmtdesc.description).unwrap_or_else(|_| "".into()),
Expand Down Expand Up @@ -92,12 +82,12 @@ impl From<EnumFmtError> for Errno {
}

/// Safe wrapper around the `VIDIOC_ENUM_FMT` ioctl.
pub fn enum_fmt<T: EnumFmt>(
pub fn enum_fmt<T: From<v4l2_fmtdesc>>(
fd: &impl AsRawFd,
queue: QueueType,
index: u32,
) -> Result<T, EnumFmtError> {
let mut fmtdesc = bindings::v4l2_fmtdesc {
let mut fmtdesc = v4l2_fmtdesc {
type_: queue as u32,
index,
..unsafe { mem::zeroed() }
Expand Down
20 changes: 6 additions & 14 deletions lib/src/ioctl/frameintervals.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use std::os::unix::io::AsRawFd;
use thiserror::Error;

use crate::{bindings, PixelFormat};

pub trait FrameIntervals {
fn from(input: bindings::v4l2_frmivalenum) -> Self;
}

impl FrameIntervals for bindings::v4l2_frmivalenum {
fn from(input: bindings::v4l2_frmivalenum) -> Self {
input
}
}
use crate::bindings;
use crate::bindings::v4l2_frmivalenum;
use crate::PixelFormat;

/// A wrapper for the 'v4l2_frmivalenum' union member types
#[derive(Debug)]
Expand All @@ -20,7 +12,7 @@ pub enum FrmIvalTypes<'a> {
StepWise(&'a bindings::v4l2_frmival_stepwise),
}

impl bindings::v4l2_frmivalenum {
impl v4l2_frmivalenum {
/// Safely access the intervals member of the struct based on the
/// returned index.
pub fn intervals(&self) -> Option<FrmIvalTypes> {
Expand Down Expand Up @@ -59,14 +51,14 @@ pub enum FrameIntervalsError {
IoctlError(nix::Error),
}

pub fn enum_frame_intervals<T: FrameIntervals>(
pub fn enum_frame_intervals<T: From<v4l2_frmivalenum>>(
fd: &impl AsRawFd,
index: u32,
pixel_format: PixelFormat,
width: u32,
height: u32,
) -> Result<T, FrameIntervalsError> {
let mut frame_interval = bindings::v4l2_frmivalenum {
let mut frame_interval = v4l2_frmivalenum {
index,
pixel_format: pixel_format.into(),
width,
Expand Down
20 changes: 6 additions & 14 deletions lib/src/ioctl/framesizes.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
use std::os::unix::io::AsRawFd;
use thiserror::Error;

use crate::{bindings, PixelFormat};

pub trait FrameSize {
fn from(input: bindings::v4l2_frmsizeenum) -> Self;
}

impl FrameSize for bindings::v4l2_frmsizeenum {
fn from(input: bindings::v4l2_frmsizeenum) -> Self {
input
}
}
use crate::bindings;
use crate::bindings::v4l2_frmsizeenum;
use crate::PixelFormat;

/// A wrapper for the 'v4l2_frmsizeenum' union member types
#[derive(Debug)]
Expand All @@ -20,7 +12,7 @@ pub enum FrmSizeTypes<'a> {
StepWise(&'a bindings::v4l2_frmsize_stepwise),
}

impl bindings::v4l2_frmsizeenum {
impl v4l2_frmsizeenum {
/// Safely access the size member of the struct based on the
/// returned index.
pub fn size(&self) -> Option<FrmSizeTypes> {
Expand Down Expand Up @@ -59,12 +51,12 @@ pub enum FrameSizeError {
IoctlError(nix::Error),
}

pub fn enum_frame_sizes<T: FrameSize>(
pub fn enum_frame_sizes<T: From<v4l2_frmsizeenum>>(
fd: &impl AsRawFd,
index: u32,
pixel_format: PixelFormat,
) -> Result<T, FrameSizeError> {
let mut frame_size = bindings::v4l2_frmsizeenum {
let mut frame_size = v4l2_frmsizeenum {
index,
pixel_format: pixel_format.into(),
..unsafe { std::mem::zeroed() }
Expand Down
24 changes: 7 additions & 17 deletions lib/src/ioctl/querycap.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
//! Safe wrapper for the `VIDIOC_QUERYCAP` ioctl.
use super::string_from_cstr;
use crate::bindings;
use crate::bindings::v4l2_capability;
use bitflags::bitflags;
use nix::errno::Errno;
use std::fmt;
use std::mem;
use std::os::unix::io::AsRawFd;
use thiserror::Error;

/// Implementors can receive the result from the `querycap` ioctl.
pub trait QueryCap {
fn from(capability: bindings::v4l2_capability) -> Self;
}

impl QueryCap for bindings::v4l2_capability {
fn from(capability: bindings::v4l2_capability) -> Self {
capability
}
}

bitflags! {
/// Flags returned by the `VIDIOC_QUERYCAP` ioctl into the `capabilities`
/// or `device_capabilities` field of `v4l2_capability`.
Expand Down Expand Up @@ -69,8 +59,8 @@ impl fmt::Display for Capabilities {
}

/// Used to get the capability flags from a `VIDIOC_QUERYCAP` ioctl.
impl QueryCap for Capabilities {
fn from(qcap: bindings::v4l2_capability) -> Self {
impl From<v4l2_capability> for Capabilities {
fn from(qcap: v4l2_capability) -> Self {
Capabilities::from_bits_truncate(qcap.capabilities)
}
}
Expand Down Expand Up @@ -99,8 +89,8 @@ impl Capability {
}
}

impl QueryCap for Capability {
fn from(qcap: bindings::v4l2_capability) -> Self {
impl From<v4l2_capability> for Capability {
fn from(qcap: v4l2_capability) -> Self {
Capability {
driver: string_from_cstr(&qcap.driver).unwrap_or_else(|_| "".into()),
card: string_from_cstr(&qcap.card).unwrap_or_else(|_| "".into()),
Expand Down Expand Up @@ -137,8 +127,8 @@ impl From<QueryCapError> for Errno {
}

/// Safe wrapper around the `VIDIOC_QUERYCAP` ioctl.
pub fn querycap<T: QueryCap>(fd: &impl AsRawFd) -> Result<T, QueryCapError> {
let mut qcap: bindings::v4l2_capability = unsafe { mem::zeroed() };
pub fn querycap<T: From<v4l2_capability>>(fd: &impl AsRawFd) -> Result<T, QueryCapError> {
let mut qcap: v4l2_capability = unsafe { mem::zeroed() };

match unsafe { ioctl::vidioc_querycap(fd.as_raw_fd(), &mut qcap) } {
Ok(_) => Ok(T::from(qcap)),
Expand Down
32 changes: 11 additions & 21 deletions lib/src/ioctl/reqbufs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Safe wrapper for the `VIDIOC_REQBUFS` ioctl.
use crate::bindings;
use crate::bindings::v4l2_requestbuffers;
use crate::memory::MemoryType;
use crate::QueueType;
use bitflags::bitflags;
Expand All @@ -8,17 +9,6 @@ use std::mem;
use std::os::unix::io::AsRawFd;
use thiserror::Error;

/// Implementors can receive the result from the `reqbufs` ioctl.
pub trait ReqBufs {
fn from(reqbufs: bindings::v4l2_requestbuffers) -> Self;
}

impl ReqBufs for bindings::v4l2_requestbuffers {
fn from(reqbufs: bindings::v4l2_requestbuffers) -> Self {
reqbufs
}
}

bitflags! {
/// Flags returned by the `VIDIOC_REQBUFS` ioctl into the `capabilities`
/// field of `struct v4l2_requestbuffers`.
Expand All @@ -33,21 +23,21 @@ bitflags! {
}
}

impl ReqBufs for () {
fn from(_reqbufs: bindings::v4l2_requestbuffers) -> Self {}
impl From<v4l2_requestbuffers> for () {
fn from(_reqbufs: v4l2_requestbuffers) -> Self {}
}

/// In case we are just interested in the number of buffers that `reqbufs`
/// created.
impl ReqBufs for usize {
fn from(reqbufs: bindings::v4l2_requestbuffers) -> Self {
impl From<v4l2_requestbuffers> for usize {
fn from(reqbufs: v4l2_requestbuffers) -> Self {
reqbufs.count as usize
}
}

/// If we just want to query the buffer capabilities.
impl ReqBufs for BufferCapabilities {
fn from(reqbufs: bindings::v4l2_requestbuffers) -> Self {
impl From<v4l2_requestbuffers> for BufferCapabilities {
fn from(reqbufs: v4l2_requestbuffers) -> Self {
BufferCapabilities::from_bits_truncate(reqbufs.capabilities)
}
}
Expand All @@ -58,8 +48,8 @@ pub struct RequestBuffers {
pub capabilities: BufferCapabilities,
}

impl ReqBufs for RequestBuffers {
fn from(reqbufs: bindings::v4l2_requestbuffers) -> Self {
impl From<v4l2_requestbuffers> for RequestBuffers {
fn from(reqbufs: v4l2_requestbuffers) -> Self {
RequestBuffers {
count: reqbufs.count,
capabilities: BufferCapabilities::from_bits_truncate(reqbufs.capabilities),
Expand Down Expand Up @@ -91,13 +81,13 @@ impl From<ReqbufsError> for Errno {
}

/// Safe wrapper around the `VIDIOC_REQBUFS` ioctl.
pub fn reqbufs<T: ReqBufs>(
pub fn reqbufs<T: From<v4l2_requestbuffers>>(
fd: &impl AsRawFd,
queue: QueueType,
memory: MemoryType,
count: u32,
) -> Result<T, ReqbufsError> {
let mut reqbufs = bindings::v4l2_requestbuffers {
let mut reqbufs = v4l2_requestbuffers {
count,
type_: queue as u32,
memory: memory as u32,
Expand Down

0 comments on commit da3c5d0

Please sign in to comment.