motoko-base icon indicating copy to clipboard operation
motoko-base copied to clipboard

Fix: Support self application of `Buffer.append`

Open matthewhammer opened this issue 4 years ago • 0 comments

Appending a buffer with itself causes divergence, which may surprise or disappoint some developers.

Possible fixes:

  1. Keep same behavior (diverge)
  2. Check for this case and trap, failing fast, with an assertion failure within Buffer.append
  3. 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.

matthewhammer avatar Aug 19 '20 17:08 matthewhammer