Skip to content

Repository files navigation

atomic

LicenseSwift CompatibilityPlatform CompatibilityCI

Description

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

Features

🔒 Thread-Safe - Automatic synchronization for concurrent access
Simple API - Just add @Atomic to any property
🎯 Type-Safe - Works with any Swift type
📱 Cross-Platform - Works on iOS, macOS, tvOS, watchOS, and visionOS
Lightweight - Minimal footprint with zero dependencies
🧪 Well Tested - Comprehensive test coverage

Table of Contents

Requirements

PlatformMinimum Version
iOS13.0+
macOS10.15+
tvOS13.0+
watchOS6.0+
visionOS1.0+
Xcode15.3+
Swift5.10+

Installation

Swift Package Manager

Add the following dependency to your Package.swift:

dependencies:[.package(url:"https://github.com/space-code/atomic.git", from:"1.1.1")]

Or add it through Xcode:

  1. File > Add Package Dependencies
  2. Enter package URL: https://github.com/space-code/atomic.git
  3. Select version requirements

Quick Start

import Atomic
@Atomicvarcounter=0
// Thread-safe increment from multiple threads
DispatchQueue.concurrentPerform(iterations:1000){ _ in
counter +=1}print("Final count: \(counter)") // Always 1000

Usage

Basic Usage

Simply add the @Atomic property wrapper to any property that needs thread-safe access:

import Atomic
classUserSession{@Atomicvaruser:User?@AtomicvarloginAttempts=0func login(username:String, password:String)asyncthrows{
// Thread-safe write
_loginAttempts.write{ $0 +=1}
// Perform authentication
letauthenticatedUser=tryawaitauthenticate(username, password)
// Thread-safe write with new value
_user.write(authenticatedUser)}func getCurrentUsername()->String?{
// Thread-safe read
_user.read{ $0?.username }}}

Thread-Safe Collections

import Atomic
classDataCache{@Atomicprivatevarcache:[String:Data]=[:]func store(_ data:Data, forKey key:String){
_cache.write{ cache incache[key]= data
}}func retrieve(forKey key:String)->Data?{
_cache.read{$0[key]}}func removeExpired(before date:Date){
_cache.write{ cache in
cache = cache.filter{ $0.value.timestamp > date }}}}

Communication

Contributing

We love contributions! 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.

Development Setup

Bootstrap the development environment:

mise install

Author

Nikita Vasilev

License

Atomic is released under the MIT license. See LICENSE for details.


⬆ back to top

Made with ❤️ by space-code

About

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages