Skip to content

Commit 6cf8f9b

Browse files
author
Max Moiseev
committed
Extract operators into a protocol
1 parent e0f4985 commit 6cf8f9b

File tree

3 files changed

+37
-224
lines changed

3 files changed

+37
-224
lines changed

stdlib/public/SDK/AppKit/AppKit.swift

Lines changed: 37 additions & 218 deletions
Original file line numberDiff line numberDiff line change
@@ -101,241 +101,60 @@ extension NSImage : _ExpressibleByImageLiteral {
101101

102102
public typealias _ImageLiteralType = NSImage
103103

104-
// Extensions for common numeric enum operations
104+
// Numeric backed types
105105

106106
@available(swift 4)
107-
public protocol _AppKitNumericWrapper : RawRepresentable { }
108-
extension _AppKitNumericWrapper where RawValue == Float {
109-
public static func +(lhs: Self, rhs: Double) -> Self {
110-
return Self(rawValue: lhs.rawValue + Float(rhs))!
111-
}
112-
public static func -(lhs: Self, rhs: Double) -> Self {
113-
return Self(rawValue: lhs.rawValue - Float(rhs))!
114-
}
115-
public static func +(lhs: Self, rhs: Float) -> Self {
116-
return Self(rawValue: lhs.rawValue + rhs)!
117-
}
118-
public static func -(lhs: Self, rhs: Float) -> Self {
119-
return Self(rawValue: lhs.rawValue - rhs)!
120-
}
121-
public static func +(lhs: Self, rhs: Int) -> Self {
122-
return Self(rawValue: lhs.rawValue + Float(rhs))!
123-
}
124-
public static func -(lhs: Self, rhs: Int) -> Self {
125-
return Self(rawValue: lhs.rawValue - Float(rhs))!
126-
}
127-
public static func +(lhs: Float, rhs: Self) -> Self {
128-
return Self(rawValue: lhs + rhs.rawValue)!
129-
}
130-
public static func +(lhs: Int, rhs: Self) -> Self {
131-
return Self(rawValue: Float(lhs) + rhs.rawValue)!
132-
}
133-
public static func +(lhs: Double, rhs: Self) -> Self {
134-
return Self(rawValue: Float(lhs) + rhs.rawValue)!
135-
}
136-
public static func += (lhs: inout Self, rhs: Double) {
137-
lhs = Self(rawValue: lhs.rawValue + Float(rhs))!
138-
}
139-
public static func += (lhs: inout Self, rhs: Float) {
140-
lhs = Self(rawValue: lhs.rawValue + rhs)!
141-
}
142-
public static func += (lhs: inout Self, rhs: Int) {
143-
lhs = Self(rawValue: lhs.rawValue + Float(rhs))!
144-
}
145-
public static func -= (lhs: inout Self, rhs: Double) {
146-
lhs = Self(rawValue: lhs.rawValue - Float(rhs))!
147-
}
148-
public static func -= (lhs: inout Self, rhs: Float) {
149-
lhs = Self(rawValue: lhs.rawValue - rhs)!
150-
}
151-
public static func -= (lhs: inout Self, rhs: Int) {
152-
lhs = Self(rawValue: lhs.rawValue - Float(rhs))!
153-
}
154-
}
155-
extension _AppKitNumericWrapper where RawValue == CGFloat {
156-
public static func +(lhs: Self, rhs: Double) -> Self {
157-
return Self(rawValue: lhs.rawValue + CGFloat(rhs))!
158-
}
159-
public static func -(lhs: Self, rhs: Double) -> Self {
160-
return Self(rawValue: lhs.rawValue - CGFloat(rhs))!
161-
}
162-
public static func +(lhs: Self, rhs: Float) -> Self {
163-
return Self(rawValue: lhs.rawValue + CGFloat(rhs))!
164-
}
165-
public static func -(lhs: Self, rhs: Float) -> Self {
166-
return Self(rawValue: lhs.rawValue - CGFloat(rhs))!
167-
}
168-
public static func +(lhs: Self, rhs: CGFloat) -> Self {
169-
return Self(rawValue: lhs.rawValue + rhs)!
170-
}
171-
public static func -(lhs: Self, rhs: CGFloat) -> Self {
172-
return Self(rawValue: lhs.rawValue - rhs)!
173-
}
174-
public static func +(lhs: Self, rhs: Int) -> Self {
175-
return Self(rawValue: lhs.rawValue + CGFloat(rhs))!
176-
}
177-
public static func -(lhs: Self, rhs: Int) -> Self {
178-
return Self(rawValue: lhs.rawValue - CGFloat(rhs))!
179-
}
180-
public static func +(lhs: Float, rhs: Self) -> Self {
181-
return Self(rawValue: CGFloat(lhs) + rhs.rawValue)!
182-
}
183-
public static func +(lhs: Int, rhs: Self) -> Self {
184-
return Self(rawValue: CGFloat(lhs) + rhs.rawValue)!
185-
}
186-
public static func +(lhs: Double, rhs: Self) -> Self {
187-
return Self(rawValue: CGFloat(lhs) + rhs.rawValue)!
188-
}
189-
public static func += (lhs: inout Self, rhs: Double) {
190-
lhs = Self(rawValue: lhs.rawValue + CGFloat(rhs))!
191-
}
192-
public static func += (lhs: inout Self, rhs: Float) {
193-
lhs = Self(rawValue: lhs.rawValue + CGFloat(rhs))!
194-
}
195-
public static func += (lhs: inout Self, rhs: CGFloat) {
196-
lhs = Self(rawValue: lhs.rawValue + rhs)!
197-
}
198-
public static func += (lhs: inout Self, rhs: Int) {
199-
lhs = Self(rawValue: lhs.rawValue + CGFloat(rhs))!
200-
}
201-
public static func -= (lhs: inout Self, rhs: Double) {
202-
lhs = Self(rawValue: lhs.rawValue - CGFloat(rhs))!
203-
}
204-
public static func -= (lhs: inout Self, rhs: Float) {
205-
lhs = Self(rawValue: lhs.rawValue - CGFloat(rhs))!
206-
}
207-
public static func -= (lhs: inout Self, rhs: CGFloat) {
208-
lhs = Self(rawValue: lhs.rawValue - rhs)!
209-
}
210-
public static func -= (lhs: inout Self, rhs: Int) {
211-
lhs = Self(rawValue: lhs.rawValue - CGFloat(rhs))!
107+
public protocol _AppKitKitNumericRawRepresentable : RawRepresentable, Comparable
108+
where RawValue: Comparable & Numeric { }
109+
110+
extension _AppKitKitNumericRawRepresentable {
111+
public static func <(lhs: Self, rhs: Self) -> Bool {
112+
return lhs.rawValue < rhs.rawValue
212113
}
213-
}
214-
extension _AppKitNumericWrapper where RawValue == Double {
215-
public static func +(lhs: Self, rhs: Double) -> Self {
114+
115+
public static func +(lhs: Self, rhs: RawValue) -> Self {
216116
return Self(rawValue: lhs.rawValue + rhs)!
217117
}
218-
public static func -(lhs: Self, rhs: Double) -> Self {
219-
return Self(rawValue: lhs.rawValue - rhs)!
220-
}
221-
public static func +(lhs: Self, rhs: Float) -> Self {
222-
return Self(rawValue: lhs.rawValue + Double(rhs))!
223-
}
224-
public static func -(lhs: Self, rhs: Float) -> Self {
225-
return Self(rawValue: lhs.rawValue - Double(rhs))!
226-
}
227-
public static func +(lhs: Self, rhs: Int) -> Self {
228-
return Self(rawValue: lhs.rawValue + Double(rhs))!
229-
}
230-
public static func -(lhs: Self, rhs: Int) -> Self {
231-
return Self(rawValue: lhs.rawValue - Double(rhs))!
232-
}
233-
public static func +(lhs: Float, rhs: Self) -> Self {
234-
return Self(rawValue: Double(lhs) + rhs.rawValue)!
235-
}
236-
public static func +(lhs: Int, rhs: Self) -> Self {
237-
return Self(rawValue: Double(lhs) + rhs.rawValue)!
238-
}
239-
public static func +(lhs: Double, rhs: Self) -> Self {
118+
119+
public static func +(lhs: RawValue, rhs: Self) -> Self {
240120
return Self(rawValue: lhs + rhs.rawValue)!
241121
}
242-
public static func += (lhs: inout Self, rhs: Double) {
243-
lhs = Self(rawValue: lhs.rawValue + rhs)!
244-
}
245-
public static func += (lhs: inout Self, rhs: Float) {
246-
lhs = Self(rawValue: lhs.rawValue + Double(rhs))!
247-
}
248-
public static func += (lhs: inout Self, rhs: Int) {
249-
lhs = Self(rawValue: lhs.rawValue + Double(rhs))!
250-
}
251-
public static func -= (lhs: inout Self, rhs: Double) {
252-
lhs = Self(rawValue: lhs.rawValue - rhs)!
253-
}
254-
public static func -= (lhs: inout Self, rhs: Float) {
255-
lhs = Self(rawValue: lhs.rawValue - Double(rhs))!
256-
}
257-
public static func -= (lhs: inout Self, rhs: Int) {
258-
lhs = Self(rawValue: lhs.rawValue - Double(rhs))!
259-
}
260-
}
261-
extension _AppKitNumericWrapper where RawValue == Int {
262-
public static func +(lhs: Self, rhs: Int) -> Self {
263-
return Self(rawValue: lhs.rawValue + rhs)!
264-
}
265-
public static func -(lhs: Self, rhs: Int) -> Self {
122+
123+
public static func -(lhs: Self, rhs: RawValue) -> Self {
266124
return Self(rawValue: lhs.rawValue - rhs)!
267125
}
268-
public static func +(lhs: Int, rhs: Self) -> Self {
269-
return Self(rawValue: lhs + rhs.rawValue)!
126+
127+
public static func -(lhs: Self, rhs: Self) -> RawValue {
128+
return lhs.rawValue - rhs.rawValue
270129
}
271-
public static func += (lhs: inout Self, rhs: Int) {
272-
lhs = Self(rawValue: lhs.rawValue + rhs)!
130+
131+
public static func +=(lhs: inout Self, rhs: RawValue) {
132+
lhs = Self(rawValue: lhs.rawValue + rhs)!
273133
}
274-
public static func -= (lhs: inout Self, rhs: Int) {
275-
lhs = Self(rawValue: lhs.rawValue - rhs)!
134+
135+
public static func -=(lhs: inout Self, rhs: RawValue) {
136+
lhs = Self(rawValue: lhs.rawValue - rhs)!
276137
}
277138
}
278139

279140
@available(swift 4)
280-
extension NSAppKitVersion : _AppKitNumericWrapper, Comparable {
281-
public static func <(lhs: NSAppKitVersion, rhs: NSAppKitVersion) -> Bool {
282-
return lhs.rawValue < rhs.rawValue
283-
}
284-
public static func >(lhs: NSAppKitVersion, rhs: NSAppKitVersion) -> Bool {
285-
return lhs.rawValue > rhs.rawValue
286-
}
287-
}
141+
extension NSAppKitVersion : _AppKitKitNumericRawRepresentable { }
142+
288143
@available(swift 4)
289-
extension NSLayoutConstraint.Priority : _AppKitNumericWrapper, Comparable {
290-
public static func <(lhs: NSLayoutConstraint.Priority, rhs: NSLayoutConstraint.Priority) -> Bool {
291-
return lhs.rawValue < rhs.rawValue
292-
}
293-
public static func >(lhs: NSLayoutConstraint.Priority, rhs: NSLayoutConstraint.Priority) -> Bool {
294-
return lhs.rawValue > rhs.rawValue
295-
}
296-
}
144+
extension NSLayoutConstraint.Priority : _AppKitKitNumericRawRepresentable { }
145+
297146
@available(swift 4)
298-
extension NSStackView.VisibilityPriority : _AppKitNumericWrapper, Comparable {
299-
public static func <(lhs: NSStackView.VisibilityPriority, rhs: NSStackView.VisibilityPriority) -> Bool {
300-
return lhs.rawValue < rhs.rawValue
301-
}
302-
public static func >(lhs: NSStackView.VisibilityPriority, rhs: NSStackView.VisibilityPriority) -> Bool {
303-
return lhs.rawValue > rhs.rawValue
304-
}
305-
}
147+
extension NSStackView.VisibilityPriority : _AppKitKitNumericRawRepresentable { }
148+
306149
@available(swift 4)
307-
extension NSToolbarItem.VisibilityPriority : _AppKitNumericWrapper, Comparable {
308-
public static func <(lhs: NSToolbarItem.VisibilityPriority, rhs: NSToolbarItem.VisibilityPriority) -> Bool {
309-
return lhs.rawValue < rhs.rawValue
310-
}
311-
public static func >(lhs: NSToolbarItem.VisibilityPriority, rhs: NSToolbarItem.VisibilityPriority) -> Bool {
312-
return lhs.rawValue > rhs.rawValue
313-
}
314-
}
315-
@available(swift 4) @available(macOS 10.12.2, *)
316-
extension NSTouchBarItem.Priority : _AppKitNumericWrapper, Comparable {
317-
public static func <(lhs: NSTouchBarItem.Priority, rhs: NSTouchBarItem.Priority) -> Bool {
318-
return lhs.rawValue < rhs.rawValue
319-
}
320-
public static func >(lhs: NSTouchBarItem.Priority, rhs: NSTouchBarItem.Priority) -> Bool {
321-
return lhs.rawValue > rhs.rawValue
322-
}
323-
}
150+
extension NSToolbarItem.VisibilityPriority : _AppKitKitNumericRawRepresentable { }
151+
152+
@available(macOS 10.12.2, *)
324153
@available(swift 4)
325-
extension NSWindow.Level : _AppKitNumericWrapper, Comparable {
326-
public static func <(lhs: NSWindow.Level, rhs: NSWindow.Level) -> Bool {
327-
return lhs.rawValue < rhs.rawValue
328-
}
329-
public static func >(lhs: NSWindow.Level, rhs: NSWindow.Level) -> Bool {
330-
return lhs.rawValue > rhs.rawValue
331-
}
332-
}
154+
extension NSTouchBarItem.Priority : _AppKitKitNumericRawRepresentable { }
155+
333156
@available(swift 4)
334-
extension NSFont.Weight : _AppKitNumericWrapper, Comparable {
335-
public static func <(lhs: NSFont.Weight, rhs: NSFont.Weight) -> Bool {
336-
return lhs.rawValue < rhs.rawValue
337-
}
338-
public static func >(lhs: NSFont.Weight, rhs: NSFont.Weight) -> Bool {
339-
return lhs.rawValue > rhs.rawValue
340-
}
341-
}
157+
extension NSWindow.Level : _AppKitKitNumericRawRepresentable { }
158+
159+
@available(swift 4)
160+
extension NSFont.Weight : _AppKitKitNumericRawRepresentable { }

test/stdlib/AppKit_Swift3.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ AppKitTests.test("NSEventMaskFromType") {
2020
AppKitTests.test("NSLayoutPriority") {
2121
let highPriority: NSLayoutPriority = NSLayoutPriorityDefaultHigh
2222

23-
// Priority added to Int
2423
let adjustedPriority1 = highPriority + 1
2524
let adjustedPriority1RawValue: Float = NSLayoutPriorityDefaultHigh + 1
2625
expectEqual(adjustedPriority1, adjustedPriority1RawValue)
2726

28-
// Double subtracted from Priority
2927
let adjustedPriority2 = highPriority - 5.0
3028
let adjustedPriority2RawValue: Float = NSLayoutPriorityDefaultHigh - 5.0
3129
expectEqual(adjustedPriority2, adjustedPriority2RawValue)
3230

33-
// Double added to Priority
3431
let adjustedPriority3 = 5.0 + highPriority
3532
let adjustedPriority3RawValue: Float = 5.0 + NSLayoutPriorityDefaultHigh
3633
expectEqual(adjustedPriority3, adjustedPriority3RawValue)

test/stdlib/AppKit_Swift4.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,14 @@ AppKitTests.test("NSColor.Literals") {
108108
AppKitTests.test("NSLayoutPriority") {
109109
let highPriority: NSLayoutConstraint.Priority = .defaultHigh
110110

111-
// Priority added to Int
112111
let adjustedPriority1 = highPriority + 1
113112
let adjustedPriority1RawValue: Float = NSLayoutConstraint.Priority.defaultHigh.rawValue + 1
114113
expectEqual(adjustedPriority1.rawValue, adjustedPriority1RawValue)
115114

116-
// Double subtracted from Priority
117115
let adjustedPriority2 = highPriority - 5.0
118116
let adjustedPriority2RawValue: Float = NSLayoutConstraint.Priority.defaultHigh.rawValue - 5.0
119117
expectEqual(adjustedPriority2.rawValue, adjustedPriority2RawValue)
120118

121-
// Double added to Priority
122119
let adjustedPriority3 = 5.0 + highPriority
123120
let adjustedPriority3RawValue: Float = 5.0 + NSLayoutConstraint.Priority.defaultHigh.rawValue
124121
expectEqual(adjustedPriority3.rawValue, adjustedPriority3RawValue)

0 commit comments

Comments
 (0)