Skip to content

Commit

Permalink
Fix the more pedantic clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Guggi authored and florg-32 committed Sep 15, 2024
1 parent 3cdc045 commit abee61c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pedantic = { level = "warn", priority = -1 }
missing_errors_doc = "allow"
module_name_repetitions = "allow"
missing_panics_doc = "allow"
should_panic_without_expect = "allow"
35 changes: 17 additions & 18 deletions filevec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ impl<T: Serialize + DeserializeOwned> FileVec<T> {
/// **Note:** If the file exists and contains invalid data, it is interpreted as
/// empty and overwritten.
pub fn open(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
let mut file =
std::fs::OpenOptions::new().read(true).write(true).create(true).open(path)?;
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)?;
let metadata = file.metadata()?;

let vec = if metadata.len() > 0 {
Expand Down Expand Up @@ -151,17 +155,12 @@ impl<'a, T: Serialize + DeserializeOwned> Drop for FileVecGuard<'a, T> {

#[cfg(test)]
mod test {
use std::{
fs::File,
io::{Read, Write},
ops::DerefMut,
};

use super::FileVec;
use std::io::{Read, Write};

#[test]
fn empty_vec() {
let f = FileVec::<u16>::open("__empty_vec".to_string()).unwrap();
let f = FileVec::<u16>::open("__empty_vec").unwrap();

assert_eq!(f.as_ref().len(), 0);
assert!(std::path::Path::new("__empty_vec").exists());
Expand All @@ -175,20 +174,20 @@ mod test {
let buffer = rmp_serde::to_vec(&DATA).unwrap();
std::fs::File::create("__prefilled").unwrap().write_all(&buffer).unwrap();

let f = FileVec::<u8>::open("__prefilled".to_string()).unwrap();
let f = FileVec::<u8>::open("__prefilled").unwrap();
assert_eq!(&DATA, f.as_ref().as_slice());

let _ = std::fs::remove_file("__prefilled");
}

#[test]
fn push_single() {
let mut f = FileVec::<i32>::open("__push_single".to_string()).unwrap();
let mut f = FileVec::<i32>::open("__push_single").unwrap();
f.push(123).unwrap();
assert_eq!(f[0], 123);

drop(f);
let f = FileVec::<i32>::open("__push_single".to_string()).unwrap();
let f = FileVec::<i32>::open("__push_single").unwrap();
assert_eq!(f[0], 123);

let _ = std::fs::remove_file("__push_single");
Expand All @@ -203,13 +202,13 @@ mod test {

#[test]
fn push_multiple_structs() {
let mut f = FileVec::open("__push_multiple".to_string()).unwrap();
let mut f = FileVec::open("__push_multiple").unwrap();
f.push(TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() }).unwrap();
f.push(TestaStruct { int16: 3, uint32: 4, stringa: "Hello2".into() }).unwrap();
f.push(TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() }).unwrap();
drop(f);

let f: FileVec<TestaStruct> = FileVec::open("__push_multiple".to_string()).unwrap();
let f: FileVec<TestaStruct> = FileVec::open("__push_multiple").unwrap();
assert_eq!(f[0], TestaStruct { int16: 1, uint32: 2, stringa: "Hello".into() });
assert_eq!(f[2], TestaStruct { int16: 5, uint32: 6, stringa: "Hello3".into() });

Expand All @@ -218,8 +217,8 @@ mod test {

#[test]
fn remove() {
let mut f: FileVec<i32> = FileVec::open("__remove".to_string()).unwrap();
f.extend([0i32, 1, 2, 3, 4, 5, 6].into_iter());
let mut f: FileVec<i32> = FileVec::open("__remove").unwrap();
f.extend([0i32, 1, 2, 3, 4, 5, 6]);

let mut buffer = Vec::new();
std::fs::File::open("__remove").unwrap().read_to_end(&mut buffer).unwrap();
Expand All @@ -237,8 +236,8 @@ mod test {

#[test]
fn pop() {
let mut f = FileVec::open("__pop".to_string()).unwrap();
f.extend([0, 1, 2, 3].into_iter());
let mut f = FileVec::open("__pop").unwrap();
f.extend([0, 1, 2, 3]);

assert_eq!(f.pop().unwrap(), Some(3));
assert_eq!(f.pop().unwrap(), Some(2));
Expand Down
9 changes: 3 additions & 6 deletions scheduler/src/command/execute_program.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use super::{CommandError, CommandResult, SyncExecutionContext};
use crate::{
command::{
check_length, terminate_student_program, truncate_to_size, Event, ProgramStatus, ResultId,
RetryEvent,
check_length, terminate_student_program, Event, ProgramStatus, ResultId, RetryEvent,
},
communication::{CEPPacket, CommunicationHandle},
};
use anyhow::anyhow;
use simple_archive::Compression;
use std::{
fs::File,
io::{ErrorKind, Read, Write},
io::{ErrorKind, Write},
path::{Path, PathBuf},
time::Duration,
};
Expand Down Expand Up @@ -152,8 +150,7 @@ fn build_result_archive(res: ResultId) -> Result<(), std::io::Error> {

let res_path =
PathBuf::from(format!("./archives/{}/results/{}", res.program_id, res.timestamp));
let student_log_path =
PathBuf::from(format!("./data/{res}.log"));
let student_log_path = PathBuf::from(format!("./data/{res}.log"));
let log_path = PathBuf::from("./log");

add_to_archive_if_exists(&mut archive, &res.to_string(), res_path, Compression::None)?;
Expand Down
5 changes: 4 additions & 1 deletion scheduler/src/command/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use filevec::FileVec;
use std::{
fmt::Display, str::FromStr, sync::{Arc, Mutex}, thread
fmt::Display,
str::FromStr,
sync::{Arc, Mutex},
thread,
};

const EVENT_SEND_TRIES: u32 = 5;
Expand Down
1 change: 1 addition & 0 deletions scheduler/tests/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn execute_program(program_id: u16, timestamp: u32, timeout: u16) -> Vec<u8>
vec
}

#[allow(dead_code)]
pub fn stop_program() -> Vec<u8> {
vec![3u8]
}
Expand Down
1 change: 1 addition & 0 deletions scheduler/tests/software_tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub fn cleanup(unique: &str) {
let _ = std::fs::remove_file(format!("tests/tmp/{unique}_r"));
}

#[allow(dead_code)]
pub fn store_archive(program_id: u16) -> Vec<u8> {
let mut vec = vec![1u8];
vec.extend(program_id.to_le_bytes());
Expand Down

0 comments on commit abee61c

Please sign in to comment.