Skip to content

[stdib] Remove RandomNumberGenerator._fill(bytes:) #20463

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

Merged
merged 1 commit into from
Nov 9, 2018
Merged
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
28 changes: 0 additions & 28 deletions stdlib/public/core/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,6 @@ public protocol RandomNumberGenerator {
///
/// - Returns: An unsigned 64-bit random value.
mutating func next() -> UInt64

// FIXME: De-underscore after swift-evolution amendment
mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer)
}

extension RandomNumberGenerator {
@inlinable
public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) {
// FIXME: Optimize
var chunk: UInt64 = 0
var chunkBytes = 0
for i in 0..<buffer.count {
if chunkBytes == 0 {
chunk = next()
chunkBytes = UInt64.bitWidth / 8
}
buffer[i] = UInt8(truncatingIfNeeded: chunk)
chunk >>= UInt8.bitWidth
chunkBytes -= 1
}
}
}

extension RandomNumberGenerator {
Expand Down Expand Up @@ -167,11 +146,4 @@ public struct SystemRandomNumberGenerator : RandomNumberGenerator {
swift_stdlib_random(&random, MemoryLayout<UInt64>.size)
return random
}

@inlinable
public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) {
if !buffer.isEmpty {
swift_stdlib_random(buffer.baseAddress!, buffer.count)
}
}
}
3 changes: 3 additions & 0 deletions test/api-digester/Outputs/stability-stdlib-abi.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ Protocol _NSStringCore has been removed

Constructor ManagedBuffer.init(_doNotCallMe:) has been removed
Func _makeAnyHashableUpcastingToHashableBaseType(_:storingResultInto:) has been removed

Func RandomNumberGenerator._fill(bytes:) has been removed
Func SystemRandomNumberGenerator._fill(bytes:) has been removed
8 changes: 8 additions & 0 deletions validation-test/stdlib/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

import StdlibUnittest
import StdlibCollectionUnittest
import SwiftShims // for swift_stdlib_random

let RandomTests = TestSuite("Random")

// _fill(bytes:)

extension RandomNumberGenerator {
func _fill(bytes buffer: UnsafeMutableRawBufferPointer) {
guard let start = buffer.baseAddress else { return }
swift_stdlib_random(start, buffer.count)
}
}

RandomTests.test("_fill(bytes:)") {
for count in [100, 1000] {
var bytes1 = [UInt8](repeating: 0, count: count)
Expand Down