| 272 | |
| 273 | #[pymethod] |
| 274 | fn __reversed__(&self, vm: &VirtualMachine) -> PyResult { |
| 275 | let start = self.start.as_bigint(); |
| 276 | let step = self.step.as_bigint(); |
| 277 | |
| 278 | // Use CPython calculation for this: |
| 279 | let length = self.__len__(); |
| 280 | let new_stop = start - step; |
| 281 | let start = &new_stop + length.clone() * step; |
| 282 | let step = -step; |
| 283 | |
| 284 | Ok( |
| 285 | if let (Some(start), Some(step), Some(_)) = |
| 286 | (start.to_isize(), step.to_isize(), new_stop.to_isize()) |
| 287 | { |
| 288 | PyRangeIterator { |
| 289 | index: AtomicCell::new(0), |
| 290 | start, |
| 291 | step, |
| 292 | // Cannot fail. If start, stop and step all successfully convert to isize, then result of zelf.len will |
| 293 | // always fit in a usize. |
| 294 | length: length.to_usize().unwrap_or(0), |
| 295 | } |
| 296 | .into_pyobject(vm) |
| 297 | } else { |
| 298 | PyLongRangeIterator { |
| 299 | index: AtomicCell::new(0), |
| 300 | start, |
| 301 | step, |
| 302 | length, |
| 303 | } |
| 304 | .into_pyobject(vm) |
| 305 | }, |
| 306 | ) |
| 307 | } |
| 308 | |
| 309 | fn __len__(&self) -> BigInt { |
| 310 | self.compute_length() |