Skip to content

Commit 1fc371e

Browse files
committedFeb 12, 2025
feat: address concurrency warnings arising from Theme
`Theme` was marked as `Sendable` in gonzalezreal#351. There's nothing wrong with this per se, but sendability is not inferred for public types, a structure is only truely `Sendable` if all of its members are `Sendable`, and most of the members of `Theme` are not `Sendable`. This patch address the sendability warnings for Theme's members that the compiler emits (regardless of `StrictConcurrency`).
1 parent fe5621b commit 1fc371e

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed
 

‎Sources/MarkdownUI/Theme/TextStyle/Styles/FontProperties.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftUI
33
/// The characteristics of a font.
44
public struct FontProperties: Hashable {
55
/// The font family.
6-
public enum Family: Hashable {
6+
public enum Family: Hashable, Sendable {
77
/// The system font family.
88
case system(Font.Design = .default)
99

@@ -12,7 +12,7 @@ public struct FontProperties: Hashable {
1212
}
1313

1414
/// The font family variant.
15-
public enum FamilyVariant: Hashable {
15+
public enum FamilyVariant: Hashable, Sendable {
1616
/// No variant. Use the current font family.
1717
case normal
1818

@@ -21,7 +21,7 @@ public struct FontProperties: Hashable {
2121
}
2222

2323
/// The font caps variant.
24-
public enum CapsVariant: Hashable {
24+
public enum CapsVariant: Hashable, Sendable {
2525
/// Don't use a font caps variant.
2626
case normal
2727

@@ -36,7 +36,7 @@ public struct FontProperties: Hashable {
3636
}
3737

3838
/// The font digit variant.
39-
public enum DigitVariant: Hashable {
39+
public enum DigitVariant: Hashable, Sendable {
4040
/// Don't use a font digit variant.
4141
case normal
4242

@@ -45,7 +45,7 @@ public struct FontProperties: Hashable {
4545
}
4646

4747
/// The font style.
48-
public enum Style {
48+
public enum Style: Sendable {
4949
/// Don't use a font style.
5050
case normal
5151

@@ -98,11 +98,11 @@ public struct FontProperties: Hashable {
9898
/// The font width.
9999
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
100100
public var width: Font.Width {
101-
get { (self.widthStorage as? Font.Width) ?? .standard }
102-
set { self.widthStorage = newValue }
101+
get { (self.widthStorage?.base as? Font.Width) ?? .standard }
102+
set { self.widthStorage = AnySendableHashableBox(newValue) }
103103
}
104104

105-
private var widthStorage: AnyHashable?
105+
private var widthStorage: AnySendableHashableBox?
106106

107107
/// The font size.
108108
public var size: CGFloat = Self.defaultSize
@@ -187,3 +187,14 @@ extension FontProperties: TextStyle {
187187
attributes.fontProperties = self
188188
}
189189
}
190+
191+
// A cheap workaround for `AnyHashable` of a `Sendable` not being `Sendable`.
192+
// This is necessary for `widthStorage`.
193+
// TODO: iOS16,macOS13 once minimum deployment target is raised, remove this indirection
194+
fileprivate struct AnySendableHashableBox: Hashable, @unchecked Sendable {
195+
let base: AnyHashable
196+
197+
init<V: Hashable & Sendable>(_ base: V) {
198+
self.base = base
199+
}
200+
}

‎Sources/MarkdownUI/Theme/TextStyle/Styles/FontSize.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22

33
/// A text style that sets the font size.
44
public struct FontSize: TextStyle {
5-
private enum Size {
5+
private enum Size: Sendable {
66
case points(CGFloat)
77
case relative(RelativeSize)
88
}

‎Sources/MarkdownUI/Theme/TextStyle/TextStyle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ import SwiftUI
5757
/// ```
5858
///
5959
/// ![](CustomBlockquote)
60-
public protocol TextStyle {
60+
public protocol TextStyle : Sendable {
6161
func _collectAttributes(in attributes: inout AttributeContainer)
6262
}

‎Sources/MarkdownUI/Utility/RelativeSize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import SwiftUI
2121
/// FontSize(.em(2))
2222
/// }
2323
/// ```
24-
public struct RelativeSize: Hashable {
25-
enum Unit: Hashable {
24+
public struct RelativeSize: Hashable, Sendable {
25+
enum Unit: Hashable, Sendable {
2626
case em
2727
case rem
2828
}

0 commit comments

Comments
 (0)
Please sign in to comment.