diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da6aa9bb..e66d72339 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **Breaking**: Removed deprecated methods - **Breaking**: The in-memory `DatabaseQueue()` initializer can now throw errors. - **Breaking**: The `selectID()` method is replaced with `selectPrimaryKey(as:)`. +- **Breaking**: `Cursor.isEmpty` is now a throwing property, instead of a method. - **New**: Request protocols and cursors now define primary associated types, enabled by [SE-0346](https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md). ## 5.26.0 diff --git a/Documentation/GRDB6MigrationGuide.md b/Documentation/GRDB6MigrationGuide.md index be65e86f3..0f97058d8 100644 --- a/Documentation/GRDB6MigrationGuide.md +++ b/Documentation/GRDB6MigrationGuide.md @@ -60,3 +60,10 @@ The `Cursor` protocol has also gained a primary associated type (the type of its } } ``` + +- `Cursor.isEmpty` is now a throwing property, instead of a method: + + ```diff + -if try cursor.isEmpty() { ... } + +if try cursor.isEmpty { ... } + ``` diff --git a/GRDB/Core/Cursor.swift b/GRDB/Core/Cursor.swift index 6b65a64ab..bba2f6df7 100644 --- a/GRDB/Core/Cursor.swift +++ b/GRDB/Core/Cursor.swift @@ -243,10 +243,12 @@ public protocol Cursor: AnyObject { } extension Cursor { - /// Returns a Boolean value indicating whether the cursor contains - /// an element. - public func isEmpty() throws -> Bool { - try next() == nil + /// Returns a Boolean value indicating whether the cursor does not contain + /// any element. + /// + /// - important: This property may consume elements. + public var isEmpty: Bool { + get throws { try next() == nil } } /// Returns a Boolean value indicating whether the cursor contains an diff --git a/Tests/GRDBTests/CursorTests.swift b/Tests/GRDBTests/CursorTests.swift index 3174db7b7..19ad7206b 100644 --- a/Tests/GRDBTests/CursorTests.swift +++ b/Tests/GRDBTests/CursorTests.swift @@ -5,13 +5,20 @@ private struct TestError : Error { } class CursorTests: GRDBTestCase { + func testIsEmpty() { + XCTAssertTrue(try AnyCursor([Int]()).isEmpty) + XCTAssertFalse(try AnyCursor([1]).isEmpty) + } + func testContainsEquatable() { XCTAssertTrue(try AnyCursor([1, 2]).contains(1)) + XCTAssertTrue(try AnyCursor([1, 2]).contains(2)) XCTAssertFalse(try AnyCursor([1, 2]).contains(3)) } func testContainsClosure() { XCTAssertTrue(try AnyCursor([1, 2]).contains { $0 == 1 }) + XCTAssertTrue(try AnyCursor([1, 2]).contains { $0 == 2 }) XCTAssertFalse(try AnyCursor([1, 2]).contains { $0 == 3 }) do { _ = try AnyCursor([1, 2]).contains { _ -> Bool in throw TestError() }