Skip to content

Commit

Permalink
add: CaseIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushi130 committed Sep 9, 2018
1 parent b376536 commit 1aed1c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Sources/CaseIterable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CaseIterable.swift
// SwiftExtensions
//
// Created by Atsushi Miyake on 2018/09/09.
// Copyright © 2018年 Atsushi Miyake. All rights reserved.
//

import Foundation

public protocol CaseIterable {
static var allCases: [Self] { get }
}

extension CaseIterable where Self: Hashable {

private static var iterator: AnyIterator<Self> {
var n = 0
return AnyIterator {
defer { n += 1 }
let next = withUnsafePointer(to: &n) {
UnsafeRawPointer($0).assumingMemoryBound(to: Self.self).pointee
}
return next.hashValue == n ? next : nil
}
}

public static var allCases: [Self] {
return Array(self.iterator)
}
}
4 changes: 4 additions & 0 deletions SwiftExtensions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
0D2E0536203A4C5600F5AB3C /* StoryboardInstantiatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D2E0535203A4C5600F5AB3C /* StoryboardInstantiatable.swift */; };
0D68994C1FFBC997004640CE /* NibInstantiatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D68994B1FFBC997004640CE /* NibInstantiatable.swift */; };
0D85A8691FFC1D0F00A9C364 /* NibDesignable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D85A8681FFC1D0F00A9C364 /* NibDesignable.swift */; };
0DD8FC212144C91C0075D874 /* CaseIterable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DD8FC202144C91C0075D874 /* CaseIterable.swift */; };
759441951FF67998001E4FD1 /* CGFloat+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759441941FF67998001E4FD1 /* CGFloat+.swift */; };
759441971FF67BE4001E4FD1 /* Double+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759441961FF67BE4001E4FD1 /* Double+.swift */; };
759441991FF67C9E001E4FD1 /* Int+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 759441981FF67C9E001E4FD1 /* Int+.swift */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
0D2E0535203A4C5600F5AB3C /* StoryboardInstantiatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoryboardInstantiatable.swift; sourceTree = "<group>"; };
0D68994B1FFBC997004640CE /* NibInstantiatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NibInstantiatable.swift; sourceTree = "<group>"; };
0D85A8681FFC1D0F00A9C364 /* NibDesignable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NibDesignable.swift; sourceTree = "<group>"; };
0DD8FC202144C91C0075D874 /* CaseIterable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseIterable.swift; sourceTree = "<group>"; };
759441941FF67998001E4FD1 /* CGFloat+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+.swift"; sourceTree = "<group>"; };
759441961FF67BE4001E4FD1 /* Double+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+.swift"; sourceTree = "<group>"; };
759441981FF67C9E001E4FD1 /* Int+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -145,6 +147,7 @@
0D68994B1FFBC997004640CE /* NibInstantiatable.swift */,
0D85A8681FFC1D0F00A9C364 /* NibDesignable.swift */,
0D2E0535203A4C5600F5AB3C /* StoryboardInstantiatable.swift */,
0DD8FC202144C91C0075D874 /* CaseIterable.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -271,6 +274,7 @@
75E596121FF21D9500784269 /* DateFormetter+.swift in Sources */,
75E596181FF21D9500784269 /* UIVew+.swift in Sources */,
75E596211FF21FD200784269 /* Operator+.swift in Sources */,
0DD8FC212144C91C0075D874 /* CaseIterable.swift in Sources */,
75E5961F1FF21EB100784269 /* Bool+.swift in Sources */,
75E596161FF21D9500784269 /* UITextView+.swift in Sources */,
75E596131FF21D9500784269 /* NSObject+.swift in Sources */,
Expand Down

0 comments on commit 1aed1c3

Please sign in to comment.