Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto-conformance to the 4 common synthesized protocols #37

Merged
merged 3 commits into from
Jun 6, 2022
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ A few ways to have a lazily-initialized value in Swift 5.1. Note that, if you ar



# Automatic Conformance #

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.

Keep in mind, though, that in order to do this, the value is automatically initialized and accessed!



# Compatibility Notice #

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.
Expand Down
40 changes: 40 additions & 0 deletions Sources/LazyContainers/LazyContainer + Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// LazyContainer + Equatable.swift
//
//
// Created by Ky on 2022-06-03.
//

import Foundation



// MARK: - Encodable

public extension LazyContainer where Self: Encodable, Value: Encodable {
func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}



extension Lazy: Encodable where Value: Encodable {}
extension ResettableLazy: Encodable where Value: Encodable {}
extension FunctionalLazy: Encodable where Value: Encodable {}



// MARK: - Decodable

public extension LazyContainer where Self: Decodable, Value: Decodable {
init(from decoder: Decoder) throws {
self = .preinitialized(try Value(from: decoder))
}
}



extension Lazy: Decodable where Value: Decodable {}
extension ResettableLazy: Decodable where Value: Decodable {}
extension FunctionalLazy: Decodable where Value: Decodable {}
32 changes: 32 additions & 0 deletions Sources/LazyContainers/LazyContainer + Equatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// LazyContainer + Equatable.swift
//
//
// Created by Ky on 2022-06-03.
//

import Foundation



public extension LazyContainer where Self: Equatable, Value: Equatable {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.wrappedValue == rhs.wrappedValue
}


static func == (lhs: Self, rhs: Value) -> Bool {
lhs.wrappedValue == rhs
}


static func == (lhs: Value, rhs: Self) -> Bool {
lhs == rhs.wrappedValue
}
}



extension Lazy: Equatable where Value: Equatable {}
extension ResettableLazy: Equatable where Value: Equatable {}
extension FunctionalLazy: Equatable where Value: Equatable {}
22 changes: 22 additions & 0 deletions Sources/LazyContainers/LazyContainer + Hashable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// LazyContainer + Hashable.swift
//
//
// Created by Ky on 2022-06-03.
//

import Foundation



public extension LazyContainer where Self: Hashable, Value: Hashable {
func hash(into hasher: inout Hasher) {
wrappedValue.hash(into: &hasher)
}
}



extension Lazy: Hashable where Value: Hashable {}
extension ResettableLazy: Hashable where Value: Hashable {}
extension FunctionalLazy: Hashable where Value: Hashable {}
38 changes: 38 additions & 0 deletions Tests/LazyContainersTests/LazyContainer + Codable tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// LazyContainer + Hashable tests.swift
//
//
// Created by S🌟System on 2022-06-03.
//

import XCTest

import LazyContainers



final class LazyContainer_Codable_tests: XCTestCase {

func testHashableConformance() {

struct Test: Codable {

@Lazy(initializer: { 42 })
var lazyInt

@FunctionalLazy(initializer: { CGFloat.pi })
var lazyFloat

@ResettableLazy(initializer: { "foobar" })
var lazyString
}



let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys

XCTAssertEqual(String(data: try encoder.encode(Test()), encoding: .utf8),
#"{"lazyFloat":3.1415926535897931,"lazyInt":42,"lazyString":"foobar"}"#)
}
}
32 changes: 32 additions & 0 deletions Tests/LazyContainersTests/LazyContainer + Equatable tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// LazyContainer + Equatable tests.swift
//
//
// Created by S🌟System on 2022-06-03.
//

import XCTest

import LazyContainers



final class LazyContainer_Equatable_tests: XCTestCase {

func testEquatableConformance() {

struct Test: Equatable {

@Lazy(initializer: { 42 })
var lazyInt

@FunctionalLazy(initializer: { CGFloat.pi })
var lazyFloat

@ResettableLazy(initializer: { "foobar" })
var lazyString
}

XCTAssertEqual(Test(), Test())
}
}
32 changes: 32 additions & 0 deletions Tests/LazyContainersTests/LazyContainer + Hashable tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// LazyContainer + Hashable tests.swift
//
//
// Created by S🌟System on 2022-06-03.
//

import XCTest

import LazyContainers



final class LazyContainer_Hashable_tests: XCTestCase {

func testHashableConformance() {

struct Test: Hashable {

@Lazy(initializer: { 42 })
var lazyInt

@FunctionalLazy(initializer: { CGFloat.pi })
var lazyFloat

@ResettableLazy(initializer: { "foobar" })
var lazyString
}

XCTAssertEqual(Test().hashValue, Test().hashValue)
}
}