Borrow typed heap payloads with Value::get_as

Merged
JA
9d ago
+571-808

Summary

value.get_as::<T>(heap) borrows a payload directly from the heap. For example, list indexing can use if let Some(slice) = key.get_as::<Slice>(vm.heap) to read the slice bounds. The result is tied to the heap borrow, and the lookup uses the existing Heap::get without creating a reader guard.

This extracts the borrowed accessor from #1024 on top of #1421. The payload registry supplies the match for inline and boxed payloads. With the current untagged references, immediate values return None before a heap lookup, and heap references need one lookup to check their payload type. The caller migrations cover single-type reads. Multi-type dispatch can continue matching several variants on a single heap lookup.

Tests

Added a unit test that borrows bytes stored inline in a heap slot and a boxed named tuple. It also checks a mismatched heap payload and an immediate value. Ran the Monty tests and interpreter fixtures with memory-model-checks, the workspace Rust lints, formatting, and the 32-bit WASI build check.


Summary by cubic

Adds Value::get_as::<T>(heap) to borrow a typed heap payload directly, while Value absorbs borrowed accessors for bytes, strings, and long ints so call sites no longer match Value::Ref against HeapData variants by hand.

  • get_as reuses the existing Heap::get lookup via the payload registry's new from_heap_data, checking the payload type for heap references and returning None for immediate values.
  • Value::as_bytes, as_str, as_long_int, and try_as_str (renamed from try_to_str) replace local helpers like value_as_bytes, str_of, buffer, and the is_long_int/long_int wrappers; codec inputs now borrow byte slices directly instead of copying through value_to_owned_bytes.
  • base16 and base32 decoders fold ASCII case and translate digits while decoding, so casefold=True accepts mixed-case input; altchars translate before strict padding checks, and invalid digits take precedence over odd-length errors.
  • Ascii85 decoding with a custom ignorechars now charges the session for the input copy it makes during decoding.
  • base64 encoders now raise the standard bytes-like TypeError; CPython prefixes that message with memoryview: for b32encode, b32hexencode, b85encode, z85encode and a85encode.
  • Unit tests cover inline and boxed payloads, mismatched heap payloads, immediate values, and interned versus heap-allocated bytes.