Last active
July 6, 2020 14:45
-
-
Save Pircate/3352b850782365b0d9d67c9b8791feea to your computer and use it in GitHub Desktop.
ReferenceBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReferenceBox<Value: Codable>: Codable { | |
public var value: Value | |
required public init(from decoder: Decoder) throws { | |
let container = try decoder.singleValueContainer() | |
self.value = try container.decode(Value.self) | |
} | |
public func encode(to encoder: Encoder) throws { | |
var container = encoder.singleValueContainer() | |
try container.encode(value) | |
} | |
public init(_ value: Value) { | |
self.value = value | |
} | |
public required init(booleanLiteral value: Value) { | |
self.value = value | |
} | |
public required init(integerLiteral value: Value) { | |
self.value = value | |
} | |
public required init(floatLiteral value: Value) { | |
self.value = value | |
} | |
public required init(stringLiteral value: Value) { | |
self.value = value | |
} | |
} | |
extension ReferenceBox: Equatable where Value: Equatable { | |
public static func == (lhs: ReferenceBox<Value>, rhs: ReferenceBox<Value>) -> Bool { | |
return lhs.value == rhs.value | |
} | |
} | |
extension ReferenceBox: ExpressibleByBooleanLiteral where Value == Bool { | |
public typealias BooleanLiteralType = Value | |
} | |
extension ReferenceBox: ExpressibleByIntegerLiteral where Value == Int { | |
public typealias IntegerLiteralType = Value | |
} | |
extension ReferenceBox: ExpressibleByFloatLiteral where Value == Float { | |
public typealias FloatLiteralType = Value | |
} | |
extension ReferenceBox: ExpressibleByUnicodeScalarLiteral where Value == String { | |
public typealias UnicodeScalarLiteralType = Value | |
} | |
extension ReferenceBox: ExpressibleByExtendedGraphemeClusterLiteral where Value == String { | |
public typealias ExtendedGraphemeClusterLiteralType = Value | |
} | |
extension ReferenceBox: ExpressibleByStringLiteral where Value == String { | |
public typealias StringLiteralType = Value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment