Skip to content

Commit 7a2c21d

Browse files
authored
[Embedded] enable random APIs if a RandomNumberGenerator is provided, but leave system random number generators unavailable (#69645)
1 parent 24dc372 commit 7a2c21d

File tree

7 files changed

+7
-9
lines changed

7 files changed

+7
-9
lines changed

stdlib/public/core/Bool.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public struct Bool: Sendable {
111111
/// - Returns: Either `true` or `false`, randomly chosen with equal
112112
/// probability.
113113
@inlinable
114-
@_unavailableInEmbedded
115114
public static func random<T: RandomNumberGenerator>(
116115
using generator: inout T
117116
) -> Bool {

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ split_embedded_sources(
221221
NORMAL Diffing.swift
222222
EMBEDDED Duration.swift
223223
EMBEDDED DurationProtocol.swift
224-
NORMAL FloatingPointRandom.swift
224+
EMBEDDED FloatingPointRandom.swift
225225
EMBEDDED Instant.swift
226226
NORMAL Mirror.swift
227227
NORMAL PlaygroundDisplay.swift

stdlib/public/core/Collection.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,6 @@ extension Collection {
923923
/// sequence may change when your program is compiled using a different
924924
/// version of Swift.
925925
@inlinable
926-
@_unavailableInEmbedded
927926
public func randomElement<T: RandomNumberGenerator>(
928927
using generator: inout T
929928
) -> Element? {

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ extension Collection {
455455
// shuffled()/shuffle()
456456
//===----------------------------------------------------------------------===//
457457

458-
@_unavailableInEmbedded
459458
extension Sequence {
460459
/// Returns the elements of the sequence, shuffled using the given generator
461460
/// as a source for randomness.
@@ -504,13 +503,13 @@ extension Sequence {
504503
///
505504
/// - Complexity: O(*n*), where *n* is the length of the sequence.
506505
@inlinable
506+
@_unavailableInEmbedded
507507
public func shuffled() -> [Element] {
508508
var g = SystemRandomNumberGenerator()
509509
return shuffled(using: &g)
510510
}
511511
}
512512

513-
@_unavailableInEmbedded
514513
extension MutableCollection where Self: RandomAccessCollection {
515514
/// Shuffles the collection in place, using the given generator as a source
516515
/// for randomness.
@@ -563,6 +562,7 @@ extension MutableCollection where Self: RandomAccessCollection {
563562
///
564563
/// - Complexity: O(*n*), where *n* is the length of the collection.
565564
@inlinable
565+
@_unavailableInEmbedded
566566
public mutating func shuffle() {
567567
var g = SystemRandomNumberGenerator()
568568
shuffle(using: &g)

stdlib/public/core/FloatingPointRandom.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
108108
/// `range` must be finite and non-empty.
109109
/// - Returns: A random value within the bounds of `range`.
110110
@inlinable
111+
@_unavailableInEmbedded
111112
public static func random(in range: Range<Self>) -> Self {
112113
var g = SystemRandomNumberGenerator()
113114
return Self.random(in: range, using: &g)
@@ -208,6 +209,7 @@ extension BinaryFloatingPoint where Self.RawSignificand: FixedWidthInteger {
208209
/// - Parameter range: The range in which to create a random value. Must be finite.
209210
/// - Returns: A random value within the bounds of `range`.
210211
@inlinable
212+
@_unavailableInEmbedded
211213
public static func random(in range: ClosedRange<Self>) -> Self {
212214
var g = SystemRandomNumberGenerator()
213215
return Self.random(in: range, using: &g)

stdlib/public/core/Integers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,6 @@ extension FixedWidthInteger {
26222622
}
26232623
}
26242624

2625-
@_unavailableInEmbedded
26262625
extension FixedWidthInteger {
26272626
/// Returns a random value within the specified range, using the given
26282627
/// generator as a source for randomness.
@@ -2695,6 +2694,7 @@ extension FixedWidthInteger {
26952694
/// `range` must not be empty.
26962695
/// - Returns: A random value within the bounds of `range`.
26972696
@inlinable
2697+
@_unavailableInEmbedded
26982698
public static func random(in range: Range<Self>) -> Self {
26992699
var g = SystemRandomNumberGenerator()
27002700
return Self.random(in: range, using: &g)
@@ -2767,6 +2767,7 @@ extension FixedWidthInteger {
27672767
/// - Parameter range: The range in which to create a random value.
27682768
/// - Returns: A random value within the bounds of `range`.
27692769
@inlinable
2770+
@_unavailableInEmbedded
27702771
public static func random(in range: ClosedRange<Self>) -> Self {
27712772
var g = SystemRandomNumberGenerator()
27722773
return Self.random(in: range, using: &g)
@@ -3357,7 +3358,6 @@ extension FixedWidthInteger {
33573358
}
33583359
}
33593360

3360-
@_unavailableInEmbedded
33613361
extension FixedWidthInteger {
33623362
@inlinable
33633363
public static func _random<R: RandomNumberGenerator>(

stdlib/public/core/Random.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import SwiftShims
5151
///
5252
/// Types that conform to `RandomNumberGenerator` should specifically document
5353
/// the thread safety and quality of the generator.
54-
@_unavailableInEmbedded
5554
public protocol RandomNumberGenerator {
5655
/// Returns a value from a uniform, independent distribution of binary data.
5756
///
@@ -64,7 +63,6 @@ public protocol RandomNumberGenerator {
6463
mutating func next() -> UInt64
6564
}
6665

67-
@_unavailableInEmbedded
6866
extension RandomNumberGenerator {
6967

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

0 commit comments

Comments
 (0)