Skip to content

[Embedded] enable random APIs if a RandomNumberGenerator is provided #69645

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 4, 2023
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
1 change: 0 additions & 1 deletion stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public struct Bool: Sendable {
/// - Returns: Either `true` or `false`, randomly chosen with equal
/// probability.
@inlinable
@_unavailableInEmbedded
public static func random<T: RandomNumberGenerator>(
using generator: inout T
) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ split_embedded_sources(
NORMAL Diffing.swift
EMBEDDED Duration.swift
EMBEDDED DurationProtocol.swift
NORMAL FloatingPointRandom.swift
EMBEDDED FloatingPointRandom.swift
EMBEDDED Instant.swift
NORMAL Mirror.swift
NORMAL PlaygroundDisplay.swift
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,6 @@ extension Collection {
/// sequence may change when your program is compiled using a different
/// version of Swift.
@inlinable
@_unavailableInEmbedded
public func randomElement<T: RandomNumberGenerator>(
using generator: inout T
) -> Element? {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/CollectionAlgorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ extension Collection {
// shuffled()/shuffle()
//===----------------------------------------------------------------------===//

@_unavailableInEmbedded
extension Sequence {
/// Returns the elements of the sequence, shuffled using the given generator
/// as a source for randomness.
Expand Down Expand Up @@ -504,13 +503,13 @@ extension Sequence {
///
/// - Complexity: O(*n*), where *n* is the length of the sequence.
@inlinable
@_unavailableInEmbedded
public func shuffled() -> [Element] {
var g = SystemRandomNumberGenerator()
return shuffled(using: &g)
}
}

@_unavailableInEmbedded
extension MutableCollection where Self: RandomAccessCollection {
/// Shuffles the collection in place, using the given generator as a source
/// for randomness.
Expand Down Expand Up @@ -563,6 +562,7 @@ extension MutableCollection where Self: RandomAccessCollection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@inlinable
@_unavailableInEmbedded
public mutating func shuffle() {
var g = SystemRandomNumberGenerator()
shuffle(using: &g)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/core/FloatingPointRandom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
/// `range` must be finite and non-empty.
/// - Returns: A random value within the bounds of `range`.
@inlinable
@_unavailableInEmbedded
public static func random(in range: Range<Self>) -> Self {
var g = SystemRandomNumberGenerator()
return Self.random(in: range, using: &g)
Expand Down Expand Up @@ -208,6 +209,7 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
/// - Parameter range: The range in which to create a random value. Must be finite.
/// - Returns: A random value within the bounds of `range`.
@inlinable
@_unavailableInEmbedded
public static func random(in range: ClosedRange<Self>) -> Self {
var g = SystemRandomNumberGenerator()
return Self.random(in: range, using: &g)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Integers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,6 @@ extension FixedWidthInteger {
}
}

@_unavailableInEmbedded
extension FixedWidthInteger {
/// Returns a random value within the specified range, using the given
/// generator as a source for randomness.
Expand Down Expand Up @@ -2695,6 +2694,7 @@ extension FixedWidthInteger {
/// `range` must not be empty.
/// - Returns: A random value within the bounds of `range`.
@inlinable
@_unavailableInEmbedded
public static func random(in range: Range<Self>) -> Self {
var g = SystemRandomNumberGenerator()
return Self.random(in: range, using: &g)
Expand Down Expand Up @@ -2767,6 +2767,7 @@ extension FixedWidthInteger {
/// - Parameter range: The range in which to create a random value.
/// - Returns: A random value within the bounds of `range`.
@inlinable
@_unavailableInEmbedded
public static func random(in range: ClosedRange<Self>) -> Self {
var g = SystemRandomNumberGenerator()
return Self.random(in: range, using: &g)
Expand Down Expand Up @@ -3357,7 +3358,6 @@ extension FixedWidthInteger {
}
}

@_unavailableInEmbedded
extension FixedWidthInteger {
@inlinable
public static func _random<R: RandomNumberGenerator>(
Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/core/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import SwiftShims
///
/// Types that conform to `RandomNumberGenerator` should specifically document
/// the thread safety and quality of the generator.
@_unavailableInEmbedded
public protocol RandomNumberGenerator {
/// Returns a value from a uniform, independent distribution of binary data.
///
Expand All @@ -64,7 +63,6 @@ public protocol RandomNumberGenerator {
mutating func next() -> UInt64
}

@_unavailableInEmbedded
extension RandomNumberGenerator {

// An unavailable default implementation of next() prevents types that do
Expand Down