- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathrdar136267186.swift
41 lines (30 loc) · 1.16 KB
/
rdar136267186.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// RUN: %target-swift-frontend -O -emit-sil -verify %s
publicstructFoo{
@Clamped(0...1)
publicvarvalue:Double=1.0
}
@propertyWrapper
publicstructClamped<WrappedValue:Numeric&Comparable>{
publicinit(wrappedValue:WrappedValue, _ range:ClosedRange<WrappedValue>){
self.range = range
self._wrappedValue = wrappedValue.clamped(to: range)
}
publicletrange:ClosedRange<WrappedValue>
privatevar_wrappedValue:WrappedValue
publicvarwrappedValue:WrappedValue{
get{ _wrappedValue }
set{ _wrappedValue = newValue.clamped(to: range)}
}
}
publicextensionComparable{
func clamped(to range:ClosedRange<Self>, exceptions:Self...)->Self{clamped(to: range, exceptions: exceptions)}
func clamped(to range:ClosedRange<Self>, exceptions:anyCollection<Self>)->Self{
return exceptions.contains(self)
?self
:max(range.lowerBound,min(range.upperBound,self))
}
mutatingfunc clamp(to range:ClosedRange<Self>, exceptions:Self...){clamp(to: range, exceptions: exceptions)}
mutatingfunc clamp(to range:ClosedRange<Self>, exceptions:anyCollection<Self>){
self=self.clamped(to: range, exceptions: exceptions)
}
}