Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shallowcopy string doesn't work with arc/orc #20015

Closed
ringabout opened this issue Jul 13, 2022 · 3 comments · Fixed by #20025
Closed

shallowcopy string doesn't work with arc/orc #20015

ringabout opened this issue Jul 13, 2022 · 3 comments · Fixed by #20025
Labels
ARC/ORC Memory Management Documentation Content Related to documentation content (not generation).

Comments

@ringabout
Copy link
Member

ringabout commented Jul 13, 2022

type RotatedString* = object
  underlying: string
  shift: int

proc rotate*(s: var string, i: int): RotatedString =
  result = RotatedString(shift: i)
  shallowCopy(result.underlying, s)
  # prepareMutation(result.underlying)

proc `[]=`*(r: var RotatedString, i: int, c: char) {.inline.} =
  # when defined(gcArc) or defined(gcOrc):
  #   prepareMutation(r.underlying)
  let L = r.underlying.len
  assert 0 <= i and i < L
  let s = i + r.shift
  if s < L:
    r.underlying[s] = c
  else:
    r.underlying[s - L] = c


var
  x = "Hello, world"
  y = x.rotate(5)

y[0] = 'f'
doAssert x[5] == 'f'

ref #19972

@Araq
Copy link
Member

Araq commented Jul 13, 2022

shallowCopy does a real copy with ARC/ORC. This needs to be documented.

@ringabout ringabout added the Documentation Content Related to documentation content (not generation). label Jul 13, 2022
@ringabout
Copy link
Member Author

ringabout commented Jul 13, 2022

Is there some alternatives or workarounds? It makes https://github.com/andreaferretti/cello fail.

@Araq
Copy link
Member

Araq commented Jul 13, 2022

You can move the strings around in order to avoid copies. But in general the point of v2 is to allow for these breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ARC/ORC Memory Management Documentation Content Related to documentation content (not generation).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants