11@dynamicMemberLookup
2- public struct NonEmpty < Collection: Swift . Collection > {
3- public internal( set) var tail : Slice < Collection >
2+ public struct NonEmpty < Collection: Swift . Collection > : Swift . Collection , NonEmptyProtocol {
3+ public typealias Element = Collection . Element
4+ public typealias Index = Collection . Index
5+
6+ public internal( set) var rawValue : Collection
47
58 public static var minimumCount : Int {
69 if let T = Collection . self as? WithMinimumCount . Type {
@@ -10,50 +13,24 @@ public struct NonEmpty<Collection: Swift.Collection> {
1013 }
1114 }
1215
13- public init ( fromSlice slice : Slice < Collection > ) throws {
14- guard !slice . dropFirst ( Self . minimumCount - 1 ) . isEmpty else {
16+ public init ( from rawValue : Collection ) throws {
17+ guard !rawValue . dropFirst ( Self . minimumCount - 1 ) . isEmpty else {
1518 if Self . minimumCount == 1 {
1619 throw NonEmptyError . emptyCollection
1720 } else {
1821 throw NonEmptyError . tooFewElements ( expected: Self . minimumCount)
1922 }
2023 }
21- let bounds = ( slice. index ( after: slice. startIndex) ) ..< slice. endIndex
22- self . tail = Slice ( base: slice. base, bounds: bounds)
24+ self . rawValue = rawValue
2325 }
2426
25- public init ( from wrappedValue: Collection ) throws {
26- let slice = Slice ( base: wrappedValue, bounds: wrappedValue. startIndex..< wrappedValue. endIndex)
27- try self . init ( fromSlice: slice)
27+ public init ? ( rawValue: Collection ) {
28+ try ? self . init ( from: rawValue)
2829 }
2930
3031 public subscript< Subject> ( dynamicMember keyPath: KeyPath < Collection , Subject > ) -> Subject {
3132 self . rawValue [ keyPath: keyPath]
3233 }
33- }
34-
35- extension NonEmpty : NonEmptyProtocol {
36- public var wrappedValue : Collection {
37- get { self . rawValue }
38- set { self . rawValue = newValue }
39- }
40- }
41-
42- extension NonEmpty : RawRepresentable {
43- public internal( set) var rawValue : Collection {
44- get { tail. base }
45- set {
46- self = try ! Self . init ( from: newValue)
47- }
48- }
49- public init ? ( rawValue: Collection ) {
50- try ? self . init ( from: rawValue)
51- }
52- }
53-
54- extension NonEmpty : Swift . Collection {
55- public typealias Element = RawValue . Element
56- public typealias Index = RawValue . Index
5734
5835 public var startIndex : Index { self . rawValue. startIndex }
5936
@@ -109,7 +86,7 @@ extension NonEmpty: Swift.Collection {
10986
11087extension NonEmpty : CustomStringConvertible {
11188 public var description : String {
112- String ( describing: self . rawValue)
89+ return String ( describing: self . rawValue)
11390 }
11491}
11592
@@ -141,6 +118,8 @@ extension NonEmpty: Decodable where Collection: Decodable {
141118 }
142119}
143120
121+ extension NonEmpty : RawRepresentable { }
122+
144123extension NonEmpty where Collection. Element: Comparable {
145124 public func max( ) -> Element {
146125 self . rawValue. max ( ) !
@@ -155,10 +134,10 @@ extension NonEmpty where Collection.Element: Comparable {
155134 }
156135}
157136
158- extension NonEmpty : BidirectionalCollection where RawValue : BidirectionalCollection {
137+ extension NonEmpty : BidirectionalCollection where Collection : BidirectionalCollection {
159138 public var last : Element { self . rawValue. last! }
160139}
161- extension NonEmptyProtocol where RawValue : BidirectionalCollection {
140+ extension NonEmptyProtocol where Collection : BidirectionalCollection {
162141 public func index( before i: Index ) -> Index {
163142 self . rawValue. index ( before: i)
164143 }
0 commit comments