-
Notifications
You must be signed in to change notification settings - Fork 190
Proposal to generate UUID
s using RandomNumberGenerator
s
#1271
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
Open
FranzBusch
wants to merge
3
commits into
main
Choose a base branch
from
fb-random-uuid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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 hidden or 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,57 @@ | ||
# Generating UUIDs using RandomNumberGenerators | ||
|
||
* Proposal: [SF-NNNN](NNNN-random-uuid.md) | ||
* Authors: [FranzBusch](https://github.com/FranzBusch) | ||
* Review Manager: TBD | ||
* Status: **Awaiting review** | ||
* Implementation: [swiftlang/swift-foundation#1271](https://github.com/swiftlang/swift-foundation/pull/1271) | ||
* Review: ([pitch](https://forums.swift.org/...)) | ||
|
||
## Introduction | ||
|
||
UUIDs (Universally Unique IDentifiers) are 128 bits long and is intended to | ||
guarantee uniqueness across space and time. This proposal adds APIs to generate | ||
UUIDs from Swift's random number generators. | ||
|
||
## Motivation | ||
|
||
UUIDs often need to be randomly generated. This is currently possible by calling | ||
the `UUID` initializer. However, this initializer doesn't allow providing a | ||
custom source from which the `UUID` is generated. Swift's standard library | ||
provides a common abstraction for random number generators through the | ||
`RandomNumberGenerator` protocol. Providing methods to generate `UUID`s using a | ||
`RandomNumberGenerator` allows developers to customize their source of randomness. | ||
|
||
An example where this is useful is where a system needs to generate UUIDs using a | ||
deterministically seeded random number generator. | ||
|
||
## Proposed solution | ||
|
||
This proposal adds a new static method to the `UUID` type to generate new random `UUIDs` using a `RandomNumberGenerator`. | ||
|
||
```swift | ||
/// Generates a new random UUID. | ||
/// | ||
/// - Parameter generator: The random number generator to use when creating the new random value. | ||
/// - Returns: A random UUID. | ||
@available(FoundationPreview 6.2, *) | ||
public static func random( | ||
using generator: inout some RandomNumberGenerator | ||
) -> UUID | ||
``` | ||
|
||
## Source compatibility | ||
|
||
The new API is purely additive and ha no impact on the existing API. | ||
|
||
## Implications on adoption | ||
|
||
This feature can be freely adopted and un-adopted in source code with no deployment constraints and without affecting source compatibility. | ||
|
||
## Alternatives considered | ||
|
||
### Initializer based random UUID generation | ||
|
||
The existing `UUID.init()` is already generating new random `UUID`s and a new | ||
`UUID(using: &rng)` method would be a good alternative to the proposed static method. | ||
However, the static `random` method has precedence on various types such as [Int.random](https://developer.apple.com/documentation/swift/int/random(in:)-9mjpw). |
This file contains hidden or 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 hidden or 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.
Is the code wrong or the comment wrong? This appears to clear bits 12 ... 15.
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.
The comment was wrong. This clears bits 48-51 of the UInt64 aligned with the RFC. Example below
10010100 10010010 10111010 11000100 11110011 01010011 00101001 11100111
10010100 10010010 10111010 11000100 11110011 01010011 00001001 11100111