Skip to main content

nanvm_lib/vm/impls/
sized_index.rs

1use core::ops::Index;
2
3use crate::{
4    common::sized_index::SizedIndex,
5    vm::{IContainer, IVm},
6};
7
8/// A wrapper newtype over a VM container — `String<A>`, `Array<A>`,
9/// `Object<A>`, `BigInt<A>` — and the container it wraps. Each wrapper
10/// implements it once, beside its `Index<u32>`, which delegates to
11/// [`item`](Self::item); `SizedIndex<u32>` follows from the blanket impl
12/// below.
13pub trait ContainerWrapper: Index<u32> {
14    type Vm: IVm;
15    type Internal: IContainer<Self::Vm, Item = Self::Output>;
16    fn internal(&self) -> &Self::Internal;
17    /// The item at `index`; panics if out of bounds.
18    fn item(&self, index: u32) -> &Self::Output {
19        self.internal().items().index(index as usize)
20    }
21}
22
23impl<W: ContainerWrapper> SizedIndex<u32> for W {
24    fn length(&self) -> u32 {
25        self.internal().items().length() as u32
26    }
27}