Skip to content

Commit 01157bf

Browse files
Merge pull request #76578 from nate-chandler/rdar136267186
[Test] Add regression test.
2 parents ebc4e8d + 653d7b3 commit 01157bf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %target-swift-frontend -O -emit-sil -verify %s
2+
3+
public struct Foo {
4+
5+
@Clamped(0...1)
6+
public var value: Double = 1.0
7+
}
8+
9+
@propertyWrapper
10+
public struct Clamped<WrappedValue: Numeric & Comparable> {
11+
12+
public init(wrappedValue: WrappedValue, _ range: ClosedRange<WrappedValue>) {
13+
self.range = range
14+
self._wrappedValue = wrappedValue.clamped(to: range)
15+
}
16+
17+
public let range: ClosedRange<WrappedValue>
18+
19+
private var _wrappedValue: WrappedValue
20+
public var wrappedValue: WrappedValue {
21+
get { _wrappedValue }
22+
set { _wrappedValue = newValue.clamped(to: range) }
23+
}
24+
}
25+
26+
public extension Comparable {
27+
28+
func clamped(to range: ClosedRange<Self>, exceptions: Self...) -> Self { clamped(to: range, exceptions: exceptions) }
29+
func clamped(to range: ClosedRange<Self>, exceptions: any Collection<Self>) -> Self {
30+
31+
return exceptions.contains(self)
32+
? self
33+
: max(range.lowerBound, min(range.upperBound, self))
34+
}
35+
36+
mutating func clamp(to range: ClosedRange<Self>, exceptions: Self...) { clamp(to: range, exceptions: exceptions) }
37+
mutating func clamp(to range: ClosedRange<Self>, exceptions: any Collection<Self>) {
38+
self = self.clamped(to: range, exceptions: exceptions)
39+
}
40+
}
41+

0 commit comments

Comments
 (0)