Build an IterCursor so short-circuit builtins (e.g. `all(range(10**6))`) stop at the first hit instead of pre-materialising. TypeError on non-iterables. */
(&self, o: Val)
| 289 | |
| 290 | /* Build an IterCursor so short-circuit builtins (e.g. `all(range(10**6))`) stop at the first hit instead of pre-materialising. TypeError on non-iterables. */ |
| 291 | pub(in crate::modules::vm) fn iter_cursor(&self, o: Val) -> Result<IterCursor, VmErr> { |
| 292 | if !o.is_heap() { |
| 293 | return Err(self.not_iterable(o)); |
| 294 | } |
| 295 | Ok(match self.heap.get(o) { |
| 296 | HeapObj::Range(s, e, st) => IterCursor::Range { cur: *s, end: *e, step: *st }, |
| 297 | HeapObj::Bytes(b) => IterCursor::Bytes { bytes: b.clone(), idx: 0 }, // Vec<u8> clone |
| 298 | HeapObj::Str(s) => IterCursor::StrChars { chars: s.chars().collect(), idx: 0 }, |
| 299 | HeapObj::List(v) => IterCursor::Vec { items: v.borrow().clone(), idx: 0 }, |
| 300 | HeapObj::Tuple(v) => IterCursor::Vec { items: v.clone(), idx: 0 }, |
| 301 | HeapObj::Set(v) => IterCursor::Vec { items: v.borrow().iter().cloned().collect(), idx: 0 }, |
| 302 | HeapObj::FrozenSet(v) => IterCursor::Vec { items: v.iter().cloned().collect(), idx: 0 }, |
| 303 | HeapObj::Dict(d) => IterCursor::Vec { items: d.borrow().keys().collect(), idx: 0 }, |
| 304 | _ => return Err(self.not_iterable(o)), |
| 305 | }) |
| 306 | } |
| 307 | |
| 308 | /* Vec<Val> from any iterable (dict yields keys, str yields one-char strs, bytes yields ints). `include_range = false` lets callers reject Range. */ |
| 309 | pub(in crate::modules::vm) fn extract_iter(&mut self, o: Val, include_range: bool) -> Result<Vec<Val>, VmErr> { |