For those migrating from base to core library: Buffer won’t be included in core, so I’ve forked it into a standalone package to avoid forcing base library dependency. You can still reference the base library version, but I thought it would be nice to have it standalone and potentially be updated in the future
New package: xtended-collections on MOPS
Xtended-Collections - DynamicArray
import DynamicArray "mo:xtended-collection/DynamicArray";
let dynamicArray = DynamicArray.DynamicArray<Nat>(3);
// Add elements
dynamicArray.add(1);
dynamicArray.add(2);
dynamicArray.add(3);
let array = DynamicArray.toArray(dynamicArray);
Key changes from mo:base/Buffer:
- Module renamed to
DynamicArray(more accurate description of what it actually is) - New
buffer()instance method exposes generic buffer interface from the new buffer library (see below)
New Buffer, as an interface
I created a new library buffer on MOPS that is a simple write-only interface for appending data:
public type Buffer<T> = {
write : (T) -> ();
};
Initially, all my encoding libraries used Buffer.Buffer<T> as the standard. With that going away, I rethought the approach and created something generic like Iter.Iter<T> that works across different collection types.
This lets users choose their preferred implementation - whether DynamicArray, List.List<T>, [T], or custom types.
Usage Examples
With DynamicArray:
let dynamicArray = DynamicArray.DynamicArray<Nat8>(3);
Cbor.toBytesBuffer(dynamicArray.buffer(), cborValue);
let cborBytes = DynamicArray.toArray(dynamicArray);
With List:
let list = List.empty<Nat8>();
Cbor.toBytesBuffer(Buffer.fromList(list), cborValue);
let cborBytes = List.toArray(list);
My hope is that this can be useful to others as a simple interface that can be used, but im open to thoughts