Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
deliro committed Dec 10, 2024
1 parent 4e0d03b commit 5e1c2fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "moka-py"
version = "0.1.12"
version = "0.1.13"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -11,7 +11,7 @@ crate-type = ["cdylib"]
[dependencies]
ahash = "0.8.11"
moka = { version = "0.12.8", features = ["sync"] }
pyo3 = "0.23.2"
pyo3 = "0.23.3"

[profile.release]
lto = true
Expand Down
21 changes: 10 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl AnyKey {
let kind = match obj.downcast_bound::<PyString>(py) {
Ok(s) if s.len()? <= Self::SHORT_STR => KeyKind::ShortStr(s.to_string()),
_ => {
let py_hash = obj.to_object(py).into_bound(py).hash()?;
let py_hash = obj.bind_borrowed(py).hash()?;
KeyKind::Other { py_hash }
}
};
Expand Down Expand Up @@ -78,8 +78,8 @@ impl PartialEq for AnyKey {
) => {
*lhs_hash == *rhs_hash
&& Python::with_gil(|py| {
let lhs = lhs_obj.to_object(py).into_bound(py);
let rhs = rhs_obj.to_object(py).into_bound(py);
let lhs = lhs_obj.bind_borrowed(py);
let rhs = rhs_obj.bind_borrowed(py);
match lhs.rich_compare(rhs, CompareOp::Eq) {
Ok(v) => v.is_truthy().unwrap_or_default(),
Err(_) => false,
Expand Down Expand Up @@ -113,7 +113,7 @@ fn cause_to_str(cause: RemovalCause) -> &'static str {
}

#[pyclass]
struct Moka(Arc<Cache<AnyKey, Arc<PyObject>, ahash::RandomState>>);
struct Moka(Cache<AnyKey, Arc<PyObject>, ahash::RandomState>);

#[pymethods]
impl Moka {
Expand Down Expand Up @@ -156,9 +156,9 @@ impl Moka {
builder = builder.eviction_listener(Box::new(listen_fn));
}

Ok(Moka(Arc::new(
Ok(Moka(
builder.build_with_hasher(ahash::RandomState::default()),
)))
))
}

#[classmethod]
Expand All @@ -171,7 +171,7 @@ impl Moka {

fn set(&self, py: Python, key: PyObject, value: PyObject) -> PyResult<()> {
let hashable_key = AnyKey::new_with_gil(key, py)?;
let value = Arc::new(value.clone_ref(py));
let value = Arc::new(value);
py.allow_threads(|| self.0.insert(hashable_key, value));
Ok(())
}
Expand All @@ -185,10 +185,9 @@ impl Moka {
) -> PyResult<Option<PyObject>> {
let hashable_key = AnyKey::new_with_gil(key, py)?;
let value = py.allow_threads(|| self.0.get(&hashable_key));
Ok(match value.map(|obj| obj.clone_ref(py)) {
None => default.map(|v| v.clone_ref(py)),
Some(v) => Some(v),
})
Ok(value
.map(|v| v.clone_ref(py))
.or_else(|| default.map(|v| v.clone_ref(py))))
}

fn get_with(&self, py: Python, key: PyObject, initializer: PyObject) -> PyResult<PyObject> {
Expand Down

0 comments on commit 5e1c2fa

Please sign in to comment.