Skip to content

Commit

Permalink
lint with clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Sep 20, 2024
1 parent 26f36cc commit 496baab
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 88 deletions.
1 change: 1 addition & 0 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rustup install nightly
rustup component add clippy
sudo apt-get update && sudo apt-get install -y \
clang \
libavdevice-dev \
Expand Down
29 changes: 12 additions & 17 deletions adder-codec-core/src/codec/compressed/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use bitstream_io::{BigEndian, BitRead, BitReader, BitWrite, BitWriter};
use priority_queue::PriorityQueue;
use std::cmp::Reverse;
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::ops::{Add, AddAssign};
use std::sync::{Arc, RwLock};

use crate::codec::compressed::source_model::event_structure::event_adu::EventAdu;
use crate::codec::compressed::source_model::HandleEvent;
use crate::codec::header::{Magic, MAGIC_COMPRESSED};
use crate::codec::rate_controller::CrfParameters;
use crate::{DeltaT, Event};

/// A message to send to the writer thread (that is, the main thread) to write out the compressed
Expand Down Expand Up @@ -71,7 +69,7 @@ pub struct CompressedInput<R: Read> {
// }

fn flush_bytes_queue_worker<W: Write>(
mut stream: Arc<RwLock<BitWriter<W, BigEndian>>>,
stream: Arc<RwLock<BitWriter<W, BigEndian>>>,
written_bytes_rx: std::sync::mpsc::Receiver<BytesMessage>,
last_message_written: Arc<RwLock<u32>>,
mut bytes_writer_queue: PriorityQueue<Vec<u8>, Reverse<u32>>,
Expand Down Expand Up @@ -105,7 +103,7 @@ fn flush_bytes_queue_worker<W: Write>(
impl<W: Write + std::marker::Send + std::marker::Sync + 'static> CompressedOutput<W> {
/// Create a new compressed output stream.
pub fn new(meta: CodecMetadata, writer: W) -> Self {
let adu = EventAdu::new(meta.plane, 0, meta.ref_interval, meta.adu_interval as usize);
let adu = EventAdu::new(meta.plane, 0, meta.ref_interval, meta.adu_interval);
let (written_bytes_tx, written_bytes_rx) = std::sync::mpsc::channel();

let stream_lock = RwLock::new(BitWriter::endian(writer, BigEndian));
Expand Down Expand Up @@ -175,7 +173,7 @@ impl<W: Write + std::marker::Send + std::marker::Sync + 'static + 'static + 'sta
self.stream().write().unwrap().byte_align()
}

fn into_writer(&mut self) -> Option<W> {
fn into_writer(mut self) -> Option<W> {
if !self.adu.skip_adu {
// while self.last_message_sent
// != self.last_message_written + self.bytes_writer_queue.len() as u32
Expand Down Expand Up @@ -203,7 +201,7 @@ impl<W: Write + std::marker::Send + std::marker::Sync + 'static + 'static + 'sta
dbg!("compressing partial last adu");
let mut temp_stream = BitWriter::endian(Vec::new(), BigEndian);

let parameters = self.options.crf.get_parameters().clone();
let parameters = *self.options.crf.get_parameters();
let mut adu = self.adu.clone();
let tx = self.written_bytes_tx.as_ref().unwrap().clone();
// Spawn a thread to compress the ADU and write out the data
Expand Down Expand Up @@ -288,7 +286,7 @@ impl<W: Write + std::marker::Send + std::marker::Sync + 'static + 'static + 'sta
// Create a temporary u8 stream to write the arithmetic-coded data to
let mut temp_stream = BitWriter::endian(Vec::new(), BigEndian);

let parameters = self.options.crf.get_parameters().clone();
let parameters = *self.options.crf.get_parameters();

// Compress the Adu. This also writes the EOF symbol and flushes the encoder
// First, clone the ADU
Expand Down Expand Up @@ -492,11 +490,11 @@ mod tests {
compressed_output
.ingest_event(Event {
coord: Coord { x, y, c: None },
t: min(280 + counter, start_t + dt_ref * num_intervals as u32),
t: min(280 + counter, start_t + dt_ref * num_intervals),
d: 7,
})
.unwrap();
if 280 + counter > start_t + dt_ref * num_intervals as u32 {
if 280 + counter > start_t + dt_ref * num_intervals {
break;
} else {
counter += 1;
Expand Down Expand Up @@ -552,14 +550,14 @@ mod tests {
for x in 0..16 {
let event = Event {
coord: Coord { x, y, c: None },
t: min(280 + counter, start_t + dt_ref * num_intervals as u32),
t: min(280 + counter, start_t + dt_ref * num_intervals),
d: 7,
};
if y == candidate_px_idx.0 && x == candidate_px_idx.1 {
input_px_events.push(event);
}
compressed_output.ingest_event(event).unwrap();
if 280 + counter > start_t + dt_ref * num_intervals as u32 {
if 280 + counter > start_t + dt_ref * num_intervals {
break;
} else {
counter += 1;
Expand All @@ -575,7 +573,7 @@ mod tests {
y: 0,
c: None,
},
t: start_t + dt_ref * num_intervals as u32 + 1,
t: start_t + dt_ref * num_intervals + 1,
d: 7,
})
.unwrap();
Expand All @@ -591,11 +589,8 @@ mod tests {
// Check that the size is less than the raw events
assert!((output.len() as u32) < counter * 9);

let mut compressed_input = CompressedInput::new(
dt_ref * num_intervals as u32,
dt_ref,
num_intervals as usize,
);
let mut compressed_input =
CompressedInput::new(dt_ref * num_intervals, dt_ref, num_intervals as usize);
compressed_input.meta.plane = plane;
let mut stream = BitReader::endian(Cursor::new(output), BigEndian);
for i in 0..counter - 1 {
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-core/src/codec/empty/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<W: std::io::Write + std::marker::Send + std::marker::Sync + 'static> WriteC
Ok(())
}

fn into_writer(&mut self) -> Option<W> {
fn into_writer(self) -> Option<W> {
None
}

Expand Down
2 changes: 1 addition & 1 deletion adder-codec-core/src/codec/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<W: Write + 'static + std::marker::Send + std::marker::Sync> Encoder<W> {
}

/// Close the encoder's writer and return it, consuming the encoder in the process.
pub fn close_writer(mut self) -> Result<Option<W>, CodecError> {
pub fn close_writer(self) -> Result<Option<W>, CodecError> {
// self.output.byte_align()?;
// self.write_eof()?;
// self.flush_writer()?;
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-core/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub trait WriteCompression<W: Write + std::marker::Send + std::marker::Sync + 's
fn byte_align(&mut self) -> io::Result<()>;

/// Consumes the compression stream and returns the underlying writer.
fn into_writer(&mut self) -> Option<W>;
fn into_writer(self) -> Option<W>;

/// Flush the `BitWriter`. Does not flush the internal `BufWriter`.
fn flush_writer(&mut self) -> io::Result<()>;
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-core/src/codec/raw/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<W: Write + std::marker::Send + std::marker::Sync + 'static> WriteCompressio
}

// If `self.writer` is a `BufWriter`, you'll need to flush it yourself after this.
fn into_writer(&mut self) -> Option<W> {
fn into_writer(mut self) -> Option<W> {
let eof = Event {
coord: Coord {
x: EOF_PX_ADDRESS,
Expand Down
4 changes: 2 additions & 2 deletions adder-codec-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ pub struct EventCoordlessRelative {
pub delta_t: DeltaT,
}

impl Into<f64> for EventCoordless {
fn into(self) -> f64 {
impl From<EventCoordless> for f64 {
fn from(val: EventCoordless) -> Self {
panic!("Not implemented")
}
}
Expand Down
1 change: 0 additions & 1 deletion adder-codec-rs/benches/framed_to_adder_hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::path::PathBuf;

use adder_codec_core::TimeMode::DeltaT;
use adder_codec_rs::transcoder::source::framed::Framed;
use adder_codec_rs::transcoder::source::video::VideoBuilder;
use adder_codec_rs::utils::viz::download_file;
use std::thread::sleep;
use std::time::Duration;
Expand Down
17 changes: 3 additions & 14 deletions adder-codec-rs/src/bin/adder_to_framed.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
use adder_codec_core::codec::compressed::stream::CompressedInput;
use adder_codec_core::codec::decoder::Decoder;
use adder_codec_core::codec::rate_controller::Crf;
use adder_codec_core::codec::raw::stream::RawInput;
use adder_codec_core::codec::{EncoderOptions, EncoderType};
use adder_codec_core::SourceCamera::{DavisU8, Dvs, FramedU8};
use adder_codec_core::open_file_decoder;
use adder_codec_core::SourceType::U8;
use adder_codec_core::{open_file_decoder, PixelMultiMode, TimeMode};
use adder_codec_rs::framer::driver::FramerMode::INSTANTANEOUS;
use adder_codec_rs::framer::driver::{FrameSequence, Framer, FramerBuilder};
use adder_codec_rs::transcoder::source::prophesee::Prophesee;
use adder_codec_rs::transcoder::source::video::SourceError;
use adder_codec_rs::utils::viz::ShowFeatureMode;
use bitstream_io::{BigEndian, BitReader};
use clap::Parser;
use std::fs::File;
use std::io;
use std::io::{BufReader, BufWriter, Write};
use std::io::{BufWriter, Write};
use std::process::Command;
use std::time::Instant;
use video_rs_adder_dep::Locator;

#[derive(Parser, Debug, Default, serde::Deserialize)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -59,7 +48,7 @@ pub struct MyArgs {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args: MyArgs = MyArgs::parse();
let args: MyArgs = MyArgs::parse();

// Set up the adder file reader
let (mut reader, mut bitreader) = open_file_decoder(&args.input)?;
Expand Down
14 changes: 5 additions & 9 deletions adder-codec-rs/src/framer/scale_intensity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl FrameValue for u8 {
FramedViewMode::Intensity => {
let intensity = event_to_intensity(event);
match source_type {
SourceType::U8 => ((intensity * tpf)) as u8,
SourceType::U8 => (intensity * tpf) as u8,
SourceType::U16 => {
(intensity / f64::from(u16::MAX) * tpf * f64::from(u8::MAX)) as u8
}
Expand Down Expand Up @@ -125,13 +125,11 @@ impl FrameValue for u16 {
let intensity = event_to_intensity(event);
match source_type {
SourceType::U8 => {
(intensity / f64::from(u8::MAX) * tpf * f64::from(u16::MAX))
as u16
(intensity / f64::from(u8::MAX) * tpf * f64::from(u16::MAX)) as u16
}
SourceType::U16 => (intensity * tpf) as u16,
SourceType::U32 => {
(intensity / f64::from(u32::MAX) * tpf * f64::from(u16::MAX))
as u16
(intensity / f64::from(u32::MAX) * tpf * f64::from(u16::MAX)) as u16
}
SourceType::U64 => {
(intensity / u64::MAX as f64 * tpf * f64::from(u16::MAX)) as u16
Expand Down Expand Up @@ -178,12 +176,10 @@ impl FrameValue for u32 {
let intensity = event_to_intensity(event);
match source_type {
SourceType::U8 => {
(intensity / f64::from(u8::MAX) * tpf * f64::from(u32::MAX))
as u32
(intensity / f64::from(u8::MAX) * tpf * f64::from(u32::MAX)) as u32
}
SourceType::U16 => {
(intensity / f64::from(u16::MAX) * tpf * f64::from(u32::MAX))
as u32
(intensity / f64::from(u16::MAX) * tpf * f64::from(u32::MAX)) as u32
}
SourceType::U32 => (intensity * tpf) as u32,
SourceType::U64 => {
Expand Down
1 change: 0 additions & 1 deletion adder-codec-rs/src/utils/logging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use adder_codec_core::Coord;
use opencv::core::KeyPoint;
use serde::ser::SerializeStruct;
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-rs/src/utils/viz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::BufWriter;
use std::io::{Cursor, Write};
use std::path::Path;
use std::process::{Command, Output};
use video_rs_adder_dep::{Frame};
use video_rs_adder_dep::Frame;

#[cfg(feature = "open-cv")]
/// Writes a given [`Mat`] to a file
Expand Down
25 changes: 12 additions & 13 deletions adder-to-dvs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use adder_codec_core::codec::CodecMetadata;
use adder_codec_core::*;
use chrono::{DateTime, Local};
use chrono::Local;
use clap::{Parser, ValueEnum};
use ndarray::Array3;
use serde::{Deserialize, Serialize};
Expand All @@ -11,7 +11,6 @@ use std::fs::File;
use std::io::{BufWriter, Write};
use std::option::Option;
use std::path::PathBuf;
use std::time::Instant;
use std::{error, io};
use video_rs::{Encoder, EncoderSettings, Options, PixelFormat};

Expand Down Expand Up @@ -149,18 +148,18 @@ fn main() -> Result<(), Box<dyn error::Error>> {
// .write(dims_str.as_ref())
// .expect("Could not write");

write!(output_events_writer, "% Height {}\n", meta.plane.h())?;
write!(output_events_writer, "% Width {}\n", meta.plane.w())?;
write!(output_events_writer, "% Version 2\n")?;
writeln!(output_events_writer, "% Height {}", meta.plane.h())?;
writeln!(output_events_writer, "% Width {}", meta.plane.w())?;
writeln!(output_events_writer, "% Version 2")?;
// Write the date and time
let now = Local::now();
let date_time_str = now.format("%Y-%m-%d %H:%M:%S").to_string();
write!(output_events_writer, "% Date {}\n", date_time_str)?;
write!(output_events_writer, "% end\n")?;
writeln!(output_events_writer, "% Date {}", date_time_str)?;
writeln!(output_events_writer, "% end")?;

if args.output_mode == WriteMode::Binary {
let event_type_size: [u8; 2] = [0, 8];
output_events_writer.write(&event_type_size)?;
output_events_writer.write_all(&event_type_size)?;
}
}

Expand Down Expand Up @@ -434,7 +433,7 @@ fn set_instant_dvs_pixel(
let grow_len = frame_idx as i32 - frame_count as i32 - frames.len() as i32 + 1;

for _ in 0..grow_len {
frames.push_back(create_blank_dvs_frame(&meta)?);
frames.push_back(create_blank_dvs_frame(meta)?);
}

if frame_idx >= frame_count {
Expand Down Expand Up @@ -472,7 +471,7 @@ pub fn write_frame_to_video(
encoder: &mut video_rs::Encoder,
timestamp: video_rs::Time,
) -> Result<(), Box<dyn Error>> {
encoder.encode(&frame, &timestamp).map_err(|e| e.into())
encoder.encode(frame, &timestamp).map_err(|e| e.into())
}

fn fire_dvs_event(
Expand Down Expand Up @@ -502,8 +501,8 @@ fn fire_dvs_event(
WriteMode::Binary => {
let event = DvsEvent {
t: t as u32,
x: x as u16,
y: y as u16,
x,
y,
p: if polarity { 1 } else { 0 },
};

Expand Down Expand Up @@ -535,7 +534,7 @@ fn write_event_binary(event: &DvsEvent, writer: &mut BufWriter<File>) -> io::Res
let mut buffer = [0; 8];

// t as u32 into the first four bytes of the buffer
buffer[0..4].copy_from_slice(&(event.t as u32).to_le_bytes());
buffer[0..4].copy_from_slice(&(event.t).to_le_bytes());

let mut data: u32 = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::ops::Range;
use crate::BitStore;

/// A [`Model`] is used to calculate the probability of a given symbol occuring
/// in a sequence. The [`Model`] is used both for encoding and decoding. A
/// in a sequence.
///
/// The [`Model`] is used both for encoding and decoding. A
/// 'fixed-length' model always expects an exact number of symbols, and so does
/// not need to encode an EOF symbol.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::ops::Range;
use crate::BitStore;

/// A [`Model`] is used to calculate the probability of a given symbol occuring
/// in a sequence. The [`Model`] is used both for encoding and decoding. A
/// in a sequence.
///
/// The [`Model`] is used both for encoding and decoding. A
/// 'max-length' model has a maximum length. The compressed size of a message
/// equal to the maximum length is larger than with a
/// [`fixed_length::Model`](crate::fixed_length::Model), but smaller than with a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ pub use crate::fixed_length::Wrapper;
use crate::{fixed_length, BitStore};

/// A [`Model`] is used to calculate the probability of a given symbol occuring
/// in a sequence. The [`Model`] is used both for encoding and decoding. A
/// in a sequence.
///
/// The [`Model`] is used both for encoding and decoding. A
/// 'fixed-length' model always expects an exact number of symbols, and so does
/// not need to encode an EOF symbol.
///
Expand Down
3 changes: 1 addition & 2 deletions arithmetic-coding-adder-dep/benches/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use arithmetic_coding_adder_dep::{Model};

use arithmetic_coding_adder_dep::Model;

pub fn round_trip<M>(model: M, input: &[M::Symbol])
where
Expand Down
1 change: 0 additions & 1 deletion arithmetic-coding-adder-dep/examples/concatenated.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(exclusive_range_pattern)]
#![feature(never_type)]

use arithmetic_coding_adder_dep::{Decoder, Encoder, Model};
Expand Down
1 change: 0 additions & 1 deletion arithmetic-coding-adder-dep/examples/max_length.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(exclusive_range_pattern)]
#![feature(never_type)]

use std::ops::Range;
Expand Down
Loading

0 comments on commit 496baab

Please sign in to comment.