forked from swift-server/swift-memcache-gsoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemcachedValue.swift
75 lines (67 loc) · 2.76 KB
/
MemcachedValue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-memcache-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
/// Protocol defining the requirements for a type that can be converted to a ByteBuffer for transmission to Memcached.
public protocol MemcachedValue {
/// Writes the value to a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer to which the value should be written.
func writeToBuffer(_ buffer: inout ByteBuffer)
/// Reads the type from a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer from which the value should be read.
static func readFromBuffer(_ buffer: inout ByteBuffer) -> Self?
}
/// Extension for FixedWidthInteger types to conform to MemcachedValue.
extension MemcachedValue where Self: FixedWidthInteger {
/// Writes the integer to a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer to which the integer should be written.
public func writeToBuffer(_ buffer: inout ByteBuffer) {
buffer.writeIntegerAsASCII(self)
}
/// Reads a FixedWidthInteger from a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer from which the value should be read.
public static func readFromBuffer(_ buffer: inout ByteBuffer) -> Self? {
return buffer.readIntegerFromASCII()
}
}
/// Extension for StringProtocol types to conform to MemcachedValue.
extension MemcachedValue where Self: StringProtocol {
/// Writes the string to a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer to which the string should be written.
public func writeToBuffer(_ buffer: inout ByteBuffer) {
buffer.writeString(String(self))
}
/// Reads a String from a ByteBuffer.
///
/// - Parameter buffer: The ByteBuffer from which the value should be read.
public static func readFromBuffer(_ buffer: inout ByteBuffer) -> Self? {
return buffer.readString(length: buffer.readableBytes) as? Self
}
}
/// MemcachedValue conformance to several standard Swift types.
extension Int: MemcachedValue {}
extension Int8: MemcachedValue {}
extension Int16: MemcachedValue {}
extension Int32: MemcachedValue {}
extension Int64: MemcachedValue {}
extension UInt: MemcachedValue {}
extension UInt8: MemcachedValue {}
extension UInt16: MemcachedValue {}
extension UInt32: MemcachedValue {}
extension UInt64: MemcachedValue {}
extension String: MemcachedValue {}