-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd076d6
deprecates `newSeqUninitialized` replaced by `newSeqUninit`
ringabout b39f174
adds a changelog
ringabout 73a5418
Update changelog.md
ringabout eaf417e
Update lib/system.nim
ringabout a448de2
adapt to reviews
ringabout 240a085
adds one more link
ringabout 1886ca1
Update changelog.md
ringabout 187f383
Update changelog.md
ringabout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# 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".} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 tonewSeqUnsafe
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 wayaddr
is unsafe.