Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions Sources/Valkey/RESP/RESPBulkString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@
import NIOCore

/// Bulk string response from Valkey command
///
/// Bulk strings can store bytes, text, serialized objects and binary arrays.
/// RESPBulkString conforms to `RandomAccessCollection where Element == UInt8` allowing
/// readonly access to the contents of the bulk string.
///
/// You can also create a Swift String from a RESPBulkString using `String(_:)`.
/// ```
/// let bulkString = valkeyClient.get("myKey")
/// let string = bulkString.map { String($0) }
/// ```
///
/// Similarly if you want the bulk string bytes in the form of a SwiftNIO ByteBuffer
/// you can use the initializer `ByteBuffer(_:)`. This method returns the internal
/// buffer used by `RESPBulkString` so does not perform any copies.
/// ```
/// let bulkString = valkeyClient.get("myKey")
/// let buffer = bulkString.map { ByteBuffer($0) }
/// ```
public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollection {
@usableFromInline
let buffer: ByteBuffer
Expand Down Expand Up @@ -66,18 +84,6 @@ public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollect
RESPBulkString(buffer: self.buffer, range: range)
}

#if compiler(>=6.2)
/// Provides safe high-performance read-only access to the readable bytes of this buffer.
@inlinable
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
public var bytes: RawSpan {
@_lifetime(borrow self)
get {
self.buffer.readableBytesSpan
}
}
#endif

// These are implemented as no-ops for performance reasons. The range check will be performed
// when the slice is indexed with an index and not a range.
// See https://github.com/swiftlang/swift/blob/153dd02cd8709f8c6afcda5f173237320a3eec87/stdlib/public/core/Collection.swift#L638
Expand All @@ -93,7 +99,8 @@ public struct RESPBulkString: Sendable, Equatable, Hashable, RandomAccessCollect

#if compiler(>=6.2)
extension RESPBulkString {
public var span: RawSpan {
/// Provides high performance read only access to the contents of the RESPBulkString
public var bytes: RawSpan {
@_lifetime(borrow self)
borrowing get {
let span = self.buffer.readableBytesSpan
Expand All @@ -119,13 +126,20 @@ extension RESPBulkString: RESPStringRenderable {
}

extension String {
/// Initialize String from `RESPBulkString`
/// - Parameter bulkString: Source bulk string
@inlinable
public init(_ bulkString: RESPBulkString) {
self.init(buffer: bulkString.buffer)
}
}

extension ByteBuffer {
/// Initialize ByteBuffer from `RESPBulkString`
///
/// This method returns the internal buffer used by `RESPBulkString` so does not perform any copies.
///
/// - Parameter bulkString: Source bulk string
@inlinable
public init(_ bulkString: RESPBulkString) {
self = bulkString.buffer
Expand Down
2 changes: 2 additions & 0 deletions Sources/Valkey/ValkeyKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ extension ValkeyKey: ExpressibleByStringLiteral {
}

extension ValkeyKey {
/// Initialize ValkeyKey from RESPBulkString
/// - Parameter bulkString: bulkString
@inlinable
public init(_ bulkString: RESPBulkString) {
self = .init(bulkString.buffer)
Expand Down
Loading