Skip to content

Commit

Permalink
Address all (clippy) lints (raymanfx#54)
Browse files Browse the repository at this point in the history
* Convert Into<> to From<> implementations

Rust automatically implements Into<> when a From<> implementation
exists.

* Bump bindgen to 0.59

* Address all remaining clippy lints

* Fix formatting issue

Signed-off-by: Christopher N. Hesse <[email protected]>

Co-authored-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
MarijnS95 and raymanfx authored Feb 20, 2022
1 parent 1818e7e commit edd8a64
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 126 deletions.
2 changes: 1 addition & 1 deletion examples/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> io::Result<()> {
}
}

println!("")
println!()
}

Ok(())
Expand Down
27 changes: 11 additions & 16 deletions examples/glium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> io::Result<()> {
let mut format: Format;
let params: Parameters;

let dev = RwLock::new(Device::with_path(path.clone())?);
let dev = RwLock::new(Device::with_path(path)?);
{
let dev = dev.write().unwrap();
format = dev.format()?;
Expand Down Expand Up @@ -95,8 +95,7 @@ fn main() -> io::Result<()> {

// building the index buffer
let index_buffer =
glium::IndexBuffer::new(&display, PrimitiveType::TriangleStrip, &[1 as u16, 2, 0, 3])
.unwrap();
glium::IndexBuffer::new(&display, PrimitiveType::TriangleStrip, &[1u16, 2, 0, 3]).unwrap();

// compiling shaders and linking them together
let program = program!(&display,
Expand Down Expand Up @@ -129,11 +128,10 @@ fn main() -> io::Result<()> {
let (tx, rx) = mpsc::channel();

thread::spawn(move || {
let mut dev = dev.write().unwrap();
let dev = dev.write().unwrap();

// Setup a buffer stream
let mut stream =
MmapStream::with_buffers(&mut *dev, Type::VideoCapture, buffer_count).unwrap();
let mut stream = MmapStream::with_buffers(&dev, Type::VideoCapture, buffer_count).unwrap();

loop {
let (buf, _) = stream.next().unwrap();
Expand All @@ -142,8 +140,7 @@ fn main() -> io::Result<()> {
b"MJPG" => {
// Decode the JPEG frame to RGB
let mut decoder = jpeg::Decoder::new(buf);
let pixels = decoder.decode().expect("failed to decode JPEG");
pixels
decoder.decode().expect("failed to decode JPEG")
}
_ => panic!("invalid buffer pixelformat"),
};
Expand Down Expand Up @@ -186,14 +183,12 @@ fn main() -> io::Result<()> {
target.finish().unwrap();

// polling and handling the events received by the window
match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
glutin::event::WindowEvent::CloseRequested => {
*control_flow = glutin::event_loop::ControlFlow::Exit;
}
_ => {}
},
_ => {}
if let glutin::event::Event::WindowEvent {
event: glutin::event::WindowEvent::CloseRequested,
..
} = event
{
*control_flow = glutin::event_loop::ControlFlow::Exit;
}

print!(
Expand Down
4 changes: 2 additions & 2 deletions examples/stream_capture_mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() -> io::Result<()> {
// Allocate 4 buffers by default
let buffer_count = 4;

let mut dev = Device::with_path(path)?;
let dev = Device::with_path(path)?;
let format = dev.format()?;
let params = dev.params()?;
println!("Active format:\n{}", format);
println!("Active parameters:\n{}", params);

// Setup a buffer stream and grab a frame, then print its data
let mut stream = MmapStream::with_buffers(&mut dev, Type::VideoCapture, buffer_count)?;
let mut stream = MmapStream::with_buffers(&dev, Type::VideoCapture, buffer_count)?;

// warmup
stream.next()?;
Expand Down
4 changes: 2 additions & 2 deletions examples/stream_capture_userptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() -> io::Result<()> {
// Allocate 4 buffers by default
let buffer_count = 4;

let mut dev = Device::with_path(path)?;
let dev = Device::with_path(path)?;
let format = dev.format()?;
let params = dev.params()?;
println!("Active format:\n{}", format);
println!("Active parameters:\n{}", params);

// Setup a buffer stream and grab a frame, then print its data
let mut stream = UserptrStream::with_buffers(&mut dev, Type::VideoCapture, buffer_count)?;
let mut stream = UserptrStream::with_buffers(&dev, Type::VideoCapture, buffer_count)?;

// warmup
stream.next()?;
Expand Down
10 changes: 5 additions & 5 deletions examples/stream_forward_mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ fn main() -> io::Result<()> {
// Allocate 4 buffers by default
let buffer_count = 4;

let mut cap = Device::with_path(source)?;
let cap = Device::with_path(source)?;
println!("Active cap capabilities:\n{}", cap.query_caps()?);
println!("Active cap format:\n{}", Capture::format(&cap)?);
println!("Active cap parameters:\n{}", Capture::params(&cap)?);

let mut out = Device::with_path(sink)?;
let out = Device::with_path(sink)?;
println!("Active out capabilities:\n{}", out.query_caps()?);
println!("Active out format:\n{}", Output::format(&out)?);
println!("Active out parameters:\n{}", Output::params(&out)?);
Expand All @@ -35,7 +35,7 @@ fn main() -> io::Result<()> {
// a format is set, even though a valid format appears to be available when doing VIDIOC_G_FMT!
// In our case, we just (try to) enforce the source format on the sink device.
let source_fmt = Capture::format(&cap)?;
let sink_fmt = Output::set_format(&mut out, &source_fmt)?;
let sink_fmt = Output::set_format(&out, &source_fmt)?;
if source_fmt.width != sink_fmt.width
|| source_fmt.height != sink_fmt.height
|| source_fmt.fourcc != sink_fmt.fourcc
Expand All @@ -48,8 +48,8 @@ fn main() -> io::Result<()> {
println!("New out format:\n{}", Output::format(&out)?);

// Setup a buffer stream and grab a frame, then print its data
let mut cap_stream = MmapStream::with_buffers(&mut cap, Type::VideoCapture, buffer_count)?;
let mut out_stream = MmapStream::with_buffers(&mut out, Type::VideoOutput, buffer_count)?;
let mut cap_stream = MmapStream::with_buffers(&cap, Type::VideoCapture, buffer_count)?;
let mut out_stream = MmapStream::with_buffers(&out, Type::VideoOutput, buffer_count)?;

// warmup
CaptureStream::next(&mut cap_stream)?;
Expand Down
10 changes: 5 additions & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ impl Default for Flags {
}

impl From<u32> for Flags {
fn from(flags: u32) -> Flags {
Flags::from_bits_truncate(flags)
fn from(flags: u32) -> Self {
Self::from_bits_truncate(flags)
}
}

impl Into<u32> for Flags {
fn into(self) -> u32 {
self.bits()
impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

Expand Down
23 changes: 11 additions & 12 deletions src/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ bitflags! {

impl From<u32> for Flags {
fn from(flags: u32) -> Self {
Flags::from_bits_truncate(flags)
Self::from_bits_truncate(flags)
}
}

impl Into<u32> for Flags {
fn into(self) -> u32 {
self.bits()
impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ pub struct Capabilities {

impl From<v4l2_capability> for Capabilities {
fn from(cap: v4l2_capability) -> Self {
let mut caps = Capabilities {
Self {
driver: str::from_utf8(&cap.driver)
.unwrap()
.trim_matches(char::from(0))
Expand All @@ -93,14 +93,13 @@ impl From<v4l2_capability> for Capabilities {
.unwrap()
.trim_matches(char::from(0))
.to_string(),
version: (0, 0, 0),
version: (
((cap.version >> 16) & 0xff) as u8,
((cap.version >> 8) & 0xff) as u8,
(cap.version & 0xff) as u8,
),
capabilities: Flags::from(cap.device_caps),
};

caps.version.0 = ((cap.version >> 16) & 0xff) as u8;
caps.version.1 = ((cap.version >> 8) & 0xff) as u8;
caps.version.2 = (cap.version & 0xff) as u8;
caps
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl TryFrom<u32> for Type {
}
}

impl Into<u32> for Type {
fn into(self) -> u32 {
self as u32
impl From<Type> for u32 {
fn from(t: Type) -> Self {
t as Self
}
}

Expand Down Expand Up @@ -85,13 +85,13 @@ bitflags! {

impl From<u32> for Flags {
fn from(flags: u32) -> Self {
Flags::from_bits_truncate(flags)
Self::from_bits_truncate(flags)
}
}

impl Into<u32> for Flags {
fn into(self) -> u32 {
self.bits()
impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ pub struct Description {

impl From<v4l2_query_ext_ctrl> for Description {
fn from(ctrl: v4l2_query_ext_ctrl) -> Self {
Description {
Self {
id: ctrl.id,
typ: Type::try_from(ctrl.type_).unwrap(),
name: unsafe { ffi::CStr::from_ptr(ctrl.name.as_ptr()) }
Expand Down
10 changes: 5 additions & 5 deletions src/format/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ bitflags! {

impl From<u32> for Flags {
fn from(flags: u32) -> Self {
Flags::from_bits_truncate(flags)
Self::from_bits_truncate(flags)
}
}

impl Into<u32> for Flags {
fn into(self) -> u32 {
self.bits()
impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

Expand Down Expand Up @@ -55,7 +55,7 @@ impl fmt::Display for Description {

impl From<v4l2_fmtdesc> for Description {
fn from(desc: v4l2_fmtdesc) -> Self {
Description {
Self {
index: desc.index,
typ: desc.type_,
flags: Flags::from(desc.flags),
Expand Down
14 changes: 7 additions & 7 deletions src/format/fourcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ impl From<u32> for FourCC {
}
}

impl Into<u32> for FourCC {
fn into(self: FourCC) -> u32 {
let mut code: u32;
impl From<FourCC> for u32 {
fn from(fourcc: FourCC) -> Self {
let mut code: Self;

code = self.repr[0] as u32;
code |= (self.repr[1] as u32) << 8;
code |= (self.repr[2] as u32) << 16;
code |= (self.repr[3] as u32) << 24;
code = fourcc.repr[0] as u32;
code |= (fourcc.repr[1] as u32) << 8;
code |= (fourcc.repr[2] as u32) << 16;
code |= (fourcc.repr[3] as u32) << 24;
code
}
}
36 changes: 18 additions & 18 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ bitflags! {

impl From<u32> for Flags {
fn from(flags: u32) -> Self {
Flags::from_bits_truncate(flags)
Self::from_bits_truncate(flags)
}
}

impl Into<u32> for Flags {
fn into(self) -> u32 {
self.bits()
impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ impl fmt::Display for Format {

impl From<v4l2_pix_format> for Format {
fn from(fmt: v4l2_pix_format) -> Self {
Format {
Self {
width: fmt.width,
height: fmt.height,
fourcc: FourCC::from(fmt.pixelformat),
Expand All @@ -137,19 +137,19 @@ impl From<v4l2_pix_format> for Format {
}
}

impl Into<v4l2_pix_format> for Format {
fn into(self: Format) -> v4l2_pix_format {
v4l2_pix_format {
width: self.width,
height: self.height,
pixelformat: self.fourcc.into(),
field: self.field_order as u32,
bytesperline: self.stride,
sizeimage: self.size,
colorspace: self.colorspace as u32,
flags: self.flags.into(),
quantization: self.quantization as u32,
xfer_func: self.transfer as u32,
impl From<Format> for v4l2_pix_format {
fn from(format: Format) -> Self {
Self {
width: format.width,
height: format.height,
pixelformat: format.fourcc.into(),
field: format.field_order as u32,
bytesperline: format.stride,
sizeimage: format.size,
colorspace: format.colorspace as u32,
flags: format.flags.into(),
quantization: format.quantization as u32,
xfer_func: format.transfer as u32,
..unsafe { mem::zeroed() }
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ impl fmt::Display for Fraction {

impl From<v4l2_fract> for Fraction {
fn from(frac: v4l2_fract) -> Self {
Fraction {
Self {
numerator: frac.numerator,
denominator: frac.denominator,
}
}
}

impl Into<v4l2_fract> for Fraction {
fn into(self: Fraction) -> v4l2_fract {
v4l2_fract {
numerator: self.numerator,
denominator: self.denominator,
impl From<Fraction> for v4l2_fract {
fn from(fraction: Fraction) -> Self {
Self {
numerator: fraction.numerator,
denominator: fraction.denominator,
}
}
}
5 changes: 1 addition & 4 deletions src/framesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ impl FrameSizeEnum {
for height in (stepwise.min_height..=stepwise.max_height)
.step_by(stepwise.step_height as usize)
{
discrete.push(Discrete {
width: width,
height: height,
});
discrete.push(Discrete { width, height });
}
}

Expand Down
Loading

0 comments on commit edd8a64

Please sign in to comment.