Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sendable conformance #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: SwiftyLab/setup-swift@latest
with:
development: true
- run: swift test --parallel
lint:
runs-on: ubuntu-latest
Expand Down
25 changes: 25 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version:6.0
import PackageDescription

let package = Package(
name: "Percentage",
products: [
.library(
name: "Percentage",
targets: [
"Percentage"
]
)
],
targets: [
.target(
name: "Percentage"
),
.testTarget(
name: "PercentageTests",
dependencies: [
"Percentage"
]
)
]
)
27 changes: 13 additions & 14 deletions Sources/Percentage/Percentage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//=> "14.3%"
```
*/
public struct Percentage: Hashable, Codable {
public struct Percentage: Hashable, Codable, Sendable, RawRepresentable {
/**
The raw percentage number.

Expand All @@ -60,7 +60,7 @@
//=> 0.5
```
*/
public var fraction: Double { rawValue / 100 }
public let fraction: Double

/**
Clamp the percentage to a value between 0% and 100%.
Expand Down Expand Up @@ -96,7 +96,7 @@
```
*/
public init<T>(_ percentage: T) where T: BinaryFloatingPoint {
self.rawValue = Double(percentage)
self.init(rawValue: Double(percentage))
}

/**
Expand All @@ -109,7 +109,7 @@
```
*/
public init<T>(_ percentage: T) where T: BinaryInteger {
self.rawValue = Double(percentage)
self.init(rawValue: Double(percentage))
}

/**
Expand All @@ -121,7 +121,12 @@
```
*/
public init(fraction: Double) {
self.rawValue = fraction * 100
self.init(rawValue: fraction * 100)
}

Check warning on line 126 in Sources/Percentage/Percentage.swift

View workflow job for this annotation

GitHub Actions / lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
public init(rawValue: Double) {
self.rawValue = rawValue
self.fraction = rawValue / 100
}

/**
Expand Down Expand Up @@ -175,12 +180,6 @@
}
}

extension Percentage: RawRepresentable {
public init(rawValue: Double) {
self.rawValue = rawValue
}
}

extension Percentage: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
Expand All @@ -189,7 +188,7 @@

extension Percentage: CustomStringConvertible {
// Note: It's a `var` for testing.
internal static var formatter: NumberFormatter = {
nonisolated(unsafe) internal static var formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
return formatter
Expand Down Expand Up @@ -220,13 +219,13 @@

extension Percentage: ExpressibleByFloatLiteral {
public init(floatLiteral value: Double) {
self.rawValue = value
self.init(rawValue: value)
}
}

extension Percentage: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Double) {
self.rawValue = value
self.init(rawValue: value)
}
}

Expand All @@ -241,7 +240,7 @@

public static func += (lhs: inout Self, rhs: Self) {
// swiftlint:disable:next shorthand_operator
lhs = lhs + rhs

Check warning on line 243 in Sources/Percentage/Percentage.swift

View workflow job for this annotation

GitHub Actions / lint

Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
}

public static func - (lhs: Self, rhs: Self) -> Self {
Expand All @@ -250,7 +249,7 @@

public static func -= (lhs: inout Self, rhs: Self) {
// swiftlint:disable:next shorthand_operator
lhs = lhs - rhs

Check warning on line 252 in Sources/Percentage/Percentage.swift

View workflow job for this annotation

GitHub Actions / lint

Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
}

public static func * (lhs: Self, rhs: Self) -> Self {
Expand All @@ -259,7 +258,7 @@

public static func *= (lhs: inout Self, rhs: Self) {
// swiftlint:disable:next shorthand_operator
lhs = lhs * rhs

Check warning on line 261 in Sources/Percentage/Percentage.swift

View workflow job for this annotation

GitHub Actions / lint

Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
}

public var magnitude: Magnitude { rawValue.magnitude }
Expand All @@ -280,6 +279,6 @@

public static func /= (lhs: inout Self, rhs: Self) {
// swiftlint:disable:next shorthand_operator
lhs = lhs / rhs

Check warning on line 282 in Sources/Percentage/Percentage.swift

View workflow job for this annotation

GitHub Actions / lint

Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
}
}
Loading