Skip to content

Commit 037df01

Browse files
committed
[Distributed] PoC using a property wrapper on distributed func param for validation
1 parent cdcba01 commit 037df01

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
292292

293293
var decoder = invocation.makeDecoder()
294294

295+
print(" > execute distributed target: \(target)")
295296
try await executeDistributedTarget(
296297
on: active,
297298
target: target,
@@ -342,6 +343,7 @@ public final class FakeRoundtripActorSystem: DistributedActorSystem, @unchecked
342343

343344
var decoder = invocation.makeDecoder()
344345

346+
print(" > execute distributed target: \(target)")
345347
try await executeDistributedTarget(
346348
on: active,
347349
target: target,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
3+
// RUN: %target-build-swift -module-name main -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
4+
// RUN: %target-codesign %t/a.out
5+
// RUN: %target-run %t/a.out | %FileCheck %s --color --dump-input=always
6+
7+
// REQUIRES: executable_test
8+
// REQUIRES: concurrency
9+
// REQUIRES: distributed
10+
11+
// UNSUPPORTED: use_os_stdlib
12+
// UNSUPPORTED: back_deployment_runtime
13+
14+
import Distributed
15+
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
16+
17+
@propertyWrapper
18+
struct Validate {
19+
// TODO: we can't throw from here, but if we could, this would be ideal for throwing validation
20+
// Today we can fatalError here if we wanted to though.
21+
init(wrappedValue: Int, in range: ClosedRange<Int>) {
22+
//precondition(range.contains(wrappedValue), "Value [\(wrappedValue)] did not fit required range \(range)")
23+
if !range.contains(wrappedValue) {
24+
print("VALIDATION WARNING: Value [\(wrappedValue)] did not fit required range \(range)")
25+
}
26+
}
27+
28+
var wrappedValue: Int {
29+
12
30+
}
31+
}
32+
33+
distributed actor CheckTheNums {
34+
35+
distributed func check(@Validate(in: 1...100) num: Int) {
36+
let i: Int = num
37+
_ = i
38+
}
39+
}
40+
41+
// ==== Execute ----------------------------------------------------------------
42+
43+
func test() async throws {
44+
let system = DefaultDistributedActorSystem()
45+
46+
let da = CheckTheNums(actorSystem: system)
47+
// try await da.check(num: 1000)
48+
49+
let remote = try CheckTheNums.resolve(id: da.id, using: system)
50+
try await remote.check(num: 10000)
51+
52+
// CHECK: > execute distributed target: main.CheckTheNums.check(num:)
53+
// CHECK: > decode argument: 10000
54+
// CHECK: VALIDATION WARNING: Value [10000] did not fit required range 1...100
55+
56+
// CHECK: OK
57+
print("OK")
58+
}
59+
60+
@main struct Main {
61+
static func main() async throws {
62+
try await test()
63+
}
64+
}

0 commit comments

Comments
 (0)