Skip to content

Commit

Permalink
Unwrap warning addition (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Nov 28, 2023
1 parent 408a468 commit 72921d0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
1 change: 1 addition & 0 deletions growth-ring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ path = "src/lib.rs"
crate-type = ["dylib", "rlib", "staticlib"]

[lints.clippy]
unwrap_used = "warn"
missing_const_for_fn = "warn"
21 changes: 10 additions & 11 deletions growth-ring/examples/demo1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn main() -> Result<(), WalError> {
let mut loader = WalLoader::new();
loader.file_nbit(9).block_nbit(8);

let store = WalStoreImpl::new(wal_dir, true).unwrap();
let mut wal = block_on(loader.load(store, recover, 0)).unwrap();
let store = WalStoreImpl::new(wal_dir, true)?;
let mut wal = block_on(loader.load(store, recover, 0))?;
for _ in 0..3 {
let _ = test(
["hi", "hello", "lol"]
Expand All @@ -55,8 +55,8 @@ fn main() -> Result<(), WalError> {
);
}

let store = WalStoreImpl::new(wal_dir, false).unwrap();
let mut wal = block_on(loader.load(store, recover, 0)).unwrap();
let store = WalStoreImpl::new(wal_dir, false)?;
let mut wal = block_on(loader.load(store, recover, 0))?;
for _ in 0..3 {
let _ = test(
vec![
Expand All @@ -69,8 +69,8 @@ fn main() -> Result<(), WalError> {
);
}

let store = WalStoreImpl::new(wal_dir, false).unwrap();
let mut wal = block_on(loader.load(store, recover, 100)).unwrap();
let store = WalStoreImpl::new(wal_dir, false)?;
let mut wal = block_on(loader.load(store, recover, 100))?;
let mut history = std::collections::VecDeque::new();
for _ in 0..3 {
let mut ids = Vec::new();
Expand All @@ -94,17 +94,16 @@ fn main() -> Result<(), WalError> {
ids.shuffle(&mut rng);
for e in ids.chunks(20) {
println!("peel(20)");
futures::executor::block_on(wal.peel(e, 100)).unwrap();
futures::executor::block_on(wal.peel(e, 100))?;
}
}
for (rec, ans) in
block_on(wal.read_recent_records(100, &growthring::wal::RecoverPolicy::Strict))
.unwrap()
block_on(wal.read_recent_records(100, &growthring::wal::RecoverPolicy::Strict))?
.into_iter()
.zip(history.into_iter().rev())
{
assert_eq!(std::str::from_utf8(&rec).unwrap(), &ans);
println!("{}", std::str::from_utf8(&rec).unwrap());
assert_eq!(&String::from_utf8_lossy(&rec), &ans);
println!("{}", String::from_utf8_lossy(&rec));
}

Ok(())
Expand Down
23 changes: 23 additions & 0 deletions growth-ring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl WalStore<WalFileImpl> for WalStoreImpl {

fn enumerate_files(&self) -> Result<Self::FileNameIter, WalError> {
let mut filenames = Vec::new();
#[allow(clippy::unwrap_used)]
for path in fs::read_dir(&self.root_dir)?.filter_map(|entry| entry.ok()) {
filenames.push(path.path());
}
Expand All @@ -198,6 +199,7 @@ mod tests {

tokio::fs::remove_file(&walfile_path).await.ok();

#[allow(clippy::unwrap_used)]
let walfile = RawWalFile::open(walfile_path).await.unwrap();

let walfile_impl = WalFileImpl::from(walfile);
Expand All @@ -211,9 +213,12 @@ mod tests {
.chain(second_half.iter().copied())
.collect();

#[allow(clippy::unwrap_used)]
walfile_impl.write(0, data).await.unwrap();
#[allow(clippy::unwrap_used)]
walfile_impl.truncate(HALF_LENGTH).await.unwrap();

#[allow(clippy::unwrap_used)]
let result = walfile_impl.read(0, HALF_LENGTH).await.unwrap();

assert_eq!(result, Some(first_half.into()))
Expand All @@ -227,17 +232,21 @@ mod tests {

tokio::fs::remove_file(&walfile_path).await.ok();

#[allow(clippy::unwrap_used)]
let walfile = RawWalFile::open(walfile_path).await.unwrap();

let walfile_impl = WalFileImpl::from(walfile);

#[allow(clippy::unwrap_used)]
walfile_impl
.write(0, vec![1u8; LENGTH].into())
.await
.unwrap();

#[allow(clippy::unwrap_used)]
walfile_impl.truncate(2 * LENGTH).await.unwrap();

#[allow(clippy::unwrap_used)]
let result = walfile_impl.read(LENGTH as u64, LENGTH).await.unwrap();

assert_eq!(result, Some(vec![0u8; LENGTH].into()))
Expand All @@ -248,15 +257,18 @@ mod tests {
let walfile = {
let walfile_path = get_temp_walfile_path(file!(), line!());
tokio::fs::remove_file(&walfile_path).await.ok();
#[allow(clippy::unwrap_used)]
RawWalFile::open(walfile_path).await.unwrap()
};

let walfile_impl = WalFileImpl::from(walfile);

let data: Vec<u8> = (0..=u8::MAX).collect();

#[allow(clippy::unwrap_used)]
walfile_impl.write(0, data.clone().into()).await.unwrap();

#[allow(clippy::unwrap_used)]
let result = walfile_impl.read(0, data.len()).await.unwrap();

assert_eq!(result, Some(data.into()));
Expand All @@ -267,17 +279,21 @@ mod tests {
let walfile = {
let walfile_path = get_temp_walfile_path(file!(), line!());
tokio::fs::remove_file(&walfile_path).await.ok();
#[allow(clippy::unwrap_used)]
RawWalFile::open(walfile_path).await.unwrap()
};

let walfile_impl = WalFileImpl::from(walfile);

let data: Vec<u8> = (0..=u8::MAX).collect();
#[allow(clippy::unwrap_used)]
walfile_impl.write(0, data.clone().into()).await.unwrap();

let mid = data.len() / 2;
let (start, end) = data.split_at(mid);
#[allow(clippy::unwrap_used)]
let read_start_result = walfile_impl.read(0, mid).await.unwrap();
#[allow(clippy::unwrap_used)]
let read_end_result = walfile_impl.read(mid as u64, mid).await.unwrap();

assert_eq!(read_start_result, Some(start.into()));
Expand All @@ -289,15 +305,18 @@ mod tests {
let walfile = {
let walfile_path = get_temp_walfile_path(file!(), line!());
tokio::fs::remove_file(&walfile_path).await.ok();
#[allow(clippy::unwrap_used)]
RawWalFile::open(walfile_path).await.unwrap()
};

let walfile_impl = WalFileImpl::from(walfile);

let data: Vec<u8> = (0..=u8::MAX).collect();

#[allow(clippy::unwrap_used)]
walfile_impl.write(0, data.clone().into()).await.unwrap();

#[allow(clippy::unwrap_used)]
let result = walfile_impl
.read((data.len() / 2) as u64, data.len())
.await
Expand All @@ -313,18 +332,21 @@ mod tests {
let walfile = {
let walfile_path = get_temp_walfile_path(file!(), line!());
tokio::fs::remove_file(&walfile_path).await.ok();
#[allow(clippy::unwrap_used)]
RawWalFile::open(walfile_path).await.unwrap()
};

let walfile_impl = WalFileImpl::from(walfile);

let data: Vec<u8> = (0..=u8::MAX).collect();

#[allow(clippy::unwrap_used)]
walfile_impl
.write(OFFSET, data.clone().into())
.await
.unwrap();

#[allow(clippy::unwrap_used)]
let result = walfile_impl
.read(0, data.len() + OFFSET as usize)
.await
Expand All @@ -338,6 +360,7 @@ mod tests {
assert_eq!(result, Some(data.into()));
}

#[allow(clippy::unwrap_used)]
fn get_temp_walfile_path(file: &str, line: u32) -> PathBuf {
let path = option_env!("CARGO_TARGET_TMPDIR")
.map(PathBuf::from)
Expand Down
8 changes: 8 additions & 0 deletions growth-ring/src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ impl<F: WalFile + 'static, S: WalStore<F>> WalFilePool<F, S> {
})
}

#[allow(clippy::unwrap_used)]
async fn read_header(&self) -> Result<Header, WalError> {
let bytes = self.header_file.read(0, HEADER_SIZE).await?.unwrap();
#[allow(clippy::unwrap_used)]
let bytes: [u8; HEADER_SIZE] = (&*bytes).try_into().unwrap();
let header: Header = cast_slice(&bytes)[0];
Ok(header)
Expand Down Expand Up @@ -450,6 +452,7 @@ impl<F: WalFile + 'static, S: WalStore<F>> WalFilePool<F, S> {

let mut removes: Vec<Pin<Box<dyn Future<Output = Result<(), WalError>>>>> = Vec::new();

#[allow(clippy::unwrap_used)]
while state.pending_removal.len() > 1 {
let (fid, counter) = state.pending_removal.front().unwrap();

Expand Down Expand Up @@ -707,6 +710,7 @@ impl<F: WalFile + 'static, S: WalStore<F>> WalWriter<F, S> {
break;
}

#[allow(clippy::unwrap_used)]
let mut m = state.io_complete.pop().unwrap();
let block_remain = block_size - (m.end & (block_size - 1));

Expand Down Expand Up @@ -741,6 +745,7 @@ impl<F: WalFile + 'static, S: WalStore<F>> WalWriter<F, S> {
self.file_pool.in_use_len()
}

#[allow(clippy::unwrap_used)]
pub async fn read_recent_records<'a>(
&'a self,
nrecords: usize,
Expand Down Expand Up @@ -774,6 +779,7 @@ impl<F: WalFile + 'static, S: WalStore<F>> WalWriter<F, S> {
for ring in rings.into_iter().rev() {
let ring = ring.map_err(|_| WalError::Other("error mapping ring".to_string()))?;
let (header, payload) = ring;
#[allow(clippy::unwrap_used)]
let payload = payload.unwrap();
match header.rtype.try_into() {
Ok(WalRingType::Full) => {
Expand Down Expand Up @@ -853,6 +859,7 @@ pub struct WalLoader {
}

impl Default for WalLoader {
#[allow(clippy::unwrap_used)]
fn default() -> Self {
WalLoader {
file_nbit: 22, // 4MB
Expand Down Expand Up @@ -1316,6 +1323,7 @@ impl WalLoader {
pub const CRC32: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
use test_case::test_case;
Expand Down
11 changes: 11 additions & 0 deletions growth-ring/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,19 @@ impl PaintStrokes {
assert!(raw.len() & 3 == 0);
let is = std::mem::size_of::<u32>();
let (len_raw, mut rest) = raw.split_at(is);
#[allow(clippy::unwrap_used)]
let len = u32::from_le_bytes(len_raw.try_into().unwrap());
let mut res = Vec::new();
for _ in 0..len {
let (s_raw, rest1) = rest.split_at(is);
let (e_raw, rest2) = rest1.split_at(is);
let (c_raw, rest3) = rest2.split_at(is);
#[allow(clippy::unwrap_used)]
res.push((
u32::from_le_bytes(s_raw.try_into().unwrap()),
#[allow(clippy::unwrap_used)]
u32::from_le_bytes(e_raw.try_into().unwrap()),
#[allow(clippy::unwrap_used)]
u32::from_le_bytes(c_raw.try_into().unwrap()),
));
rest = rest3
Expand Down Expand Up @@ -390,6 +394,7 @@ impl Canvas {
return None;
}
let idx = rng.gen_range(0..self.queue.len());
#[allow(clippy::unwrap_used)]
let (pos, _) = self.queue.get_index(idx).unwrap();
let pos = *pos;
Some((self.paint(pos), pos))
Expand All @@ -400,6 +405,7 @@ impl Canvas {
self.waiting.clear();
}

#[allow(clippy::unwrap_used)]
pub fn paint_all(&mut self) {
for (pos, q) in self.queue.iter() {
self.canvas[*pos as usize] = q.back().unwrap().0;
Expand All @@ -411,8 +417,10 @@ impl Canvas {
self.queue.is_empty()
}

#[allow(clippy::unwrap_used)]
pub fn paint(&mut self, pos: u32) -> Option<WalRingId> {
let q = self.queue.get_mut(&pos).unwrap();
#[allow(clippy::unwrap_used)]
let (c, rid) = q.pop_front().unwrap();
if q.is_empty() {
self.queue.remove(&pos);
Expand Down Expand Up @@ -574,6 +582,7 @@ impl PaintingSim {

pub fn get_walloader(&self) -> WalLoader {
let mut loader = WalLoader::new();
#[allow(clippy::unwrap_used)]
loader
.file_nbit(self.file_nbit)
.block_nbit(self.block_nbit)
Expand All @@ -586,6 +595,7 @@ impl PaintingSim {
let mut ops: Vec<PaintStrokes> = Vec::new();
let mut ringid_map = HashMap::new();
let fgen = Rc::new(CountFailGen::new());
#[allow(clippy::unwrap_used)]
self.run(
state,
&mut canvas,
Expand All @@ -612,6 +622,7 @@ impl PaintingSim {
let mut last_idx = 0;
let mut napplied = 0;
canvas.clear_queued();
#[allow(clippy::unwrap_used)]
block_on(wal.load(
WalStoreEmul::new(state, Rc::new(ZeroFailGen)),
|payload, ringid| {
Expand Down

0 comments on commit 72921d0

Please sign in to comment.