Coding.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /// Serializes a message into a sequence of bytes.
  17. ///
  18. /// Message serializers convert an input message to a sequence of bytes. Serializers are used to
  19. /// convert messages into a form which is suitable for sending over a network. The reverse
  20. /// operation, deserialization, is performed by a ``MessageDeserializer``.
  21. ///
  22. /// Serializers are used frequently and implementations should take care to ensure that
  23. /// serialization is as cheap as possible.
  24. @available(gRPCSwift 2.0, *)
  25. public protocol MessageSerializer<Message>: Sendable {
  26. /// The type of message this serializer can serialize.
  27. associatedtype Message
  28. /// Serializes a ``Message`` into a sequence of bytes.
  29. ///
  30. /// - Parameter message: The message to serialize.
  31. /// - Returns: The serialized bytes of a message.
  32. func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes
  33. }
  34. /// Deserializes a sequence of bytes into a message.
  35. ///
  36. /// Message deserializers convert a sequence of bytes into a message. Deserializers are used to
  37. /// convert bytes received from the network into an application specific message. The reverse
  38. /// operation, serialization, is performed by a ``MessageSerializer``.
  39. ///
  40. /// Deserializers are used frequently and implementations should take care to ensure that
  41. /// deserialization is as cheap as possible.
  42. @available(gRPCSwift 2.0, *)
  43. public protocol MessageDeserializer<Message>: Sendable {
  44. /// The type of message this deserializer can deserialize.
  45. associatedtype Message
  46. /// Deserializes a sequence of bytes into a ``Message``.
  47. ///
  48. /// - Parameter serializedMessageBytes: The bytes to deserialize.
  49. /// - Returns: The deserialized message.
  50. func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message
  51. }