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

[Won't merge] Use stdlib random API directly via a C shim #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ let package = Package(
targets: ["UniqueID"]),
],
targets: [
.target(name: "Swift_UniqueID_RuntimeShims"),
.target(
name: "UniqueID",
dependencies: ["Swift_UniqueID_RuntimeShims"],
swiftSettings: settings),
.testTarget(
name: "UniqueIDTests",
Expand Down
15 changes: 15 additions & 0 deletions Sources/Swift_UniqueID_RuntimeShims/dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright The swift-UniqueID Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "shims.h"
21 changes: 21 additions & 0 deletions Sources/Swift_UniqueID_RuntimeShims/include/shims.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright The swift-UniqueID Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef Swift_UniqueID_RuntimeShims_h
#define Swift_UniqueID_RuntimeShims_h

#include "stddef.h"
void swift_stdlib_random(void*, size_t);

#endif /* Swift_UniqueID_RuntimeShims_h */
15 changes: 13 additions & 2 deletions Sources/UniqueID/UniqueID+v4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Swift_UniqueID_RuntimeShims

extension UniqueID {

/// Generates a new UUID with random bits from the system's random number generator.
Expand Down Expand Up @@ -41,8 +43,17 @@ extension UniqueID {
///
@inlinable
public static func random() -> UniqueID {
var rng = SystemRandomNumberGenerator()
return random(using: &rng)
var bytes = UniqueID.null.bytes
withUnsafeMutableBytes(of: &bytes) { dest in
swift_stdlib_random(dest.baseAddress!, 16)
}
// octet 6 = time_hi_and_version (high octet).
// high 4 bits = version number.
bytes.6 = (bytes.6 & 0xF) | 0x40
// octet 8 = clock_seq_high_and_reserved.
// high 2 bits = variant (10 = standard).
bytes.8 = (bytes.8 & 0x3F) | 0x80
return UniqueID(bytes: bytes)
}

/// Generates a new UUID with random bits from the given random number generator.
Expand Down