Skip to content

Commit eeb53fd

Browse files
committed
Revert all unnecessary changes
There are a lot of things I changed which I could have avoided. To make Pull Request more readable, I preferred to revert them.
1 parent ed3a12b commit eeb53fd

File tree

4 files changed

+32
-58
lines changed

4 files changed

+32
-58
lines changed

Sources/NonEmpty/NonEmpty+Dictionary.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public protocol _DictionaryProtocol: Collection where Element == (key: Key, valu
1818
extension Dictionary: _DictionaryProtocol {}
1919

2020
extension NonEmpty where Collection: _DictionaryProtocol {
21-
@_disfavoredOverload
2221
public init(_ head: Element, _ tail: Collection) {
2322
guard !tail.keys.contains(head.key) else { fatalError("Dictionary contains duplicate key") }
2423
var tail = tail
@@ -88,13 +87,12 @@ extension NonEmpty where Collection: _DictionaryProtocol {
8887
}
8988
}
9089

91-
// Commented to avoid "Ambiguous use of operator '=='", doesn't break the tests.
92-
//extension NonEmpty where Collection: _DictionaryProtocol, Collection.Value: Equatable {
93-
// public static func == (lhs: NonEmpty, rhs: NonEmpty) -> Bool {
94-
// return Dictionary(uniqueKeysWithValues: Array(lhs))
95-
// == Dictionary(uniqueKeysWithValues: Array(rhs))
96-
// }
97-
//}
90+
extension NonEmpty where Collection: _DictionaryProtocol, Collection.Value: Equatable {
91+
public static func == (lhs: NonEmpty, rhs: NonEmpty) -> Bool {
92+
return Dictionary(uniqueKeysWithValues: Array(lhs))
93+
== Dictionary(uniqueKeysWithValues: Array(rhs))
94+
}
95+
}
9896

9997
extension NonEmpty where Collection: _DictionaryProtocol & ExpressibleByDictionaryLiteral {
10098
public init(_ head: Element) {

Sources/NonEmpty/NonEmpty+RangeReplaceableCollection.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ extension NonEmptyProtocol where Collection: RangeReplaceableCollection {
1515
}
1616

1717
public init?<S>(_ elements: S) where S: Sequence, Collection.Element == S.Element {
18-
try? self.init(from: Collection(elements))
18+
try? self.init(from: elements)
1919
}
20+
}
2021

22+
extension NonEmpty where Collection: RangeReplaceableCollection {
2123
public mutating func append(_ newElement: Element) {
22-
self.wrappedValue.append(newElement)
24+
self.rawValue.append(newElement)
2325
}
2426

2527
public mutating func append<S: Sequence>(contentsOf newElements: S) where Element == S.Element {
26-
self.wrappedValue.append(contentsOf: newElements)
28+
self.rawValue.append(contentsOf: newElements)
2729
}
2830

2931
public mutating func insert(_ newElement: Element, at i: Index) {
30-
self.wrappedValue.insert(newElement, at: i)
32+
self.rawValue.insert(newElement, at: i)
3133
}
3234

3335
public mutating func insert<S>(
3436
contentsOf newElements: S, at i: Index
3537
) where S: Swift.Collection, Element == S.Element {
36-
self.wrappedValue.insert(contentsOf: newElements, at: i)
38+
self.rawValue.insert(contentsOf: newElements, at: i)
3739
}
3840

3941
public static func += <S: Sequence>(lhs: inout Self, rhs: S) where Element == S.Element {

Sources/NonEmpty/NonEmpty.swift

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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

11087
extension 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+
144123
extension 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
}

Sources/NonEmpty/NonEmptyProtocol.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ public protocol WithMinimumCount {
55
}
66

77
public protocol NonEmptyProtocol: Swift.Collection, RawRepresentable, WithMinimumCount
8-
where RawValue: Swift.Collection,
9-
Element == RawValue.Element,
8+
where Element == RawValue.Element,
109
Index == RawValue.Index,
11-
Collection.Element == Element,
12-
Collection.Index == Index
10+
Collection == RawValue
1311
{
1412
associatedtype Collection: Swift.Collection
15-
var tail: Slice<Collection> { get }
16-
var wrappedValue: Collection { get set }
17-
init(from wrappedValue: Collection) throws
13+
init(from rawValue: Collection) throws
1814
}
1915

2016
extension NonEmptyProtocol {
21-
public var head: Element { self[self.startIndex] }
22-
public var first: Element { head }
17+
public var first: Element { self[self.startIndex] }
2318
}

0 commit comments

Comments
 (0)