Skip to content

Commit ab3d954

Browse files
authored
RESPBulkString comment and cleanup (#276)
* RESPBulkString comment Signed-off-by: Adam Fowler <[email protected]> * Minor comment change Signed-off-by: Adam Fowler <[email protected]> --------- Signed-off-by: Adam Fowler <[email protected]>
1 parent eb11a5c commit ab3d954

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Sources/Valkey/RESP/RESPBulkString.swift

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88
import NIOCore
99

1010
/// Bulk string response from Valkey command
11+
///
12+
/// Bulk strings can store bytes, text, serialized objects and binary arrays.
13+
/// RESPBulkString conforms to `RandomAccessCollection where Element == UInt8` allowing
14+
/// readonly access to the contents of the bulk string.
15+
///
16+
/// You can also create a Swift String from a RESPBulkString using `String(_:)`.
17+
/// ```
18+
/// let bulkString = valkeyClient.get("myKey")
19+
/// let string = bulkString.map { String($0) }
20+
/// ```
21+
///
22+
/// Similarly if you want the bulk string bytes in the form of a SwiftNIO ByteBuffer
23+
/// you can use the initializer `ByteBuffer(_:)`. This method returns the internal
24+
/// buffer used by `RESPBulkString` so does not perform any copies.
25+
/// ```
26+
/// let bulkString = valkeyClient.get("myKey")
27+
/// let buffer = bulkString.map { ByteBuffer($0) }
28+
/// ```
1129
public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollection {
1230
@usableFromInline
1331
let buffer: ByteBuffer
@@ -66,18 +84,6 @@ public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollect
6684
RESPBulkString(buffer: self.buffer, range: range)
6785
}
6886

69-
#if compiler(>=6.2)
70-
/// Provides safe high-performance read-only access to the readable bytes of this buffer.
71-
@inlinable
72-
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
73-
public var bytes: RawSpan {
74-
@_lifetime(borrow self)
75-
get {
76-
self.buffer.readableBytesSpan
77-
}
78-
}
79-
#endif
80-
8187
// These are implemented as no-ops for performance reasons. The range check will be performed
8288
// when the slice is indexed with an index and not a range.
8389
// See https://github.com/swiftlang/swift/blob/153dd02cd8709f8c6afcda5f173237320a3eec87/stdlib/public/core/Collection.swift#L638
@@ -93,7 +99,8 @@ public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollect
9399

94100
#if compiler(>=6.2)
95101
extension RESPBulkString {
96-
public var span: RawSpan {
102+
/// Provides high performance read only access to the contents of the RESPBulkString
103+
public var bytes: RawSpan {
97104
@_lifetime(borrow self)
98105
borrowing get {
99106
let span = self.buffer.readableBytesSpan
@@ -119,13 +126,20 @@ extension RESPBulkString: RESPStringRenderable {
119126
}
120127

121128
extension String {
129+
/// Initialize String from `RESPBulkString`
130+
/// - Parameter bulkString: Source bulk string
122131
@inlinable
123132
public init(_ bulkString: RESPBulkString) {
124133
self.init(buffer: bulkString.buffer)
125134
}
126135
}
127136

128137
extension ByteBuffer {
138+
/// Initialize ByteBuffer from `RESPBulkString`
139+
///
140+
/// This method returns the internal buffer used by `RESPBulkString` so does not perform any copies.
141+
///
142+
/// - Parameter bulkString: Source bulk string
129143
@inlinable
130144
public init(_ bulkString: RESPBulkString) {
131145
self = bulkString.buffer

Sources/Valkey/ValkeyKey.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ extension ValkeyKey: ExpressibleByStringLiteral {
100100
}
101101

102102
extension ValkeyKey {
103+
/// Initialize ValkeyKey from RESPBulkString
104+
/// - Parameter bulkString: bulkString
103105
@inlinable
104106
public init(_ bulkString: RESPBulkString) {
105107
self = .init(bulkString.buffer)

0 commit comments

Comments
 (0)