File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
validation-test/SILOptimizer Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments