Coding.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. public protocol MessageSerializer<Message>: Sendable {
  25. /// The type of message this serializer can serialize.
  26. associatedtype Message
  27. /// Serializes a ``Message`` into a sequence of bytes.
  28. ///
  29. /// - Parameter message: The message to serialize.
  30. /// - Returns: The serialized bytes of a message.
  31. func serialize(_ message: Message) throws -> [UInt8]
  32. }
  33. /// Deserializes a sequence of bytes into a message.
  34. ///
  35. /// Message deserializers convert a sequence of bytes into a message. Deserializers are used to
  36. /// convert bytes received from the network into an application specific message. The reverse
  37. /// operation, serialization, is performed by a ``MessageSerializer``.
  38. ///
  39. /// Deserializers are used frequently and implementations should take care to ensure that
  40. /// deserialization is as cheap as possible.
  41. public protocol MessageDeserializer<Message>: Sendable {
  42. /// The type of message this deserializer can deserialize.
  43. associatedtype Message
  44. /// Deserializes a sequence of bytes into a ``Message``.
  45. ///
  46. /// - Parameter serializedMessageBytes: The bytes to deserialize.
  47. /// - Returns: The deserialized message.
  48. func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message
  49. }