Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# master
*Please add new entries at the top.*
1. Add `_modify` accessor to `Atomic.value` (#731, kudos to @p4checo)

# 6.0.0
1. Dropped support for Swift 4.2 (Xcode 9)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ public final class Atomic<Value> {
set(newValue) {
swap(newValue)
}

_modify {
lock.lock()
defer { lock.unlock() }

yield &_value
}
}

/// Initialize the variable with the given initial value.
Expand Down
5 changes: 4 additions & 1 deletion Tests/ReactiveSwiftTests/AtomicSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class AtomicSpec: QuickSpec {
atomic = Atomic(1)
}

it("should read and write the value directly") {
it("should read, write and modify the value directly") {
expect(atomic.value) == 1

atomic.value = 2
expect(atomic.value) == 2

atomic.value += 1
expect(atomic.value) == 3
}

it("should swap the value atomically") {
Expand Down