(obj: Borrowed<'_, 'py, PyAny>)
| 68 | } |
| 69 | |
| 70 | fn create_array_from_obj<'py, T, const N: usize>(obj: Borrowed<'_, 'py, PyAny>) -> PyResult<[T; N]> |
| 71 | where |
| 72 | T: FromPyObjectOwned<'py>, |
| 73 | { |
| 74 | // Types that pass `PySequence_Check` usually implement enough of the sequence protocol |
| 75 | // to support this function and if not, we will only fail extraction safely. |
| 76 | // SAFETY: passing valid pointer to python API |
| 77 | if unsafe { ffi::PySequence_Check(obj.as_ptr()) } == 0 { |
| 78 | return Err(CastError::new(obj, PySequence::type_object(obj.py()).into_any()).into()); |
| 79 | } |
| 80 | |
| 81 | let seq_len = obj.len()?; |
| 82 | if seq_len != N { |
| 83 | return Err(invalid_sequence_length(N, seq_len)); |
| 84 | } |
| 85 | array_try_from_fn(|idx| { |
| 86 | obj.get_item(idx) |
| 87 | .and_then(|any| any.extract().map_err(Into::into)) |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | // TODO: use `core::array::try_from_fn`, if that stabilises: |
| 92 | // https://github.com/rust-lang/rust/issues/89379 |
no test coverage detected