-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add UUID v4 generation to the standard library. #6618
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
Conversation
UUIDs must be unique, and accepting an arbitrary RNG doesn't guarantee that. An application may inadvertently use a deterministic or badly seeded RNG. Do we really want the application to provide the RNG? Not having to do so would also simplify the interface. |
@jedisct1 How would you propose making it truly unique? |
With |
For comparison: uuid-rs/uuid
|
I think there should be methods for both with and without rng parameter. Sometimes gathering entropy in kernel can be slow. E.g app running in a freshly started Docker container. I have seen cases where it takes minutes to gather enough entropy to generate a couple of random UUIDs. |
Was is with the On Linux, it is using the On BSDs, it is using If something ever blocks, it means that the system is unable to generate random numbers, so it would be unable to generate UUIDs based on random numbers anyway. |
It was not related to zig. I don't remember exact details, it used /dev/random or some other thing that definitely blocked, though. |
In many situations you don't want or require a syscall for randomness, e.g.
on thread Start you would usually seed a threadlocal csprng from the
kernel, and then use that in the thread.
Similarly for freestanding targets you will need to provide a rand object.
…On Fri, 9 Oct 2020, 21:36 Frank Denis, ***@***.***> wrote:
Was is with the randomBytes() function?
On Linux, it is using the getrandom() system call which should never
block; there's enough entropy gathered when the system starts to provide
all the randomness you will ever need later.
On BSDs, it is using arc4random() that never blocks. Same on WASI. If
these functions are not available, it is using /dev/urandom (not
/dev/random) that doesn't block either.
If something ever blocks, it means that the system is unable to generate
random numbers, so it would be unable to generate UUIDs based on random
numbers anyway.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#6618 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA7HY3EQ3BHE6W7OYASAMTSJ3RTBANCNFSM4SJO74AQ>
.
|
#6622 explains my concerns about requiring a We currently don't have any safe option to use in that context. And if we had one, would we need more? |
I'll get on this tomorrow |
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.
Looks good enough to me now!
Left a couple of nits that don't need to be fixed
Also, should have space after //
before comment.
lib/std/uuid.zig
Outdated
const flip: u128 = 0b1111 << 48; | ||
|
||
//We then set the 4 bits at 48 to 0x4 (0b0100) | ||
return Self{ .id = ((gen.int(u128) & ~flip) | (0x4 << 48)) }; |
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.
there's an extra set of parenthesis here (the outermost pair)
lib/std/uuid.zig
Outdated
const selector = @truncate(u4, self.id >> shift); | ||
shift += 4; | ||
|
||
buf[i] = chars[@intCast(usize, selector)]; |
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.
no need for an @intCast
here: a cast from u4
to usize
will be implicit.
lib/std/uuid.zig
Outdated
pub fn format(value: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { | ||
var buf = [_]u8{0} ** 36; | ||
|
||
var chars = "0123456789ABCDEF"; |
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.
Should set to undefined
here: var chars: *const [16]u8 = undefined;
Or could reorganise to have:
const chars = if ( ..... ) ... else if (....) ... else ...;
Here's a proposal which is intended to address this use case: #6704 Idea here would be the API could be a simple, global, infallible call to gimmeDatUUID() and it would use this thread-local CSPRNG as the backing RNG. |
To be clear, this PR is blocking on #6704 |
This is about to get unblocked by #7482 |
|
Upon further reflection, I think this code is appropriate for a third party library rather than the std lib. |
This is a small addition to the standard library which allows you to create a UUID v4 UUID with an allocator and random number generator.
Example use:
Output:
b29f4b7f-945b-4e55-b95b-b6ebeab198e2