Objects that contain pointers that point to the same object are not supported by Nim's model. Otherwise swapped objects would end up in an inconsistent state.
Does this mean:
FWIW, this seems to work (valgrind reports no leaks at least):
type A = ref object
x: A
b: string
proc main() =
for i in 0 ..< 100:
var a = A()
a.x = a
a.b = "hello world"
a.b &= "test"
swap(a, a.x)
swap(a[], a.x[])
echo a.b
echo a.x.b
GC_fullCollect()
main()
It is option (3). What I tried to say is that this does not work:
type
SSO = object
len: int
p: ptr UncheckedArray[char] # points to payload or into the heap
payload: array[24, char]
The reason for that is that the SSO objects are not always made aware of object movements as the compiler assumes a bitcopy + wasMoved is sufficient.
btw clang's libc++ does not use self-references either https://tc-imba.github.io/posts/cpp-sso/, instead they have something akin to this translation:
template isLong(s): bool = (s.cap and strLongFlag) == strLongFlag
Which of course tanks the performance...
Which of course tanks the performance...
Maybe, or maybe they decided on this design so that their strings become memcpy'able.