MCPcopy Index your code
hub / github.com/RustPython/RustPython / with_slice

Method with_slice

crates/vm/src/sliceable.rs:351–367  ·  view source on GitHub ↗

Equivalent to PySlice_Unpack.

(slice: &PySlice, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

349impl SaturatedSlice {
350 // Equivalent to PySlice_Unpack.
351 pub fn with_slice(slice: &PySlice, vm: &VirtualMachine) -> PyResult<Self> {
352 let step = to_isize_index(vm, slice.step_ref(vm))?.unwrap_or(1);
353 if step == 0 {
354 return Err(vm.new_value_error("slice step cannot be zero"));
355 }
356 let start = to_isize_index(vm, slice.start_ref(vm))?
357 .unwrap_or_else(|| if step.is_negative() { isize::MAX } else { 0 });
358
359 let stop = to_isize_index(vm, &slice.stop(vm))?.unwrap_or_else(|| {
360 if step.is_negative() {
361 isize::MIN
362 } else {
363 isize::MAX
364 }
365 });
366 Ok(Self { start, stop, step })
367 }
368
369 // Equivalent to PySlice_AdjustIndices
370 /// Convert for usage in indexing the underlying rust collections. Called *after*

Callers

nothing calls this directly

Calls 5

to_isize_indexFunction · 0.85
step_refMethod · 0.80
start_refMethod · 0.80
ErrClass · 0.50
stopMethod · 0.45

Tested by

no test coverage detected