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

Method setstate

crates/stdlib/src/random.rs:123–142  ·  view source on GitHub ↗
(&self, state: PyTupleRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

121
122 #[pymethod]
123 fn setstate(&self, state: PyTupleRef, vm: &VirtualMachine) -> PyResult<()> {
124 let state: &[_; mt19937::N + 1] = state
125 .as_slice()
126 .try_into()
127 .map_err(|_| vm.new_value_error("state vector is the wrong size"))?;
128 let (index, state) = state.split_last().unwrap();
129 let index: usize = index.try_to_value(vm)?;
130 if index > mt19937::N {
131 return Err(vm.new_value_error("invalid state"));
132 }
133 let state: [u32; mt19937::N] = state
134 .iter()
135 .map(|i| i.try_to_value(vm))
136 .process_results(|it| it.collect_array())?
137 .unwrap();
138 let mut rng = self.rng.lock();
139 rng.set_state(&state);
140 rng.set_index(index);
141 Ok(())
142 }
143 }
144}

Callers

nothing calls this directly

Calls 8

try_to_valueMethod · 0.80
set_stateMethod · 0.80
ErrClass · 0.50
as_sliceMethod · 0.45
unwrapMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
lockMethod · 0.45

Tested by

no test coverage detected