cow-swift

1.0.0

A tiny, dependency-free copy-on-write wrapper for Swift value types
AlexNachbaur/cow-swift

What's New

1.0.0

2026-10-11T02:26:23Z

The first release of cow-swift: a tiny, dependency-free copy-on-write wrapper for your own value types.

Added

  • CopyOnWrite<Value>, a copy-on-write wrapper for any value type, usable directly or as the
    @CopyOnWrite property wrapper.
  • In-place mutation through value, through dynamic member lookup, through modify(_:), and through
    wrapped properties, with a single uniqueness check per mutation.
  • modify(_:) with typed throws, so the closure's error type is preserved.
  • isIdentical(to:), ===, and !== for O(1) storage identity checks.
  • Conditional Sendable, Equatable, Hashable, Comparable, Encodable, and Decodable conformances,
    plus CustomStringConvertible, CustomDebugStringConvertible, and CustomReflectable.
  • KeyedDecodingContainer/KeyedEncodingContainer overloads so @CopyOnWrite optional properties decode a
    missing key as nil and omit nil when encoding, like plain optional properties.
  • A DocC catalog with guides on getting started, thread safety, and performance.
  • Support for Apple platforms, Linux, Android, and Windows, with no dependencies beyond the standard library.

Installation

.package(url: "https://github.com/AlexNachbaur/cow-swift.git", from: "1.0.0"),

cow-swift

CI Documentation Swift 6.0+ Platforms License

A tiny, dependency-free copy-on-write wrapper for your own value types.

Copying a Swift struct copies every stored property. CopyOnWrite moves the value onto the heap and shares it between copies, so copying costs one reference-count increment no matter how large the value is. The value is copied only when a copy is mutated while its storage is still shared, which is exactly what Array, Dictionary, and String do. Value semantics are preserved; the copies just got cheap.

import CopyOnWrite

struct Document: Equatable, Codable, Sendable {
    @CopyOnWrite var contents: Contents   // a large struct
    var revision = 0
}

var draft = Document(contents: Contents())
let snapshot = draft                      // O(1)
draft.contents.title = "Untitled"         // copies `Contents` once; `snapshot` is unaffected
draft.contents.paragraphs.append("…")     // in place from now on

Features

  • Two styles, one behavior. Use it as a property wrapper (@CopyOnWrite var contents: Contents) or as a value (CopyOnWrite(contents)), whichever reads better.
  • Mutations stay in place. Writing through .value, through a member (wrapper.title = "…"), through modify { … }, or through a wrapped property all mutate uniquely owned storage directly, without hidden copies.
  • Transparent conformances. Equatable, Hashable, Comparable, Codable, Sendable, and CustomStringConvertible are provided whenever the wrapped value supports them, and they see through the wrapper. Synthesized conformances on types that use @CopyOnWrite work as expected, including optional properties with missing keys.
  • Identity checks. a.isIdentical(to: b) (or a === b) tells you in O(1) whether two wrappers still share storage, which means neither has been mutated since they were copied.
  • Thread-safe the way Array is. Copies can be read and mutated concurrently, and the wrapper is Sendable whenever its value is. There is no lock inside; see Thread safety.
  • Zero dependencies. Only the standard library, so it builds everywhere Swift does.

Usage

As a property wrapper

struct Document {
    @CopyOnWrite var contents: Contents
}

var draft = Document(contents: Contents())
draft.contents.title = "Untitled"

// The `$` projection gives read-only access to the wrapper, which is useful for identity checks:
if draft.$contents.isIdentical(to: snapshot.$contents) {
    // nothing has been written since the snapshot
}

As a value

var profile = CopyOnWrite(Profile(name: "Jane", tags: []))

profile.value.name = "John"       // mutate through `value`…
profile.name = "John"             // …or through dynamic member lookup
let count = profile.modify {      // …or batch several mutations, returning a result
    $0.tags.append("admin")
    $0.tags.append("staff")
    return $0.tags.count
}

var copy = profile                // shares storage
copy.name = "Jane"                // `copy` gets its own storage; `profile` still says "John"
print(profile === copy)           // false

The full API is documented in the DocC catalog, including guides on thread safety and performance.

Thread safety

CopyOnWrite offers the same guarantees as the standard library's collections:

  • Different copies are independent. They can be read and mutated from different threads at the same time, even while they share storage. Storage is only ever written to by a wrapper that holds the only reference to it, so no other copy can observe the write.
  • A single variable is not synchronized. Mutating one variable from several threads at once is a data race, just as it is for an Array, and the Swift 6 compiler rejects such code. Protect a shared mutable wrapper with an actor or a Mutex, as you would any other value.

Copy-on-write doesn't need a lock to be thread-safe: uniqueness checks rely on the atomic reference count Swift already maintains, so reads and uncontended writes cost no more than a pointer dereference and a uniqueness check. The concurrency tests run under the thread sanitizer in CI.

Requirements

  • Swift 6.0 or later
  • Any platform Swift supports: Apple platforms, Linux, Android, and Windows. CI builds and tests on macOS, Linux, and Windows, and builds for Android.

Installation

Add the package to your Package.swift:

dependencies: [
    .package(url: "https://github.com/AlexNachbaur/cow-swift.git", from: "1.0.0"),
],
targets: [
    .target(name: "MyTarget", dependencies: [
        .product(name: "CopyOnWrite", package: "cow-swift"),
    ]),
]

Or in Xcode, choose File ▸ Add Package Dependencies… and enter https://github.com/AlexNachbaur/cow-swift.git.

Contributing

Contributions are welcome. Please read CONTRIBUTING.md for how to build, test, and submit changes, and SECURITY.md for how to report a vulnerability.

License

cow-swift is released under the Apache License, Version 2.0.

The initial implementation was extracted from the okta-mobile-swift library, which is also Apache 2.0 licensed. Original copyright notices are preserved in the affected source files, and attribution is recorded in NOTICE.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Nov 29 2026 18:43:47 GMT-1000 (Hawaii-Aleutian Standard Time)