Skip to content

Commit 07bedef

Browse files
committed
⚡️ Improve iteration in type classes
1 parent efd3ff2 commit 07bedef

File tree

1 file changed

+9
-15
lines changed
  • packages/agent_dart_base/lib/candid

1 file changed

+9
-15
lines changed

packages/agent_dart_base/lib/candid/idl.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,7 @@ class TupleClass<T extends List> extends ConstructType<List> {
10541054
// `>=` because tuples can be covariant when encoded.
10551055
return x.length >= _fields.length &&
10561056
_components
1057-
.asMap()
1058-
.entries
1059-
.map((t) => t.value.covariant(x[t.key]) ? 0 : 1)
1057+
.mapIndexed((index, value) => value.covariant(x[index]) ? 0 : 1)
10601058
.reduce((value, element) => value + element) ==
10611059
0;
10621060
}
@@ -1103,10 +1101,7 @@ class TupleClass<T extends List> extends ConstructType<List> {
11031101
);
11041102
}
11051103
final res = [];
1106-
for (final entry in tuple._components.asMap().entries) {
1107-
// [i, wireType]
1108-
final i = entry.key;
1109-
final wireType = entry.value;
1104+
for (final (i, wireType) in tuple._components.indexed) {
11101105
if (i >= _components.length) {
11111106
// skip value
11121107
wireType.decodeValue(x, wireType);
@@ -1406,7 +1401,7 @@ class FuncClass extends ConstructType<List> {
14061401
'Arity mismatch',
14071402
);
14081403
}
1409-
return '(${types.asMap().entries.map((e) => e.value.valueToString(v[e.key])).join(', ')})';
1404+
return '(${types.mapIndexed((i, e) => e.valueToString(v[i])).join(', ')})';
14101405
}
14111406

14121407
@override
@@ -1866,22 +1861,21 @@ List idlDecode(List<CType> retTypes, Uint8List bytes) {
18661861
}
18671862
}
18681863

1869-
rawTable.asMap().forEach((i, entry) {
1870-
final t = buildType(entry);
1864+
for (final (i, e) in rawTable.indexed) {
1865+
final t = buildType(e);
18711866
table[i].fill(t);
1872-
});
1867+
}
18731868

18741869
final types = rawTypes.map((t) => getType(t)).toList();
18751870

1876-
final output = retTypes.asMap().entries.map((entry) {
1877-
final result = entry.value.decodeValue(b, types[entry.key]);
1878-
return result;
1879-
}).toList();
1871+
final output =
1872+
retTypes.mapIndexed((i, e) => e.decodeValue(b, types[i])).toList();
18801873

18811874
// Skip unused values.
18821875
for (int ind = retTypes.length; ind < types.length; ind++) {
18831876
types[ind].decodeValue(b, types[ind]);
18841877
}
1878+
18851879
if (b.buffer.isNotEmpty) {
18861880
throw StateError('Unexpected left-over bytes.');
18871881
}

0 commit comments

Comments
 (0)