Skip to content

Commit 17fa5da

Browse files
committed
Cleanup Extensions by placing code in more appropriate places
1 parent f2fb726 commit 17fa5da

File tree

3 files changed

+92
-86
lines changed

3 files changed

+92
-86
lines changed

Sources/RediStack/Extensions/StandardLibrary.swift

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
extension Array where Element == RESPValue {
16-
/// Converts the collection of `RESPValueConvertible` elements and appends them to the end of the array.
17-
/// - Note: This method guarantees that only one storage expansion will happen to copy the elements.
18-
/// - Parameters elementsToCopy: The collection of elements to convert to `RESPValue` and append to the array.
19-
public mutating func append<ValueCollection>(convertingContentsOf elementsToCopy: ValueCollection)
20-
where
21-
ValueCollection: Collection,
22-
ValueCollection.Element: RESPValueConvertible
23-
{
24-
guard elementsToCopy.count > 0 else { return }
25-
26-
self.reserveCapacity(self.count + elementsToCopy.count)
27-
elementsToCopy.forEach { self.append($0.convertedToRESPValue()) }
28-
}
29-
30-
/// Adds the elements of a collection to this array, delegating the details of how they are added to the given closure.
31-
///
32-
/// When your closure will be doing more than a simple transform of the element value, such as when you're adding both the key _and_ value from a `KeyValuePair`,
33-
/// you should set the `overestimatedCountBeingAdded` to a value you do not expect to exceed in order to prevent multiple allocations from the increasing
34-
/// element count.
35-
///
36-
/// For example:
37-
///
38-
/// let pairs = [
39-
/// "MyID": 30,
40-
/// "YourID": 31
41-
/// ]
42-
/// var values: [RESPValue] = []
43-
/// values.add(contentsOf: pairs, overestimatedCountBeingAdded: pairs.count * 2) { (array, element) in
44-
/// // element is a (key, value) tuple
45-
/// array.append(element.0.convertedToRESPValue())
46-
/// array.append(element.1.convertedToRESPValue())
47-
/// }
48-
///
49-
/// However, if you just want to apply a transform, you can do that more similarly to a call to the `reduce` methods:
50-
///
51-
/// let valuesToConvert = [...] // some collection of non-`RESPValueConvertible` elements, such as third-party types
52-
/// let values: [RESPValue] = []
53-
/// values.add(contentsOf: valuesToConvert) { (array, element) in
54-
/// // your transform and insert/append implementation
55-
/// }
56-
///
57-
/// If the `elementsToCopy` has no elements, the `closure` is never called.
58-
///
59-
/// - Parameters:
60-
/// - elementsToCopy: The collection of elements that will be added to the array in the closure.
61-
/// - overestimatedCountBeingAdded: The number of elements that will be added to the array.
62-
/// If no value is provided, the size of the collection being copied will be used.
63-
/// - closure: A closure left to define how the collection's element should be added into the array.
64-
public mutating func add<ValueCollection: Collection>(
65-
contentsOf elementsToCopy: ValueCollection,
66-
overestimatedCountBeingAdded: Int? = nil,
67-
_ closure: (inout [RESPValue], ValueCollection.Element) -> Void
68-
) {
69-
guard elementsToCopy.count > 0 else { return }
70-
71-
let sizeToAdd = overestimatedCountBeingAdded ?? elementsToCopy.count
72-
self.reserveCapacity(self.count + sizeToAdd)
73-
74-
elementsToCopy.forEach { closure(&self, $0) }
75-
}
76-
}
77-
7815
extension Array {
7916
/// Initializes an empty array, reserving the desired `initialCapacity`.
8017
/// - Parameter initialCapacity: The desired element size the array should start with.

Sources/RediStack/Extensions/SwiftNIO.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,6 @@
1414

1515
import NIO
1616

17-
extension EventLoopFuture where Value == RESPValue {
18-
/// Attempts to convert the `RESPValue` to the desired `RESPValueConvertible` type.
19-
/// If the `RESPValueConvertible.init(_:)` returns `nil`, then the `EventLoopFuture` will fail.
20-
/// - Parameter to: The desired type to convert to.
21-
/// - Returns: An `EventLoopFuture` that resolves a value of the desired type.
22-
@inlinable
23-
public func convertFromRESPValue<T>(
24-
to type: T.Type = T.self,
25-
file: StaticString = #function,
26-
function: StaticString = #function,
27-
line: UInt = #line
28-
)
29-
-> EventLoopFuture<T> where T: RESPValueConvertible
30-
{
31-
return self.flatMapThrowing {
32-
guard let value = T(fromRESP: $0) else {
33-
throw RedisClientError.failedRESPConversion(to: type)
34-
}
35-
return value
36-
}
37-
}
38-
}
39-
4017
extension TimeAmount {
4118
/// The seconds representation of the TimeAmount.
4219
@usableFromInline

Sources/RediStack/RESP/RESPValue.swift

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,95 @@ extension RESPValue: RESPValueConvertible {
162162
return self
163163
}
164164
}
165+
166+
// MARK: EventLoopFuture Extensions
167+
168+
import NIO
169+
170+
extension EventLoopFuture where Value == RESPValue {
171+
/// Attempts to convert the `RESPValue` to the desired `RESPValueConvertible` type.
172+
/// If the `RESPValueConvertible.init(_:)` returns `nil`, then the `EventLoopFuture` will fail.
173+
/// - Parameter to: The desired type to convert to.
174+
/// - Returns: An `EventLoopFuture` that resolves a value of the desired type.
175+
@inlinable
176+
public func convertFromRESPValue<T>(
177+
to type: T.Type = T.self,
178+
file: StaticString = #function,
179+
function: StaticString = #function,
180+
line: UInt = #line
181+
)
182+
-> EventLoopFuture<T> where T: RESPValueConvertible
183+
{
184+
return self.flatMapThrowing {
185+
guard let value = T(fromRESP: $0) else {
186+
throw RedisClientError.failedRESPConversion(to: type)
187+
}
188+
return value
189+
}
190+
}
191+
}
192+
193+
// MARK: RESPValue Collections
194+
195+
extension RangeReplaceableCollection where Element == RESPValue {
196+
/// Converts the collection of `RESPValueConvertible` elements and appends them to the end of the array.
197+
/// - Note: This method guarantees that only one storage expansion will happen to copy the elements.
198+
/// - Parameters elementsToCopy: The collection of elements to convert to `RESPValue` and append to the array.
199+
public mutating func append<ValueCollection>(convertingContentsOf elementsToCopy: ValueCollection)
200+
where
201+
ValueCollection: Collection,
202+
ValueCollection.Element: RESPValueConvertible
203+
{
204+
guard elementsToCopy.count > 0 else { return }
205+
206+
self.reserveCapacity(self.count + elementsToCopy.count)
207+
elementsToCopy.forEach { self.append($0.convertedToRESPValue()) }
208+
}
209+
210+
/// Adds the elements of a collection to this array, delegating the details of how they are added to the given closure.
211+
///
212+
/// When your closure will be doing more than a simple transform of the element value, such as when you're adding both the key _and_ value from a `KeyValuePair`,
213+
/// you should set the `overestimatedCountBeingAdded` to a value you do not expect to exceed in order to prevent multiple allocations from the increasing
214+
/// element count.
215+
///
216+
/// For example:
217+
///
218+
/// let pairs = [
219+
/// "MyID": 30,
220+
/// "YourID": 31
221+
/// ]
222+
/// var values: [RESPValue] = []
223+
/// values.add(contentsOf: pairs, overestimatedCountBeingAdded: pairs.count * 2) { (array, element) in
224+
/// // element is a (key, value) tuple
225+
/// array.append(element.0.convertedToRESPValue())
226+
/// array.append(element.1.convertedToRESPValue())
227+
/// }
228+
///
229+
/// However, if you just want to apply a transform, you can do that more similarly to a call to the `reduce` methods:
230+
///
231+
/// let valuesToConvert = [...] // some collection of non-`RESPValueConvertible` elements, such as third-party types
232+
/// let values: [RESPValue] = []
233+
/// values.add(contentsOf: valuesToConvert) { (array, element) in
234+
/// // your transform and insert/append implementation
235+
/// }
236+
///
237+
/// If the `elementsToCopy` has no elements, the `closure` is never called.
238+
///
239+
/// - Parameters:
240+
/// - elementsToCopy: The collection of elements that will be added to the array in the closure.
241+
/// - overestimatedCountBeingAdded: The number of elements that will be added to the array.
242+
/// If no value is provided, the size of the collection being copied will be used.
243+
/// - closure: A closure left to define how the collection's element should be added into the array.
244+
public mutating func add<ValueCollection: Collection>(
245+
contentsOf elementsToCopy: ValueCollection,
246+
overestimatedCountBeingAdded: Int? = nil,
247+
_ closure: (inout Self, ValueCollection.Element) -> Void
248+
) {
249+
guard elementsToCopy.count > 0 else { return }
250+
251+
let sizeToAdd = overestimatedCountBeingAdded ?? elementsToCopy.count
252+
self.reserveCapacity(self.count + sizeToAdd)
253+
254+
elementsToCopy.forEach { closure(&self, $0) }
255+
}
256+
}

0 commit comments

Comments
 (0)