Skip to content

Commit c9a5f7a

Browse files
committed
Work around a platform-specific error/lint
1 parent 13cda62 commit c9a5f7a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

flatgfa-py/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl ListRef {
169169
{
170170
match arg {
171171
SliceOrInt::Slice(slice) => {
172-
let indices = slice.indices(self.len().into())?;
172+
let indices = py_slice_indices(slice, self.len())?;
173173
if indices.step == 1 {
174174
Ok(L::from(self.slice(indices.start as u32, indices.stop as u32)).into_py(py))
175175
} else {
@@ -525,6 +525,17 @@ impl PyHandle {
525525
}
526526
}
527527

528+
/// Get the components of a Python slice object.
529+
///
530+
/// This wraps an underlying PyO3 utility but supports a `usize` length.
531+
fn py_slice_indices(slice: &PySlice, len: u32) -> PyResult<pyo3::types::PySliceIndices> {
532+
// Depending on the size of a C `long`, this may or may not need a fallible
533+
// conversion. This is a workaround to avoid either errors or Clippy
534+
// warnings, depending on the platform.
535+
#[allow(clippy::unnecessary_fallible_conversions)]
536+
slice.indices(len.try_into().unwrap())
537+
}
538+
528539
/// A list of :class:`Handle` objects, such as a sequence of path steps.
529540
#[pyclass]
530541
#[pyo3(module = "flatgfa")]
@@ -547,7 +558,7 @@ impl StepList {
547558
fn __getitem__(&self, arg: SliceOrInt, py: Python) -> PyResult<PyObject> {
548559
match arg {
549560
SliceOrInt::Slice(slice) => {
550-
let indices = slice.indices(self.0.len().into())?;
561+
let indices = py_slice_indices(slice, self.0.len())?;
551562
if indices.step == 1 {
552563
let list = self.0.slice(indices.start as u32, indices.stop as u32);
553564
Ok(Self(list).into_py(py))

0 commit comments

Comments
 (0)