asListView<TNum extends num> method

List<TNum> asListView<TNum extends num>()

Returns a dart list view on the pointer.

The resulting list operates on the same memory. This means if you modify elements of the list, the data the pointer points to changes as well. You can either get a reference to the whole pointer, or use viewAt to select a specific portion of the pointer. All returned lists are guaranteed to also implement the TypedData interface.

Note: As the returned list is a reference, calling SodiumPointer.dispose is not allowed as long as you still use the returned list. If you still dispose of the pointer, any try to access the data will crash your application.

Implementation

List<TNum> asListView<TNum extends num>() {
  final signage = _StaticallyTypedSizeOf.signage<T>();
  switch (signage) {
    case _Signage.signed:
      if (elementSize <= sizeOf<Int8>()) {
        return ptr.cast<Int8>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Int16>()) {
        return ptr.cast<Int16>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Int32>()) {
        return ptr.cast<Int32>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Int64>()) {
        return ptr.cast<Int64>().asTypedList(count) as List<TNum>;
      }
    case _Signage.unsigned:
      if (elementSize <= sizeOf<Uint8>()) {
        return ptr.cast<Uint8>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Uint16>()) {
        return ptr.cast<Uint16>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Uint32>()) {
        return ptr.cast<Uint32>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Uint64>()) {
        return ptr.cast<Uint64>().asTypedList(count) as List<TNum>;
      }
    case _Signage.float:
      if (elementSize <= sizeOf<Float>()) {
        return ptr.cast<Float>().asTypedList(count) as List<TNum>;
      } else if (elementSize <= sizeOf<Double>()) {
        return ptr.cast<Double>().asTypedList(count) as List<TNum>;
      }
  }

  // coverage:ignore-start
  throw UnsupportedError(
    'Cannot create a list view for a pointer of type $T',
  );
  // coverage:ignore-end
}