RawDog is a Swift library that extends RawRepresentable types with arithmetic operations. It defines two protocols and an enum to handle arithmetic for types with fallible initializers and infallible initializers.
- ArithmeticForFallibleType: This protocol is for types that have a fallible initializer. The arithmetic operations return optional values to account for the possibility of failure.
- ArithmeticForInfallibleType: This protocol is for types that have an infallible initializer. The arithmetic operations always succeed and return non-optional values.
- BinaryIntegerDivisionStrategy: This enum defines three strategies for handling division of binary integers: floor, round, and ceiling.
The library is fully tested with 100% code coverage
To add RawDog to your project using Swift Package Manager, add the following dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/ChristianFox/RawDog.git", from: "1.0.0")
]
Don't forget to add RawDog to your target dependencies:
targets: [
.target(
name: "YourTarget",
dependencies: ["RawDog"]),
]
To use RawDog conform your RawRepresentable type to either the ArithmeticForFallibleType
or ArithmeticForInfallibleType
protocol, depending on whether your type has a fallible or infallible initializer.
Here's an example of a struct that represents SISeconds with raw values (Based on Dave DeLong's Time, the inspiration for the library), conforming to the ArithmeticForInfallibleType
protocol:
import RawDog
struct SISeconds: RawRepresentable, ArithmeticForInfallibleType {
let rawValue: Double
init(rawValue: Double) {
self.rawValue = rawValue
}
}
Simply by conforming to the ArithmeticForInfallibleType
protocol
you can now perform any of the following operations:
var a = SISeconds(rawValue: 60)
let b = SISeconds(rawValue: 3600)
let c = a + b // 3660
let d = a - b // 60
let e = a * b // 216000
let f = a / b // 60
a += b // 3660
a -= b // 60
a *= b // 216000
a /= b // 60
let aDouble = 2.0
let g = a ~+ aDouble // 62
let h = a ~- aDouble // 60
let i = a ~* aDouble // 120
let j = a ~/ aDouble // 60
a ~+= aDouble // 62
a ~-= aDouble // 60
a ~*= aDouble // 120
a ~/= aDouble // 60
Please open an issue for support.
Pull requests are welcome. I welcome developers of all skill levels to help improve the library, fix bugs, or add new features.
For major changes, please open an issue first to discuss what you would like to change.
Before submitting a pull request, please ensure that your code adheres to the existing code style and conventions, and that all tests pass. Additionally, if you're adding new functionality, please make sure to include unit tests to verify the behavior.
If you have any questions or need assistance, feel free to open an issue, and I'll do my best to help you out.
RawDog is available under the MIT license. See the LICENSE file for more info.