neo icon indicating copy to clipboard operation
neo copied to clipboard

Upstream is introducing newSeqUninit

Open ringabout opened this issue 2 years ago • 2 comments

ref https://github.com/nim-lang/Nim/pull/22739

ringabout avatar Sep 21 '23 14:09 ringabout

Hi @ringabout , thank you for your contribution, but there is an issue to fix before that: shallowCopy has been removed, and hence neo is currently non functional on Nim 2.

I am not sure what is the best way to proceed - I have little time to figure out how to make copies shallow on Nim 2, and until that is done, there is little point in making this change, which only affects Nim 2 anyway.

I am open to suggestions

andreaferretti avatar Sep 22 '23 15:09 andreaferretti

It breaks Nim CI, besides you can still use neo with Nim 2.0 in refc, which keeps shallowCopy.

You might be able to inspect the intenals of seqs, for instance

type
  NimSeqV2[T] = object
    len: int
    p: pointer

proc unsafeShallowCopy*[T](dest: var seq[T], src: seq[T]) =
  cast[ptr NimSeqV2[T]](addr dest).p = cast[ptr NimSeqV2[T]](addr src).p
  cast[ptr NimSeqV2[T]](addr dest).len = cast[ptr NimSeqV2[T]](addr src).len


proc foo =
  var x = @[1, 2, 3]
  var y: seq[int]
  unsafeShallowCopy(y, x)
  echo (x, y)

foo()

but you need to be cautious of the lifetime of x or set x.p to nil if needed.

ringabout avatar Sep 25 '23 14:09 ringabout