ProtocolDistributed5.9.0
DistributedTargetInvocationDecoder
Decoder that must be provided to executeDistributedTarget
and is used by the Swift runtime to decode arguments of the invocation.
- iOS
- 16.0+
- macOS
- 13.0+
- tvOS
- 16.0+
- watchOS
- 9.0+
protocol DistributedTargetInvocationDecoder
Protocol requirements
Similar to the DistributedTargetInvocationEncoder
and its recordArgument
and recordReturnType
protocol requirements, the DistributedTargetInvocationDecoder
contains a method which is not possible to express in source due to advanced use of generics combined with associated types. Specifically, the decodeNextArgument
method is not expressed in source as protocol requirement, but will be treated by the compiler as-if it was.
In addition to the compiler offering compile errors if this witness is missing in an adopting type, we present its signature here for reference:
/// Ad-hoc protocol requirement
///
/// Attempt to decode the next argument from the underlying buffers into pre-allocated storage
/// pointed at by 'pointer'.
///
/// This method should throw if it has no more arguments available, if decoding the argument failed,
/// or, optionally, if the argument type we're trying to decode does not match the stored type.
///
/// The result of the decoding operation must be stored into the provided 'pointer' rather than
/// returning a value. This pattern allows the runtime to use a heavily optimized, pre-allocated
/// buffer for all the arguments and their expected types. The 'pointer' passed here is a pointer
/// to a "slot" in that pre-allocated buffer. That buffer will then be passed to a thunk that
/// performs the actual distributed (local) instance method invocation.
mutating func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument
Decoding DistributedActor arguments using Codable
When using an actor system where ActorID
is Codable
, every distributed actor using that system is also implicitly Codable
(see DistributedActorSystem
). Such distributed actors are encoded as their ActorID
stored in an Encoder.singleValueContainer
. When Codable
is being used by such a system, the decodeNextArgument
method will be using Decoder
to decode the incoming values, which may themselves be distributed actors.
An actor system must be provided to the Decoder
in order for a distributed actor’s Decodable.init(from:)
to be able to return the instance of the actor. Specifically, the decodedActorID
is passed to the actor system’s resolve(id:as:)
method in order to return either a local instance identified by this ID, or creating a remote actor reference. Thus, you must set the actor system the decoding is performed for, on the decoder’s userInfo
, as follows:
mutating func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
let argumentData: Data = /// ...
// ...
decoder.userInfo[.actorSystemKey] = self.actorSystem
return try Argument.decode(
}
Requirements
associatedtype SerializationRequirement
The serialization requirement that the types passed to
decodeNextArgument
are required to conform to. The type returned bydecodeReturnType
is also expected to conform to this associated type requirement.func decodeErrorType(
) throws -> Any.Type? Decode the specific error type that the distributed invocation target has recorded. Currently this effectively can only ever be
Error.self
.func decodeGenericSubstitutions(
) throws -> [Any.Type] Decode all generic substitutions that were recorded for this invocation.
func decodeReturnType(
) throws -> Any.Type? Attempt to decode the known return type of the distributed invocation.