Skip to content

Thread Safety On Macro and Property Wrappers#24

Merged
hainayanda merged 7 commits into
mainfrom
feature/thread-safety
Jun 16, 2025
Merged

Thread Safety On Macro and Property Wrappers#24
hainayanda merged 7 commits into
mainfrom
feature/thread-safety

Conversation

@hainayanda
Copy link
Copy Markdown
Owner

@hainayanda hainayanda commented Jun 16, 2025

This pull request introduces significant changes to the SwiftEnvironment library, focusing on concurrency safety, simplifying macro usage, and updating documentation. Key updates include the introduction of atomic access methods for thread safety, the removal of the StaticModifier enum, and updates to tests and documentation to reflect these changes.

Concurrency and Thread Safety

  • Added a private accessQueue with concurrent attributes in GlobalEnvironment.swift to ensure thread-safe access to global values. Introduced atomicRead and atomicWrite methods for safe concurrent reads and writes. (Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift: [1] [2] [3] [4]
  • Made GlobalValues.atomicRead publicly accessible for broader use. (Sources/SwiftEnvironment/Environment/GlobalValues.swift: Sources/SwiftEnvironment/Environment/GlobalValues.swiftL91-R91)

Macro Simplification

  • Removed the StaticModifier enum and its associated logic for isolated/nonisolated modifiers. Simplified the GlobalEntry macro by introducing a new ___ValueWrapper struct to handle value isolation. (Sources/SwiftEnvironment/Macros.swift: [1] Sources/SwiftEnvironmentMacro/Macro/GlobalEntryMacro.swift: [2] [3]

Documentation Updates

  • Updated the README.md to reflect the new version of the library (4.1.4) and removed outdated examples related to the StaticModifier enum. (README.md: [1] [2] [3]

Test Updates

  • Updated tests to align with the new macro behavior and removed tests related to the StaticModifier enum. (Tests/SwiftEnvironmentTests/DummyDependency.swift: [1] Tests/SwiftEnvironmentTests/MacroTests.swift: [2] [3]

Dependency Updates

@hainayanda hainayanda requested a review from Copilot June 16, 2025 02:34

This comment was marked as outdated.

@hainayanda hainayanda requested a review from Copilot June 16, 2025 04:25

This comment was marked as outdated.

@hainayanda hainayanda requested a review from Copilot June 16, 2025 04:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances thread safety across the SwiftEnvironment library, simplifies the GlobalEntry macro by removing the StaticModifier enum and introducing a value wrapper, and updates tests and documentation accordingly.

  • Added atomicRead/atomicWrite methods and dispatch queues for thread-safe global and property-wrapper accesses.
  • Simplified the GlobalEntry macro: dropped the .isolated parameter, removed StaticModifier, and introduced ___ValueWrapper structs.
  • Updated tests to reflect macro changes, added a sendable-injection integration test, and bumped the README to version 4.1.4.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Tests/SwiftEnvironmentTests/MacroTests.swift Updated expected macro expansion to use atomicRead and ___ValueWrapper; removed isolated test.
Tests/SwiftEnvironmentTests/IntegrationTests.swift Added test_givenSendableInjection_whenGet_shouldAlwaysReturnNewValue to cover transient sendable entries.
Tests/SwiftEnvironmentTests/DummyDependency.swift Changed sixthDummy to use a sendable dependency, updated initializer, and imported SwiftUICore.
Sources/SwiftEnvironmentMacro/Macro/GlobalEntryMacro.swift Removed StaticModifier logic and nonisolated attribute, now emits a ___ValueWrapper struct and uses atomicRead.
Sources/SwiftEnvironment/Macros.swift Simplified GlobalEntry macro signature—dropped modifier parameter and StaticModifier enum.
Sources/SwiftEnvironment/Environment/GlobalValues.swift Changed atomicRead from private to public for broader accessibility.
Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift Introduced a concurrent accessQueue, added atomicRead/atomicWrite around wrappedValue, and marked as @unchecked Sendable.
README.md Updated package version to 4.1.4 and removed outdated StaticModifier usage example.
Comments suppressed due to low confidence (4)

README.md:62

  • [nitpick] Consider adding a migration note about the removal of the .isolated parameter from @GlobalEntry, since users upgrading from earlier versions will need to update calls like @GlobalEntry(.isolated).
### Accessing Global Values

Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift:12

  • [nitpick] The name accessQueue in GlobalEnvironment could be confused with GlobalValues.accessQueue. Consider renaming it to environmentAccessQueue or similar to clarify its scope.
private let accessQueue = DispatchQueue(label: "GlobalEnvironment.accessQueue", attributes: .concurrent)

Tests/SwiftEnvironmentTests/DummyDependency.swift:35

  • The type DummySendableDependency is not defined or imported, causing a build error. Define this protocol (e.g., protocol DummySendableDependency: AnyObject & Sendable { var id: UUID { get } }) or adjust to a known type.
@GlobalEntry var sixthDummy: DummySendableDependency = DummySendableClass()

Tests/SwiftEnvironmentTests/MacroTests.swift:33

  • [nitpick] Exact indentation in the expected macro-expansion string can make tests brittle. Consider normalizing indentation or using an indentation-insensitive assertion to improve test resilience.
GlobalValues.atomicRead {

Comment thread Sources/SwiftEnvironment/Environment/GlobalEnvironment.swift
@hainayanda hainayanda merged commit 8666f69 into main Jun 16, 2025
3 checks passed
@hainayanda hainayanda deleted the feature/thread-safety branch June 16, 2025 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants