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

proc addBytes[T](buf: seq[uint8], a: T) #722

Open
timotheecour opened this issue May 7, 2021 · 3 comments
Open

proc addBytes[T](buf: seq[uint8], a: T) #722

timotheecour opened this issue May 7, 2021 · 3 comments

Comments

@timotheecour
Copy link
Owner

timotheecour commented May 7, 2021

when true:
  import std/[sugar,locks]
  proc addBytes[T](buf: var seq[uint8], a: T) =
    let n = T.sizeof
    let old = buf.len
    buf.setLen old + n
    copyMem(buf[old].addr, a.unsafeAddr, n)

  var a: Lock
  assert a.sizeof == 64
  initLock(a)

  echo seq[uint8].default.dup(addBytes(a)) # @[90, 84, 85, 77, 0...]

originally proposed in nim-lang#17944 (comment) (but changed its semantics to be additive, which is strictly more flexible)

note

maybe add to std/reflection nim-lang#17641

@juancarlospaco
Copy link
Collaborator

buf: var openArray[uint8] ?. 🤔

@timotheecour
Copy link
Owner Author

can't work with setLen

@juancarlospaco
Copy link
Collaborator

Iam wrong, you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants