shape-runtime 0.3.1

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// @module std::core::len
/// `Len` — uniform length interface for collection-like types.
///
/// Any type that exposes a zero-argument `len()` method returning `int`
/// satisfies the structural contract of `Len`. Dispatch today goes through
/// per-type PHF method tables (see `shape-runtime::method_registry`), so the
/// canonical call form is `value.len()`.
///
/// This module carries the trait definition plus explicit implementations for
/// built-in length-bearing types. Future work: auto-derive `impl Len for T`
/// for any `T` whose method registry contains an exact-signature
/// `len() -> int`, enabling generic bounds `fn f<T: Len>(x: T) -> int`
/// without hand-written impls.
///
/// ## Known Implementations
///
/// | Type           | Dispatch source                              |
/// |----------------|----------------------------------------------|
/// | Array<T>       | built-in PHF (array_basic, array_ops)        |
/// | String         | built-in PHF (string_methods)                |
/// | HashMap<K,V>   | built-in PHF (hashmap_methods)               |
/// | Set<T>         | std/core/set.shape                           |
/// | TypedArray<T>  | built-in PHF (typed_access.rs)               |

/// Uniform length interface for collection-like types.
///
/// Every length-bearing built-in satisfies this contract via method dispatch
/// on `.len()`. The declared signature intentionally returns `int` (not
/// `number`) to match the zero-tag typed-opcode path (`ArrayLenTyped`,
/// `MapLenTyped`).
///
/// `isEmpty()` is a defaulted method whose body returns `self.len() == 0`.
/// Built-in types whose method registry already exposes a faster
/// `isEmpty` handler (`array_basic::handle_len_v2` is_empty shim,
/// `hashmap_methods::v2_is_empty`, Set/Deque/PriorityQueue/TypedArray/
/// Range `v2_is_empty` PHF entries, `string_methods::v2_string_is_empty`)
/// bypass the default at dispatch time. User-defined types that
/// implement `impl Len for T` inherit the default body without
/// authoring their own `isEmpty`.
trait Len {
    /// Return the number of elements (or bytes, for strings).
    method len() -> int;

    /// Return `true` when the receiver has zero elements. Default body
    /// delegates to `self.len() == 0`; types with a faster path (e.g.
    /// HashMap, Range) override via their PHF method registry, which
    /// takes precedence over this default at dispatch time.
    method isEmpty() -> bool {
        self.len() == 0
    }
}