Skip to content

Commit 360a35c

Browse files
authored
Merge pull request #37 from RougeWare/feature/Autoconformance
Added auto-conformance to the 4 common synthesized protocols
2 parents 3cd0c1d + f92d9fe commit 360a35c

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ A few ways to have a lazily-initialized value in Swift 5.1. Note that, if you ar
1414

1515

1616

17+
# Automatic Conformance #
18+
19+
The built-in containers (`Lazy`, `ResettableLazy`, and `FunctionalLazy`) automatically conform to `Equatable`, `Hashable`, `Encodable`, and `Decodable` when their values conform do too! This is a passthrough conformance, simply calling the functions of the wrapped value.
20+
21+
Keep in mind, though, that in order to do this, the value is automatically initialized and accessed!
22+
23+
24+
1725
# Compatibility Notice #
1826

1927
The entire repository structure had to be changed in order to be compatible with Swift Package Manager ([#4](https://github.com/RougeWare/Swift-Lazy-Patterns/issues/4)). Because of this, the API version changed from 2.0.0 to 3.0.0. Very little of the actual API changed along with this ([#8](https://github.com/RougeWare/Swift-Lazy-Patterns/issues/8)); it was almost entirely to service Swift Package manager.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// LazyContainer + Equatable.swift
3+
//
4+
//
5+
// Created by Ky on 2022-06-03.
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
// MARK: - Encodable
13+
14+
public extension LazyContainer where Self: Encodable, Value: Encodable {
15+
func encode(to encoder: Encoder) throws {
16+
try wrappedValue.encode(to: encoder)
17+
}
18+
}
19+
20+
21+
22+
extension Lazy: Encodable where Value: Encodable {}
23+
extension ResettableLazy: Encodable where Value: Encodable {}
24+
extension FunctionalLazy: Encodable where Value: Encodable {}
25+
26+
27+
28+
// MARK: - Decodable
29+
30+
public extension LazyContainer where Self: Decodable, Value: Decodable {
31+
init(from decoder: Decoder) throws {
32+
self = .preinitialized(try Value(from: decoder))
33+
}
34+
}
35+
36+
37+
38+
extension Lazy: Decodable where Value: Decodable {}
39+
extension ResettableLazy: Decodable where Value: Decodable {}
40+
extension FunctionalLazy: Decodable where Value: Decodable {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// LazyContainer + Equatable.swift
3+
//
4+
//
5+
// Created by Ky on 2022-06-03.
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public extension LazyContainer where Self: Equatable, Value: Equatable {
13+
static func == (lhs: Self, rhs: Self) -> Bool {
14+
lhs.wrappedValue == rhs.wrappedValue
15+
}
16+
17+
18+
static func == (lhs: Self, rhs: Value) -> Bool {
19+
lhs.wrappedValue == rhs
20+
}
21+
22+
23+
static func == (lhs: Value, rhs: Self) -> Bool {
24+
lhs == rhs.wrappedValue
25+
}
26+
}
27+
28+
29+
30+
extension Lazy: Equatable where Value: Equatable {}
31+
extension ResettableLazy: Equatable where Value: Equatable {}
32+
extension FunctionalLazy: Equatable where Value: Equatable {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// LazyContainer + Hashable.swift
3+
//
4+
//
5+
// Created by Ky on 2022-06-03.
6+
//
7+
8+
import Foundation
9+
10+
11+
12+
public extension LazyContainer where Self: Hashable, Value: Hashable {
13+
func hash(into hasher: inout Hasher) {
14+
wrappedValue.hash(into: &hasher)
15+
}
16+
}
17+
18+
19+
20+
extension Lazy: Hashable where Value: Hashable {}
21+
extension ResettableLazy: Hashable where Value: Hashable {}
22+
extension FunctionalLazy: Hashable where Value: Hashable {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// LazyContainer + Hashable tests.swift
3+
//
4+
//
5+
// Created by S🌟System on 2022-06-03.
6+
//
7+
8+
import XCTest
9+
10+
import LazyContainers
11+
12+
13+
14+
final class LazyContainer_Codable_tests: XCTestCase {
15+
16+
func testHashableConformance() {
17+
18+
struct Test: Codable {
19+
20+
@Lazy(initializer: { 42 })
21+
var lazyInt
22+
23+
@FunctionalLazy(initializer: { CGFloat.pi })
24+
var lazyFloat
25+
26+
@ResettableLazy(initializer: { "foobar" })
27+
var lazyString
28+
}
29+
30+
31+
32+
let encoder = JSONEncoder()
33+
encoder.outputFormatting = .sortedKeys
34+
35+
XCTAssertEqual(String(data: try encoder.encode(Test()), encoding: .utf8),
36+
#"{"lazyFloat":3.1415926535897931,"lazyInt":42,"lazyString":"foobar"}"#)
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// LazyContainer + Equatable tests.swift
3+
//
4+
//
5+
// Created by S🌟System on 2022-06-03.
6+
//
7+
8+
import XCTest
9+
10+
import LazyContainers
11+
12+
13+
14+
final class LazyContainer_Equatable_tests: XCTestCase {
15+
16+
func testEquatableConformance() {
17+
18+
struct Test: Equatable {
19+
20+
@Lazy(initializer: { 42 })
21+
var lazyInt
22+
23+
@FunctionalLazy(initializer: { CGFloat.pi })
24+
var lazyFloat
25+
26+
@ResettableLazy(initializer: { "foobar" })
27+
var lazyString
28+
}
29+
30+
XCTAssertEqual(Test(), Test())
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// LazyContainer + Hashable tests.swift
3+
//
4+
//
5+
// Created by S🌟System on 2022-06-03.
6+
//
7+
8+
import XCTest
9+
10+
import LazyContainers
11+
12+
13+
14+
final class LazyContainer_Hashable_tests: XCTestCase {
15+
16+
func testHashableConformance() {
17+
18+
struct Test: Hashable {
19+
20+
@Lazy(initializer: { 42 })
21+
var lazyInt
22+
23+
@FunctionalLazy(initializer: { CGFloat.pi })
24+
var lazyFloat
25+
26+
@ResettableLazy(initializer: { "foobar" })
27+
var lazyString
28+
}
29+
30+
XCTAssertEqual(Test().hashValue, Test().hashValue)
31+
}
32+
}

0 commit comments

Comments
 (0)