using System; using System.Collections; using System.Collections.Generic; namespace Capnp { /// /// ListDeserializer specialization for List(T) when T is a known struct (i.e. a list of fixed-width composites). /// public class ListOfStructsDeserializer: ListDeserializer, IReadOnlyList { internal ListOfStructsDeserializer(in DeserializerState context): base(context) { } /// /// Always returns ListKind.ListOfStructs. /// public override ListKind Kind => ListKind.ListOfStructs; /// /// Returns the deserializer state at given index. /// /// Element index /// Element deserializer state /// is out of range. /// Traversal limit reached public DeserializerState this[int index] { get { if (index < 0 || index >= Count) throw new IndexOutOfRangeException(); int stride = State.StructDataCount + State.StructPtrCount; var state = State; // the “traversal limit” should count a list of zero-sized elements as if each element were one word instead. state.IncrementBytesTraversed(checked(8u * (uint)(stride == 0 ? 1 : stride))); state.Offset = checked(state.Offset + 1 + index * stride); state.Kind = ObjectKind.Struct; return state; } } /// /// Converts this list to a different representation by applying an element selector function. /// /// Target type after applying the selector function /// The selector function /// The new list representation public override IReadOnlyList Cast(Func cons) { return this.LazyListSelect(cons); } IEnumerable Enumerate() { for (int i = 0; i < Count; i++) { yield return this[i]; } } /// /// Implements . /// public IEnumerator GetEnumerator() { return Enumerate().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }