Skip to content

Commit ef4f9e6

Browse files
committed
[Distributed] More docs for ad-hoc requirements
1 parent cf8f61d commit ef4f9e6

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ import _Concurrency
173173
///
174174
/// Implementing the remote calls correctly and efficiently is the important task for a distributed actor system library.
175175
/// Since those methods are not currently expressible as protocol requirements due to advanced use of generics
176-
/// combined with type aliases, they will not appear in the protocol's documentation as explicit requirements.
176+
/// combined with associated types, they will not appear in the protocol's documentation as explicit requirements.
177177
/// Instead, we present their signatures that a conforming type has to implement here:
178178
///
179179
/// > Note: Although the `remoteCall` methods are not expressed as protocol requirements in source,
@@ -688,6 +688,32 @@ func _executeDistributedTarget<D: DistributedTargetInvocationDecoder>(
688688
/// Once encoded, the system should use some underlying transport mechanism to send the
689689
/// bytes serialized by the invocation to the remote peer.
690690
///
691+
/// ### Protocol requirements
692+
/// Similar to the ``DistributedActorSystem`` and its `remoteCall` and `remoteCallVoid` protocol requirements,
693+
/// the `DistributedTargetInvocationEncoder` contains a few methods which are not possible to express in source due to
694+
/// advanced use generics combined with associated types. Specifically, the `recordArgument` and `recordReturnType`
695+
/// methods are not expressed in source as protocol requirements, but will be treated by the compiler as-if they were.
696+
///
697+
/// > Note: Although the `recordArgument` method is not expressed as protocol requirement in source,
698+
/// > the compiler will provide the same errors as-if they were declared explicitly in this protocol.
699+
///
700+
/// In addition to the compiler offering compile errors if those witnesses are missing in an adopting type,
701+
/// we present their signatures here for reference:
702+
///
703+
/// ```swift
704+
/// /// Record an argument of `Argument` type.
705+
/// /// This will be invoked for every argument of the target, in declaration order.
706+
/// mutating func recordArgument<Value: SerializationRequirement>(
707+
/// _ argument: DistributedTargetArgument<Value>
708+
/// ) throws
709+
///
710+
/// /// Ad-hoc requirement
711+
/// ///
712+
/// /// Record the return type of the distributed method.
713+
/// /// This method will not be invoked if the target is returning `Void`.
714+
/// mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws
715+
/// ```
716+
///
691717
/// ## Decoding an invocation
692718
/// Since every actor system is going to deal with a concrete invocation type, they may
693719
/// implement decoding them whichever way is most optimal for the given system.
@@ -771,6 +797,35 @@ public struct RemoteCallArgument<Value> {
771797

772798
/// Decoder that must be provided to `executeDistributedTarget` and is used
773799
/// by the Swift runtime to decode arguments of the invocation.
800+
///
801+
/// ### Protocol requirements
802+
/// Similar to the ``DistributedTargetInvocationEncoder`` and its `recordArgument` and `recordReturnType` protocol requirements,
803+
/// the `DistributedTargetInvocationDecoder` contains a method which is not possible to express in source due to
804+
/// advanced use generics combined with associated types. Specifically, the `decodeNextArgument`
805+
/// method is not expressed in source as protocol requirement, but will be treated by the compiler as-if it was.
806+
///
807+
/// > Note: Although the `decodeNextArgument` method is not expressed as protocol requirement in source,
808+
/// > the compiler will provide the same errors as-if they were declared explicitly in this protocol.
809+
///
810+
/// In addition to the compiler offering compile errors if this witness is missing in an adopting type,
811+
/// we present its signature here for reference:
812+
///
813+
/// ```swift
814+
/// /// Ad-hoc protocol requirement
815+
/// ///
816+
/// /// Attempt to decode the next argument from the underlying buffers into pre-allocated storage
817+
/// /// pointed at by 'pointer'.
818+
/// ///
819+
/// /// This method should throw if it has no more arguments available, if decoding the argument failed,
820+
/// /// or, optionally, if the argument type we're trying to decode does not match the stored type.
821+
/// ///
822+
/// /// The result of the decoding operation must be stored into the provided 'pointer' rather than
823+
/// /// returning a value. This pattern allows the runtime to use a heavily optimized, pre-allocated
824+
/// /// buffer for all the arguments and their expected types. The 'pointer' passed here is a pointer
825+
/// /// to a "slot" in that pre-allocated buffer. That buffer will then be passed to a thunk that
826+
/// /// performs the actual distributed (local) instance method invocation.
827+
/// mutating func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument
828+
/// ```
774829
@available(SwiftStdlib 5.7, *)
775830
public protocol DistributedTargetInvocationDecoder {
776831
associatedtype SerializationRequirement
@@ -801,15 +856,50 @@ public protocol DistributedTargetInvocationDecoder {
801856
mutating func decodeReturnType() throws -> Any.Type?
802857
}
803858

859+
/// Protocol a distributed invocation execution's result handler.
860+
///
861+
/// An instance conforming to this type must be passed when invoking
862+
/// ``executeDistributedTarget(on:target:invocationDecoder:handler:)`` while handling an incoming distributed call.
863+
///
864+
/// The handler will then be invoked with the return value (or error) that the invoked target returned (or threw).
865+
///
866+
/// ### Protocol requirements
867+
/// Similar to the ``DistributedActorSystem`` and its `remoteCall` and `remoteCallVoid` protocol requirements,
868+
/// the `DistributedTargetInvocationResultHandler` contains a method which is not possible to express in source due to
869+
/// advanced use generics combined with associated types. Specifically, the `onReturn` method is not expressed in
870+
/// source as protocol requirement, but will be treated by the compiler as-if they were.
871+
///
872+
/// > Note: Although the `onReturn` method is not expressed as protocol requirement in source,
873+
/// > the compiler will provide the same errors as-if they were declared explicitly in this protocol.
874+
///
875+
/// In addition to the compiler offering compile errors if this witnesses is missing in an adopting type,
876+
/// we present its signature here for reference:
877+
///
878+
/// ```swift
879+
/// /// Ad-hoc protocol requirement
880+
/// ///
881+
/// /// Invoked when the distributed target execution returned successfully.
882+
/// /// The `value` is the return value of the executed distributed invocation target.
883+
/// func onReturn<Success: SerializationRequirement>(value: Success) async throws
884+
/// ```
804885
@available(SwiftStdlib 5.7, *)
805886
public protocol DistributedTargetInvocationResultHandler {
806887
associatedtype SerializationRequirement
888+
889+
// /// Ad-hoc protocol requirement
890+
// ///
891+
// /// Invoked when the distributed target execution returned successfully.
892+
// /// The `value` is the return value of the executed distributed invocation target.
807893
// func onReturn<Success: SerializationRequirement>(value: Success) async throws
808894

809-
/// Invoked when the distributed target invocation of a `Void` returning
895+
/// Invoked when the distributed target execution of a `Void` returning
810896
/// function has completed successfully.
811897
func onReturnVoid() async throws
812898

899+
/// Invoked when the distributed target execution of a target has thrown an error.
900+
///
901+
/// It is not guaranteed that the error conform to the ``SerializationRequirement``;
902+
/// This guarantee is only given to return values (and offered by `onReturn`).
813903
func onThrow<Err: Error>(error: Err) async throws
814904
}
815905

0 commit comments

Comments
 (0)