|
| 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 | + |
| 17 | +/// Serializes a message into a sequence of bytes. |
| 18 | +/// |
| 19 | +/// Message serializers convert an input message to a sequence of bytes. Serializers are used to |
| 20 | +/// convert messages into a form which is suitable for sending over a network. The reverse |
| 21 | +/// operation, deserialization, is performed by a ``MessageDeserializer``. |
| 22 | +/// |
| 23 | +/// Serializers are used frequently and implementations should take care to ensure that |
| 24 | +/// serialization is as cheap as possible. |
| 25 | +public protocol MessageSerializer<Message>: Sendable { |
| 26 | + /// The type of message this serializer can serialize. |
| 27 | + associatedtype Message |
| 28 | + |
| 29 | + /// Serializes a ``Message`` into a sequence of bytes. |
| 30 | + /// |
| 31 | + /// - Parameter message: The message to serialize. |
| 32 | + /// - Returns: The serialized bytes of a message. |
| 33 | + func serialize(_ message: Message) throws -> [UInt8] |
| 34 | +} |
| 35 | + |
| 36 | +/// Deserializes a sequence of bytes into a message. |
| 37 | +/// |
| 38 | +/// Message deserializers convert a sequence of bytes into a message. Deserializers are used to |
| 39 | +/// convert bytes received from the network into an application specific message. The reverse |
| 40 | +/// operation, serialization, is performed by a ``MessageSerializer``. |
| 41 | +/// |
| 42 | +/// Deserializers are used frequently and implementations should take care to ensure that |
| 43 | +/// deserialization is as cheap as possible. |
| 44 | +public protocol MessageDeserializer<Message>: Sendable { |
| 45 | + /// The type of message this deserializer can deserialize. |
| 46 | + associatedtype Message |
| 47 | + |
| 48 | + /// Deserializes a sequence of bytes into a ``Message``. |
| 49 | + /// |
| 50 | + /// - Parameter serializedMessageBytes: The bytes to deserialize. |
| 51 | + /// - Returns: The deserialized message. |
| 52 | + func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message |
| 53 | +} |
0 commit comments