Skip to content

Commit bde886d

Browse files
first commit
0 parents  commit bde886d

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm
7+
.netrc
8+
.vscode
9+
Package.resolved

Package.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// swift-tools-version:6.1
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "UnwrapArithmeticOperators",
7+
products: [
8+
.library(
9+
name: "UnwrapArithmeticOperators",
10+
targets: ["UnwrapArithmeticOperators"]
11+
),
12+
],
13+
traits: [
14+
.default(enabledTraits: ["UnwrapArithmetic"]),
15+
.trait(
16+
name: "UnwrapAddition",
17+
description: "Enables unchecked overflow addition operators (`+!` and `+=!`)."
18+
),
19+
.trait(
20+
name: "UnwrapSubtraction",
21+
description: "Enables unchecked overflow subtraction operators (`-!` and `-=!`)."
22+
),
23+
.trait(
24+
name: "UnwrapArithmetic",
25+
description: "Enables unchecked overflow operators.",
26+
enabledTraits: [
27+
"UnwrapAddition",
28+
"UnwrapSubtraction"
29+
]
30+
),
31+
],
32+
targets: [
33+
.target(
34+
name: "UnwrapArithmeticOperators"
35+
),
36+
.testTarget(
37+
name: "UnwrapArithmeticOperatorsTests",
38+
dependencies: ["UnwrapArithmeticOperators"]
39+
),
40+
]
41+
)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
// MARK: Addition
3+
infix operator +!: AdditionPrecedence
4+
infix operator +=!: AdditionPrecedence
5+
6+
extension FixedWidthInteger {
7+
/// Returns the sum of the two given values.
8+
///
9+
/// - Parameters:
10+
/// - lhs: The first value to add.
11+
/// - rhs: The second value to add.
12+
///
13+
/// - Note: Equivalent to `&+` if the package trait `UnwrapAddition` is enabled, and `+` if disabled.
14+
@inlinable
15+
@inline(__always)
16+
public static func +! (lhs: Self, rhs: Self) -> Self {
17+
#if UnwrapAddition
18+
lhs &+ rhs
19+
#else
20+
lhs + rhs
21+
#endif
22+
}
23+
24+
/// Adds two values and stores the result in the left-hand-side variable.
25+
///
26+
/// - Parameters:
27+
/// - lhs: The first value to add.
28+
/// - rhs: The second value to add.
29+
///
30+
/// - Note: Equivalent to `&+=` if the package trait `UnwrapAddition` is enabled, and `+=` if disabled.
31+
@inlinable
32+
@inline(__always)
33+
public static func +=! (lhs: inout Self, rhs: Self) {
34+
#if UnwrapAddition
35+
lhs &+= rhs
36+
#else
37+
lhs += rhs
38+
#endif
39+
}
40+
}
41+
42+
43+
44+
// MARK: Subtraction
45+
infix operator -!: AdditionPrecedence
46+
infix operator -=!: AdditionPrecedence
47+
48+
extension FixedWidthInteger {
49+
/// Returns the difference of the two given values.
50+
///
51+
/// - Parameters:
52+
/// - lhs: A numeric value.
53+
/// - rhs: The value to subtract from lhs.
54+
///
55+
/// - Note: Equivalent to `&-` if the package trait `UnwrapSubtraction` is enabled, and `-` if disabled.
56+
@inlinable
57+
@inline(__always)
58+
public static func -! (lhs: Self, rhs: Self) -> Self {
59+
#if UnwrapSubtraction
60+
lhs &- rhs
61+
#else
62+
lhs - rhs
63+
#endif
64+
}
65+
66+
/// Subtracts the second value from the first and stores the difference in the left-hand-side variable.
67+
///
68+
/// - Parameters:
69+
/// - lhs: A numeric value.
70+
/// - rhs: The value to subtract from lhs.
71+
///
72+
/// - Note: Equivalent to `&-=` if the package trait `UnwrapSubtraction` is enabled, and `-=` if disabled.
73+
@inlinable
74+
@inline(__always)
75+
public static func -=! (lhs: inout Self, rhs: Self) {
76+
#if UnwrapSubtraction
77+
lhs &-= rhs
78+
#else
79+
lhs -= rhs
80+
#endif
81+
}
82+
}
83+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import Testing
3+
@testable import UnwrapArithmeticOperators
4+
5+
@Test
6+
func example() {
7+
#expect(1 + 1 == 2)
8+
#expect(1 +! 1 == 2)
9+
#expect(1 + 1 +! 1 +! 1 == 4)
10+
11+
var test = 1
12+
test +=! 1
13+
#expect(test == 2)
14+
15+
#expect(1 - 1 == 0)
16+
#expect(1 -! 1 == 0)
17+
#expect(1 - 1 -! 1 -! 1 == -2)
18+
19+
test -=! 2
20+
#expect(test == 0)
21+
}

0 commit comments

Comments
 (0)