Add PyItem container
Also reduce queue item trait bound from Ord to just PartialOrd
This commit is contained in:
parent
ee004bac19
commit
38a544db76
8 changed files with 91 additions and 32 deletions
46
src/backing/containers/py_item.rs
Normal file
46
src/backing/containers/py_item.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use std::cmp::Ordering;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Container that provides PartialOrd based on the Python object it holds
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PyItem(Py<PyAny>);
|
||||
|
||||
impl PyItem {
|
||||
/// Creates a new instance
|
||||
fn new(item: Py<PyAny>) -> Self {
|
||||
PyItem(item)
|
||||
}
|
||||
|
||||
/// Retrieves the internal data
|
||||
fn item(self) -> Py<PyAny> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for PyItem {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Python::with_gil(|py| {
|
||||
// Bind objects for convenience
|
||||
let ours = self.0.bind(py);
|
||||
let theirs = other.0.bind(py);
|
||||
|
||||
// Compare based on Python comparison implementations
|
||||
match ours.lt(theirs) {
|
||||
Ok(true) => Some(Ordering::Less),
|
||||
Ok(false) => match ours.gt(theirs) {
|
||||
Ok(true) => Some(Ordering::Greater),
|
||||
Ok(false) => Some(Ordering::Equal), // If the python class is implemented strangely then this may be wrong
|
||||
Err(_) => None,
|
||||
},
|
||||
Err(_) => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for PyItem {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Python::with_gil(|py| self.0.bind(py).eq(other.0.bind(py)).unwrap_or(false))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue