zero_mut

Zero-Overhead, Compile-Time Immutable Collections, Zero-Copy Binary Slices, Lazy Sequences, and Reactive State Security for Dart.

Pub Version License: MIT

zero_mut provides a complete ecosystem for zero-allocation data structures and compile-time immutability across 5 architectural pillars.


1. Core Immutable Collections (Zero-Overhead)

ZeroSlice (Slice<E>) & ZeroList (ImmList<E>)

import 'package:zero_mut/zero_mut.dart';

void main() {
  final numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];

  // Zero-allocation slice view [start, end)
  final slice = numbers.slice(2, 7); // [30, 40, 50, 60, 70]
  print(slice[0]); // 30 (O(1) access)
  print(slice.length); // 5

  // Sub-slices, take, and skip are also O(1) with 0 memory copies
  final subSlice = slice.slice(1, 4); // [40, 50, 60]
  final firstTwo = slice.take(2); // [30, 40]
  final skipped = slice.skip(2); // [50, 60, 70]

  // Immutable full list wrapper
  final immList = numbers.asImm();
  final evolved = immList.withAdded(110); // Non-destructive copy-on-write

  // COMPILE ERROR in editor:
  // slice[0] = 99; // Compilation error!
  // slice.add(99); // Compilation error!
  // immList.clear(); // Compilation error!
}

ZeroMap (ImmMap<K, V>) & ZeroSet (ImmSet<E>)

import 'package:zero_mut/zero_mut.dart';

void main() {
  final map = {'theme': 'dark', 'version': '1.0'}.asImm();
  final updatedMap = map.withPut('debug', 'false');

  final set = {1, 2, 3}.asImm();
  final unionSet = set.union({3, 4, 5});

  // COMPILE ERROR in editor:
  // map['theme'] = 'light'; // Compilation error!
  // set.add(10); // Compilation error!
}

2. Low-Level Zero-Copy Binary Data (ZeroByteSlice & ZeroTypedList)

Safe, zero-copy buffer views over binary memory:

import 'dart:typed_data';
import 'package:zero_mut/zero_mut.dart';

void main() {
  final buffer = Uint8List(1024);
  final byteSlice = buffer.byteSlice(16, 64);

  // Endian-aware primitive decoding in O(1)
  final int16Val = byteSlice.getInt16(0, Endian.big);
  final uint32Val = byteSlice.getUint32(2, Endian.big);
  final float64Val = byteSlice.getFloat64(6, Endian.little);

  // Nested sub-slicing without memory duplication
  final subPacket = byteSlice.slice(4, 32);

  // COMPILE ERROR in editor:
  // byteSlice[0] = 0xFF; // Compilation error!
  // byteSlice.setUint8(0, 0xFF); // Compilation error!
}

3. Lazy Sequences & Transform Views (ZeroMappedList & ZeroFilterView)

On-demand element transformations and filtering with $O(1)$ indexed access:

import 'package:zero_mut/zero_mut.dart';

void main() {
  final numbers = List.generate(1000000, (i) => i);

  // Elements are transformed only when accessed by index (0 intermediate memory)
  final mapped = numbers.mapLazy((x) => 'item_$x');
  print(mapped[500]); // "item_500" (computed on-demand in O(1))

  // Lazy filtered view
  final filtered = numbers.filterLazy((x) => x.isEven);
}

4. Reactive State Security (ZeroNotifier & ZeroStream)

Guarantees unidirectional data flow by preventing consumers from altering state:

import 'dart:async';
import 'package:zero_mut/zero_mut.dart';

class CounterState {
  final _notifier = ZeroStateNotifier<int>(0);

  // Expose only read-only facade
  ZeroNotifier<int> get count => _notifier.asReadOnly;

  void increment() => _notifier.value++;
}

void main() {
  final state = CounterState();
  state.count.addListener(() => print('Count: ${state.count.value}'));

  // Read-only stream facade
  final controller = StreamController<String>();
  final readOnlyStream = ZeroStream.fromController(controller);

  // COMPILE ERROR in editor:
  // state.count.setValue(10); // Compilation error!
  // readOnlyStream.add('hacked'); // Compilation error!
}

5. Domain-Specific Data Structures (ZeroMatrix & ZeroTree)

ZeroMatrix (2D Views over Flat Continuous Memory)

import 'package:zero_mut/zero_mut.dart';

void main() {
  final flatBuffer = [
    1, 2, 3, 4,
    5, 6, 7, 8,
    9, 10, 11, 12,
  ];

  final matrix = ZeroMatrix<int>(flatBuffer, 3, 4);
  print(matrix.get(1, 2)); // 7 (O(1) cell access)

  // O(1) row slice without copying flat memory
  final row1 = matrix.row(1); // [5, 6, 7, 8]

  // O(1) sub-matrix window
  final sub = matrix.subMatrix(1, 3, 1, 3); // 2x2 submatrix
}

ZeroTree & ZeroNode (Immutable Tree Hierarchies)

import 'package:zero_mut/zero_mut.dart';

void main() {
  final tree = ZeroTree<String>(
    ZeroNode(
      'Root',
      ZeroList([
        ZeroNode('ChildA', ZeroList([ZeroNode('Leaf1')])),
        ZeroNode('ChildB'),
      ]),
    ),
  );

  for (final node in tree.depthFirst) {
    print(node.value);
  }
}

Author & Maintainer

Created and maintained by josephinoo.dev.


Benchmarks

Operation Standard Dart zero_mut Speedup / Memory
Slicing 10k windows ~7.9 ms (Heap copies) ~0.9 ms ~8.5x faster
Memory Allocation $O(N)$ new array buffer $0$ bytes extra Zero heap churn
Mutation Detection Runtime exception (or silent bug) Compile-time Error Instant IDE feedback

License

MIT License. See LICENSE for details.

Libraries

zero_mut
zero_mut provides zero-overhead compile-time immutable collections, zero-copy binary slicing, lazy sequences, reactive state security, and specialized structures.