Skip to content

Commit 22e22a8

Browse files
committed
Fix errors due to latest Swift 4 version
This closes #42.
1 parent 3fbc6ba commit 22e22a8

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

Sources/RandomKit/Extensions/Swift/Integer+RandomKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ extension UnsignedInteger where Self: RandomWithMaxWidth & RandomWithExactWidth
356356
/// Generates a random value of `Self` with an exact width using `randomGenerator`.
357357
public static func random<R: RandomGenerator>(withExactWidth width: Int, using randomGenerator: inout R) -> Self {
358358
guard width > 0 else { return 0 }
359-
return random(withMaxWidth: width, using: &randomGenerator) | (1 &<< Self(width - 1))
359+
return random(withMaxWidth: width, using: &randomGenerator) | Self(1 &<< Self(width - 1))
360360
}
361361
}
362362

@@ -577,7 +577,7 @@ extension Int8: UnsafeRandom, RandomWithMax, RandomWithMin, RandomToValue, Rando
577577
#if swift(>=3.2)
578578
extension UnsignedInteger where Self: FixedWidthInteger {
579579
fileprivate var _resigned: Self {
580-
let bits = Self(extendingOrTruncating: MemoryLayout<Self>.size * 8 - 1)
580+
let bits = Self(truncatingIfNeeded: MemoryLayout<Self>.size * 8 - 1)
581581
return self ^ (1 &<< bits)
582582
}
583583
}

Sources/RandomKit/Types/RandomGenerator/ARC4Random.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public final class ARC4Random: RandomGenerator {
6464
/// Generates a random unsigned 16-bit integer.
6565
public func random16() -> UInt16 {
6666
#if swift(>=3.2)
67-
return UInt16(extendingOrTruncating: random32())
67+
return UInt16(truncatingIfNeeded: random32())
6868
#else
6969
return UInt16(truncatingBitPattern: random32())
7070
#endif
@@ -73,7 +73,7 @@ public final class ARC4Random: RandomGenerator {
7373
/// Generates a random unsigned 8-bit integer.
7474
public func random8() -> UInt8 {
7575
#if swift(>=3.2)
76-
return UInt8(extendingOrTruncating: random32())
76+
return UInt8(truncatingIfNeeded: random32())
7777
#else
7878
return UInt8(truncatingBitPattern: random32())
7979
#endif

Sources/RandomKit/Types/RandomGenerator/ChaCha.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ public struct ChaCha: RandomBytesGenerator, Seedable, SeedableFromSequence, Seed
131131
/// Sets the internal 128-bit counter.
132132
public mutating func setCounter(low: UInt64, high: UInt64) {
133133
#if swift(>=3.2)
134-
_state.12 = UInt32(extendingOrTruncating: low)
135-
_state.13 = UInt32(extendingOrTruncating: low &>> 32)
136-
_state.14 = UInt32(extendingOrTruncating: high)
137-
_state.15 = UInt32(extendingOrTruncating: high &>> 32)
134+
_state.12 = UInt32(truncatingIfNeeded: low)
135+
_state.13 = UInt32(truncatingIfNeeded: low &>> 32)
136+
_state.14 = UInt32(truncatingIfNeeded: high)
137+
_state.15 = UInt32(truncatingIfNeeded: high &>> 32)
138138
#else
139139
_state.12 = UInt32(truncatingBitPattern: low)
140140
_state.13 = UInt32(truncatingBitPattern: low >> 32)

Sources/RandomKit/Types/RandomGenerator/RandomBytesGenerator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension RandomBytesGenerator where Bytes == UInt64 {
4646
/// Generates a random unsigned 32-bit integer.
4747
public mutating func random32() -> UInt32 {
4848
#if swift(>=3.2)
49-
return UInt32(extendingOrTruncating: randomBytes())
49+
return UInt32(truncatingIfNeeded: randomBytes())
5050
#else
5151
return UInt32(truncatingBitPattern: randomBytes())
5252
#endif
@@ -55,7 +55,7 @@ extension RandomBytesGenerator where Bytes == UInt64 {
5555
/// Generates a random unsigned 16-bit integer.
5656
public mutating func random16() -> UInt16 {
5757
#if swift(>=3.2)
58-
return UInt16(extendingOrTruncating: randomBytes())
58+
return UInt16(truncatingIfNeeded: randomBytes())
5959
#else
6060
return UInt16(truncatingBitPattern: randomBytes())
6161
#endif
@@ -64,7 +64,7 @@ extension RandomBytesGenerator where Bytes == UInt64 {
6464
/// Generates a random unsigned 8-bit integer.
6565
public mutating func random8() -> UInt8 {
6666
#if swift(>=3.2)
67-
return UInt8(extendingOrTruncating: randomBytes())
67+
return UInt8(truncatingIfNeeded: randomBytes())
6868
#else
6969
return UInt8(truncatingBitPattern: randomBytes())
7070
#endif
@@ -87,7 +87,7 @@ extension RandomBytesGenerator where Bytes == UInt32 {
8787
/// Generates a random unsigned 16-bit integer.
8888
public mutating func random16() -> UInt16 {
8989
#if swift(>=3.2)
90-
return UInt16(extendingOrTruncating: randomBytes())
90+
return UInt16(truncatingIfNeeded: randomBytes())
9191
#else
9292
return UInt16(truncatingBitPattern: randomBytes())
9393
#endif
@@ -96,7 +96,7 @@ extension RandomBytesGenerator where Bytes == UInt32 {
9696
/// Generates a random unsigned 8-bit integer.
9797
public mutating func random8() -> UInt8 {
9898
#if swift(>=3.2)
99-
return UInt8(extendingOrTruncating: randomBytes())
99+
return UInt8(truncatingIfNeeded: randomBytes())
100100
#else
101101
return UInt8(truncatingBitPattern: randomBytes())
102102
#endif

Sources/RandomKit/Types/RandomGenerator/ReseedingRandomGenerator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ extension Int {
109109
fileprivate func _saturatingAddPositive(_ other: Int) -> Int {
110110
#if swift(>=3.2)
111111
let (result, overflow) = addingReportingOverflow(other)
112-
return overflow == .overflow ? .max : result
113112
#else
114113
let (result, overflow) = Int.addWithOverflow(self, other)
115-
return overflow ? .max : result
116114
#endif
115+
return overflow ? .max : result
117116
}
118117
}
119118

0 commit comments

Comments
 (0)