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

deprecates newSeqUninitialized replaced by unsafeseqs.newSeqUninit #22586

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

- Adds `newStringUninit` to system, which creates a new string of length `len` like `newString` but with uninitialized content.

- Adds a module `std/unsafeseqs`, which contains `newSeqUninit` and `newSeqUnsafe` functions that create a new sequence of length `len` like `newSeq` but with uninitialized content.

[//]: # "Deprecations:"

- Deprecates `system.newSeqUninitialized`, which is replaced by `unsafeseqs.newSeqUninit`.

[//]: # "Removals:"

Expand Down
63 changes: 63 additions & 0 deletions lib/std/unsafeseqs.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#
#
# Nim's Runtime Library
# (c) Copyright 2023 Nim contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#

## This module provides some high performance sequence operations.

import typetraits

when not defined(js):
template newSeqImpl(T, len) =
result = newSeqOfCap[T](len)
when defined(nimSeqsV2):
cast[ptr int](addr result)[] = len
else:
var s = cast[PGenericSeq](result)
s.len = len

proc newSeqUnsafe*[T](len: Natural): seq[T] =
## Creates a new sequence of type `seq[T]` with length `len`.
##
## Note that the sequence will be uninitialized.
## After the creation of the sequence you should assign
## entries to the sequence instead of adding them.
runnableExamples:
# newSeqUnsafe can be safely used for types, which don't contain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a primary use case - I think the documentation should generally recommend newSeqUninit and only fall back to newSeqUnsafe for cases that involve complex types - I also think it's prudent to clearly establish the meaning of "unsafe" in this context, otherwise it's just a vacuous word for unknown bounds which makes it difficult to reason about in usage. ie "how is this unsafe?" for example, it is not unsafe in the same way addr is unsafe.

# managed memory or have destructors
var x = newSeqUnsafe[int](3)
assert len(x) == 3
x[0] = 10

# be cautious to use it with types with managed memory or destructors
# and `{.nodestroy.}` can be handy in these cases
proc initStringSeq(x: Natural): seq[string] {.nodestroy.} =
result = newSeqUnsafe[string](x)
for i in 0..<x: result[i] = "abc"

let s = initStringSeq(10)
assert len(s) == 10
assert s[0] == "abc"
newSeqImpl(T, len)

proc newSeqUninit*[T](len: Natural): seq[T] =
## Creates a new sequence of type `seq[T]` with length `len`.
##
## Only available for types, which don't contain
## managed memory or have destructors.
## Note that the sequence will be uninitialized.
## After the creation of the sequence you should assign
## entries to the sequence instead of adding them.
runnableExamples:
var x = newSeqUninit[int](3)
assert len(x) == 3
x[0] = 10

when supportsCopyMem(T):
newSeqImpl(T, len)
else:
{.error: "The type T cannot contain managed memory or have destructors".}
5 changes: 3 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ proc newSeq*[T](len = 0.Natural): seq[T] =
##
## See also:
## * `newSeqOfCap <#newSeqOfCap,Natural>`_
## * `newSeqUninitialized <#newSeqUninitialized,Natural>`_
## * `newSeqUninit <unsafeseqs.html#newSeqUninit,Natural>`_
ringabout marked this conversation as resolved.
Show resolved Hide resolved
## * `newSeqUnsafe <unsafeseqs.html#newSeqUnsafe,Natural>`_
newSeq(result, len)

proc newSeqOfCap*[T](cap: Natural): seq[T] {.
Expand Down Expand Up @@ -1611,7 +1612,7 @@ when notJSnotNims and defined(nimSeqsV2):
include "system/seqs_v2"

when not defined(js):
proc newSeqUninitialized*[T: SomeNumber](len: Natural): seq[T] =
proc newSeqUninitialized*[T: SomeNumber](len: Natural): seq[T] {.deprecated: "Use `unsafeseqs.newSeqUninit` instead".} =
## Creates a new sequence of type `seq[T]` with length `len`.
##
## Only available for numbers types. Note that the sequence will be
Expand Down