diff --git a/src/queue/paired.rs b/src/queue/paired.rs index 5d32074..84f743f 100644 --- a/src/queue/paired.rs +++ b/src/queue/paired.rs @@ -25,7 +25,11 @@ impl PairedQueue { #[pyo3(signature = (items=None))] fn new(items: Option>) -> PyResult { if let Some(py_object) = items { - Python::with_gil(|py| Self::new_from_any(py_object.bind(py))) + Python::with_gil(|py| Self::from_any(py_object.bind(py))).and_then(|vec| { + Ok(Self { + backing: Box::new(BinaryHeap::from_iter(vec)), + }) + }) } else { Ok(Self { backing: Box::new(BinaryHeap::new()), @@ -63,13 +67,13 @@ impl PairedQueue { } impl<'py> PairedQueue { - fn new_from_any(object: &Bound<'py, PyAny>) -> PyResult { + fn from_any(object: &Bound<'py, PyAny>) -> PyResult, f64>>> { if let Ok(vec) = object.extract::, f64)>>() { - Ok(Self::new_from_vec(vec)) + Ok(Self::from_vec(vec)) } else { if object.is_instance_of::() { if let Ok(dict) = object.downcast::() { - Self::new_from_map(dict) + Self::from_dict(dict) } else { Err(PyErr::new::( "Argument claimed to be a dict but wasn't", @@ -83,16 +87,13 @@ impl<'py> PairedQueue { } } - fn new_from_vec(list: Vec<(Py, f64)>) -> Self { - Self { - backing: Box::new(BinaryHeap::from_iter( - list.into_iter() - .map(|(data, priority)| Item::new(data, priority)), - )), - } + fn from_vec(list: Vec<(Py, f64)>) -> Vec, f64>> { + list.into_iter() + .map(|(data, priority)| Item::new(data, priority)) + .collect() } - fn new_from_map(dict: &Bound<'py, PyDict>) -> PyResult { + fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult, f64>>> { if let Ok(items) = dict .into_iter() .map(|(data, priority)| match priority.extract::() { @@ -101,9 +102,7 @@ impl<'py> PairedQueue { }) .collect::, _>>() { - Ok(Self { - backing: Box::new(BinaryHeap::from_iter(items)), - }) + Ok(items) } else { Err(PyErr::new::("Dict keys were not floats")) }