copyTo method

Object copyTo(
  1. Object dst
)

Copies this tensor's data into dst.

When dst is typed data matching the tensor's element type (e.g. Float32List for a float32 tensor), the bytes are bulk-copied directly and dst itself is returned; no per-element conversion or nested-list allocation.

Implementation

Object copyTo(Object dst) {
  int size = tfliteBinding.TfLiteTensorByteSize(_tensor);
  final data = cast<Uint8>(tfliteBinding.TfLiteTensorData(_tensor));
  checkState(isNotNull(data), message: 'Tensor data is null.');
  // View over native memory: valid only until the next invoke/allocate.
  // Every branch below copies out of it before returning.
  final src = data.asTypedList(size);

  final tensorType = type;
  if (dst is Uint8List) {
    if (dst.length != size) {
      throw ArgumentError(
        'Output object shape mismatch: tensor has $size bytes but the '
        'provided Uint8List has ${dst.length}.',
      );
    }
    dst.setAll(0, src);
    return dst;
  }
  if (dst is ByteBuffer) {
    final view = dst.asUint8List();
    view.setRange(0, view.length, src);
    return dst;
  }
  final typedDst = _copyToTypedData(dst, src, tensorType, size);
  if (typedDst != null) {
    return typedDst;
  }

  final obj = _convertBytesToObject(src);
  if (obj is List && dst is List) {
    list_utils.duplicateList(obj, dst);
  }
  return obj;
}