From c99f771f56ed78c79c73ef82bc78298183564218 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Sun, 18 Jun 2023 18:49:36 +0400 Subject: [PATCH] Implement `Atomic` package (#1) * Create the initial project's structure * Update `Package.swift` * Implement `Atomic` property wrapper * Update `README.md` --- .github/workflows/ci.yml | 41 ++++++ .swiftformat | 64 +++++++++ .swiftlint.yml | 135 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + MIntfile | 2 + Makefile | 19 +++ Package.swift | 28 ++++ README.md | 63 +++++++- Sources/Atomic/Atomic.swift | 40 ++++++ Tests/AtomicTests/AtomicTests.swift | 21 +++ hooks/pre-commit | 11 ++ 11 files changed, 430 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .swiftformat create mode 100644 .swiftlint.yml create mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata create mode 100644 MIntfile create mode 100644 Makefile create mode 100644 Package.swift create mode 100644 Sources/Atomic/Atomic.swift create mode 100644 Tests/AtomicTests/AtomicTests.swift create mode 100755 hooks/pre-commit diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9f03a75 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +name: "atomic" + +on: + push: + branches: + - main + - dev + pull_request: + branches: [ main ] + +concurrency: + group: ci + cancel-in-progress: true + +jobs: + Latest: + name: Test Latest (iOS, macOS, tvOS, watchOS) + runs-on: macOS-12 + env: + DEVELOPER_DIR: "/Applications/Xcode_14.1.app/Contents/Developer" + timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + include: + - destination: "OS=16.1,name=iPhone 14 Pro" + name: "iOS" + scheme: "Atomic" + - destination: "OS=16.1,name=Apple TV" + name: "tvOS" + scheme: "Atomic" + - destination: "OS=9.1,name=Apple Watch Series 8 (45mm)" + name: "watchOS" + scheme: "Atomic" + - destination: "platform=macOS" + name: "macOS" + scheme: "Atomic" + steps: + - uses: actions/checkout@v3 + - name: ${{ matrix.name }} + run: xcodebuild test -scheme "${{ matrix.scheme }}" -destination "${{ matrix.destination }}" clean \ No newline at end of file diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 0000000..5d83d63 --- /dev/null +++ b/.swiftformat @@ -0,0 +1,64 @@ +# Stream rules + +--swiftversion 5.3 + +# Use 'swiftformat --options' to list all of the possible options + +--header "\nConcurrency\nCopyright © {created.year} Space Code. All rights reserved.\n//" + +--enable blankLinesBetweenScopes +--enable blankLinesAtStartOfScope +--enable blankLinesAtEndOfScope +--enable blankLinesAroundMark +--enable anyObjectProtocol +--enable consecutiveBlankLines +--enable consecutiveSpaces +--enable duplicateImports +--enable elseOnSameLine +--enable emptyBraces +--enable initCoderUnavailable +--enable leadingDelimiters +--enable numberFormatting +--enable preferKeyPath +--enable redundantBreak +--enable redundantExtensionACL +--enable redundantFileprivate +--enable redundantGet +--enable redundantInit +--enable redundantLet +--enable redundantLetError +--enable redundantNilInit +--enable redundantObjc +--enable redundantParens +--enable redundantPattern +--enable redundantRawValues +--enable redundantReturn +--enable redundantSelf +--enable redundantVoidReturnType +--enable semicolons +--enable sortedImports +--enable sortedSwitchCases +--enable spaceAroundBraces +--enable spaceAroundBrackets +--enable spaceAroundComments +--enable spaceAroundGenerics +--enable spaceAroundOperators +--enable spaceInsideBraces +--enable spaceInsideBrackets +--enable spaceInsideComments +--enable spaceInsideGenerics +--enable spaceInsideParens +--enable strongOutlets +--enable strongifiedSelf +--enable todos +--enable trailingClosures +--enable unusedArguments +--enable void +--enable markTypes +--enable isEmpty + +# format options + +--wraparguments before-first +--wrapcollections before-first +--maxwidth 140 \ No newline at end of file diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..89efd09 --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,135 @@ +excluded: + - Tests + - Package.swift + - .build + +# Rules + +disabled_rules: + - trailing_comma + - todo + - opening_brace + +opt_in_rules: # some rules are only opt-in + - anyobject_protocol + - array_init + - attributes + - closure_body_length + - closure_end_indentation + - closure_spacing + - collection_alignment + - conditional_returns_on_newline + - contains_over_filter_count + - contains_over_filter_is_empty + - contains_over_first_not_nil + - contains_over_range_nil_comparison + - convenience_type + - discouraged_object_literal + - discouraged_optional_boolean + - empty_collection_literal + - empty_count + - empty_string + - empty_xctest_method + - enum_case_associated_values_count + - explicit_init + - fallthrough + - fatal_error_message + - file_name + - file_types_order + - first_where + - flatmap_over_map_reduce + - force_unwrapping + - ibinspectable_in_extension + - identical_operands + - implicit_return + - inert_defer + - joined_default_parameter + - last_where + - legacy_multiple + - legacy_random + - literal_expression_end_indentation + - lower_acl_than_parent + - multiline_arguments + - multiline_function_chains + - multiline_literal_brackets + - multiline_parameters + - multiline_parameters_brackets + - no_space_in_method_call + - operator_usage_whitespace + - optional_enum_case_matching + - orphaned_doc_comment + - overridden_super_call + - override_in_extension + - pattern_matching_keywords + - prefer_self_type_over_type_of_self + - prefer_zero_over_explicit_init + - prefixed_toplevel_constant + - private_action + - prohibited_super_call + - quick_discouraged_call + - quick_discouraged_focused_test + - quick_discouraged_pending_test + - reduce_into + - redundant_nil_coalescing + - redundant_objc_attribute + - redundant_type_annotation + - required_enum_case + - single_test_class + - sorted_first_last + - sorted_imports + - static_operator + - strict_fileprivate + - switch_case_on_newline + - toggle_bool + - unavailable_function + - unneeded_parentheses_in_closure_argument + - unowned_variable_capture + - untyped_error_in_catch + - vertical_parameter_alignment_on_call + - vertical_whitespace_closing_braces + - vertical_whitespace_opening_braces + - xct_specific_matcher + - yoda_condition + +force_cast: warning +force_try: warning + +identifier_name: + excluded: + - id + - URL + +analyzer_rules: + - unused_import + - unused_declaration + +line_length: + warning: 130 + error: 200 + +type_body_length: + warning: 300 + error: 400 + +file_length: + warning: 500 + error: 1200 + +function_body_length: + warning: 30 + error: 50 + +large_tuple: + error: 3 + +nesting: + type_level: + warning: 2 + statement_level: + warning: 10 + + +type_name: + max_length: + warning: 40 + error: 50 \ No newline at end of file diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/MIntfile b/MIntfile new file mode 100644 index 0000000..1f32d33 --- /dev/null +++ b/MIntfile @@ -0,0 +1,2 @@ +nicklockwood/SwiftFormat@0.47.12 +realm/SwiftLint@0.47.1 \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..856d64b --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +all: bootstrap + +bootstrap: hook + mint bootstrap + +hook: + ln -sf ../../hooks/pre-commit .git/hooks/pre-commit + chmod +x .git/hooks/pre-commit + +mint: + mint bootstrap + +lint: + mint run swiftlint + +fmt: + mint run swiftformat Sources Tests + +.PHONY: all bootstrap hook mint lint fmt \ No newline at end of file diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..1893edd --- /dev/null +++ b/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version: 5.5 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Atomic", + platforms: [ + .macOS(.v10_15), + .iOS(.v13), + .watchOS(.v6), + .tvOS(.v11), + ], + products: [ + .library(name: "Atomic", targets: ["Atomic"]), + ], + dependencies: [], + targets: [ + .target( + name: "Atomic", + dependencies: [] + ), + .testTarget( + name: "AtomicTests", + dependencies: ["Atomic"] + ), + ] +) diff --git a/README.md b/README.md index fa8df29..8d09c2f 100644 --- a/README.md +++ b/README.md @@ -1 +1,62 @@ -# atomic \ No newline at end of file +

atomic

+ +

+License +Platform +Swift5.5 +CI + +

+ +## Description +`atomic` is a fast, safe class for making values thread-safe in Swift. + +- [Usage](#usage) +- [Requirements](#requirements) +- [Installation](#installation) +- [Communication](#communication) +- [Contributing](#contributing) +- [Author](#author) +- [License](#license) + +## Usage + +```swift +import Atomic + +/// Creates an `Atomic` property. +@Atomic var value = 5 +``` + +## Installation +### Swift Package Manager + +The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler. It is in early development, but `atomic` does support its use on supported platforms. + +Once you have your Swift package set up, adding `atomic` as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`. + +```swift +dependencies: [ + .package(url: "https://github.com/space-code/atomic.git", .upToNextMajor(from: "0.0.1")) +] +``` + +## Communication +- If you **found a bug**, open an issue. +- If you **have a feature request**, open an issue. +- If you **want to contribute**, submit a pull request. + +## Contributing +Bootstrapping development environment + +``` +make bootstrap +``` + +Please feel free to help out with this project! If you see something that could be made better or want a new feature, open up an issue or send a Pull Request! + +## Author +Nikita Vasilev, nv3212@gmail.com + +## License +atomic is available under the MIT license. See the LICENSE file for more info. \ No newline at end of file diff --git a/Sources/Atomic/Atomic.swift b/Sources/Atomic/Atomic.swift new file mode 100644 index 0000000..b2d57d4 --- /dev/null +++ b/Sources/Atomic/Atomic.swift @@ -0,0 +1,40 @@ +// +// Concurrency +// Copyright © 2023 Space Code. All rights reserved. +// + +import Foundation + +/// An `Atomic` property wrapper. +@propertyWrapper +public final class Atomic { + // MARK: Properties + + private let semaphore = DispatchSemaphore(value: 1) + private var value: Value + + // MARK: Initialization + + /// Create a new `Atomic` property wrapper with the given value. + /// + /// - Parameter value: A value object, + public init(wrappedValue value: Value) { + self.value = value + } + + // MARK: Public + + /// Automatically gets or sets the value of the variable. + public var wrappedValue: Value { + get { + semaphore.wait() + defer { semaphore.signal() } + return value + } + set { + semaphore.wait() + value = newValue + semaphore.signal() + } + } +} diff --git a/Tests/AtomicTests/AtomicTests.swift b/Tests/AtomicTests/AtomicTests.swift new file mode 100644 index 0000000..d85cfb2 --- /dev/null +++ b/Tests/AtomicTests/AtomicTests.swift @@ -0,0 +1,21 @@ +// +// Concurrency +// Copyright © 2023 Space Code. All rights reserved. +// + +@testable import Atomic +import XCTest + +final class AtomicTests: XCTestCase { + // MARK: Properties + + @Atomic var dict = [Int: String]() + + // MARK: Tests + + func test_thatAtomicPropertyChangesValue() { + DispatchQueue.concurrentPerform(iterations: 1000) { _ in + self.dict[.random(in: 0 ... 1000)] = "test" + } + } +} diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..89b3319 --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,11 @@ +#!/bin/bash +git diff --diff-filter=d --staged --name-only | grep -e '\.swift$' | while read line; do + if [[ $line == *"/Generated"* ]]; then + echo "IGNORING GENERATED FILE: " "$line"; + else + mint run swiftformat swiftformat "${line}"; + git add "$line"; + fi +done + +swiftlint \ No newline at end of file