Skip to content

Commit

Permalink
Merge pull request #37 from RougeWare/feature/Autoconformance
Browse files Browse the repository at this point in the history
Added auto-conformance to the 4 common synthesized protocols
  • Loading branch information
KyNorthstar authored Jun 6, 2022
2 parents 3cd0c1d + f92d9fe commit 360a35c
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
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)
}
}

0 comments on commit 360a35c

Please sign in to comment.