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

added _SendableAnyHTMLBox #60

Merged
merged 4 commits into from
Dec 7, 2024
Merged
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
31 changes: 31 additions & 0 deletions Sources/Elementary/ServerSupport/SendOnceBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if swift(>=6.0) && !hasFeature(Embedded)
import Synchronization

@available(macOS 15.0, *)
final class SendOnceBox: Sendable, SendOnceBoxing {
// final class SendOnceBox<Value>: Sendable, SendOnceBoxing {
typealias Value = any HTML
// NOTE: generics+Synchronization crashes the compiler ATM
// https://github.com/swiftlang/swift/issues/78048

let mutex: Mutex<Value?>

init(_ value: sending Value) {
mutex = Mutex(value)
}

func tryTake() -> sending Value? {
mutex.withLock { value -> sending Value? in
let result = value
value = nil
return result
}
}
}

// NOTE: this is for macOS runtime availability of SendOnceBox and can be removed when macOS 15 is the minimum
protocol SendOnceBoxing<Value>: AnyObject, Sendable {
associatedtype Value
func tryTake() -> sending Value?
}
#endif
46 changes: 46 additions & 0 deletions Sources/Elementary/ServerSupport/SendableAnyHTMLBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#if !hasFeature(Embedded)
/// A wrapper around an `any HTML` value that can be safely sent once.
///
/// Note: For non-sendable values, this will only allow the value to be taken only once.
/// Sendable values can safely be taken multiple times.
public struct _SendableAnyHTMLBox: Sendable {
var storage: Storage

enum Storage {
case sendable(any HTML & Sendable)
#if swift(>=6.0)
// NOTE: protocol can be removed when macOS 15 is the minimum
case sendOnceBox(any SendOnceBoxing<any HTML>)
#endif
}

public init(_ html: any HTML & Sendable) {
storage = .sendable(html)
}

#if swift(>=6.0)
@available(macOS 15, *)
public init(_ html: sending any HTML) {
storage = .sendOnceBox(SendOnceBox(html))
}
#endif

#if swift(>=6.0)
public consuming func tryTake() -> sending (any HTML)? {
switch storage {
case let .sendable(html):
return html
case let .sendOnceBox(box):
return box.tryTake()
}
}
#else
public consuming func tryTake() -> (any HTML & Sendable)? {
switch storage {
case let .sendable(html):
return html
}
}
#endif
}
#endif
32 changes: 32 additions & 0 deletions Tests/ElementaryTests/SendableAnyHTMLBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Elementary
import XCTest

@available(macOS 15.0, *)
final class SendOnceHTMLValueTests: XCTestCase {
func testHoldsSendableValue() {
let html = div { "Hello, World!" }
let box = _SendableAnyHTMLBox(html)
XCTAssertNotNil(box.tryTake())
XCTAssertNotNil(box.tryTake())
}

#if swift(>=6.0)
func testHoldsNonSendable() {
let html = MyComponent()
let box = _SendableAnyHTMLBox(html)
XCTAssertNotNil(box.tryTake())
XCTAssertNil(box.tryTake())
}
#endif
}

class NonSendable {
var x: Int = 0
}

struct MyComponent: HTML {
let ns = NonSendable()
var content: some HTML {
div { "\(ns.x)" }
}
}
Loading