Skip to content

Commit 3a9aa07

Browse files
Following the clippy god (#5)
1 parent c0e3bfa commit 3a9aa07

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/vectors.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::{self, Seek, SeekFrom, Write};
77
use std::mem::MaybeUninit;
88
use std::ops::Deref;
99
use std::os::unix::prelude::FileExt;
10-
use std::path::PathBuf;
10+
use std::path::{Path, PathBuf};
1111
use std::sync::atomic::{self, AtomicUsize};
1212
use std::sync::{Arc, Condvar, Mutex, RwLock, Weak};
1313

@@ -45,8 +45,8 @@ pub struct Domain {
4545
}
4646

4747
impl Domain {
48-
fn open(dir: &PathBuf, name: &str, index: usize) -> io::Result<Self> {
49-
let mut path = dir.clone();
48+
fn open(dir: &Path, name: &str, index: usize) -> io::Result<Self> {
49+
let mut path = dir.to_path_buf();
5050
let name = encode(name);
5151
path.push(format!("{name}.vecs"));
5252
let mut write_file = File::options()
@@ -150,9 +150,11 @@ struct PageSpec {
150150
index: usize,
151151
}
152152

153+
type GuardedLoadState = Arc<(Condvar, Mutex<LoadState>)>;
154+
153155
struct PageArena {
154156
free: Mutex<Vec<Box<VectorPage>>>,
155-
loading: Mutex<HashMap<PageSpec, Arc<(Condvar, Mutex<LoadState>)>>>,
157+
loading: Mutex<HashMap<PageSpec, GuardedLoadState>>,
156158
loaded: RwLock<HashMap<PageSpec, PinnedVectorPage>>,
157159
cache: RwLock<LruCache<PageSpec, LoadedVectorPage>>,
158160
}
@@ -167,10 +169,7 @@ enum LoadState {
167169

168170
impl LoadState {
169171
fn is_loading(&self) -> bool {
170-
match self {
171-
Self::Loading => true,
172-
_ => false,
173-
}
172+
matches!(self, Self::Loading)
174173
}
175174
}
176175

@@ -230,7 +229,7 @@ impl PageArena {
230229

231230
fn start_loading_or_wait(self: &Arc<Self>, spec: PageSpec) -> LoadState {
232231
let mut loading = self.loading.lock().unwrap();
233-
if let Some(x) = loading.get(&spec).map(|x| x.clone()) {
232+
if let Some(x) = loading.get(&spec).cloned() {
234233
// someone is already loading. Let's wait.
235234
std::mem::drop(loading);
236235
let (cv, m) = &*x;
@@ -332,9 +331,7 @@ impl PageArena {
332331
let mut cache = self.cache.write().unwrap();
333332

334333
let page = cache.pop(&spec);
335-
if page.is_none() {
336-
return None;
337-
}
334+
page.as_ref()?;
338335
let page = page.unwrap();
339336
let handle = Arc::new(PageHandle {
340337
spec,
@@ -453,7 +450,7 @@ impl PageHandle {
453450
// returns the pagehandle as an arc, and the page doesn't get
454451
// moved out of the loaded pages unless this arc's refcount is
455452
// 0.
456-
unsafe { &*(self.p as *const Embedding).offset(index as isize) }
453+
unsafe { &*(self.p as *const Embedding).add(index) }
457454
}
458455

459456
pub fn get_loaded_vec(self: &Arc<Self>, index: usize) -> LoadedVec {
@@ -464,7 +461,7 @@ impl PageHandle {
464461
);
465462
}
466463

467-
let vec = unsafe { (self.p as *const Embedding).offset(index as isize) };
464+
let vec = unsafe { (self.p as *const Embedding).add(index) };
468465
LoadedVec {
469466
page: self.clone(),
470467
vec,

0 commit comments

Comments
 (0)