motoko-base
motoko-base copied to clipboard
Fix: Support self application of `Buffer.append`
Appending a buffer with itself causes divergence, which may surprise or disappoint some developers.
Possible fixes:
- Keep same behavior (diverge)
- Check for this case and trap, failing fast, with an assertion failure within
Buffer.append
- Iterators keep their own state about when to stop, copying it from the
Buffer
's internal variables (same outcome as first cloning arg before using it for appending, but more efficient).
This PR uses solution 3.
Rationale: The current "problem" is that there's a read-write dependency between the buffer's size when appearing as an argument, and when (also) appearing as the receiver of the append
invocation. To permit this call with the expected functional behavior, we just need to break this dependency (copying the number of buffer elements to read before increasing this variable). This PR does that with a minor remedy to the code.