xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/transport/metadata.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_LIB_TRANSPORT_METADATA_H
16 #define GRPC_SRC_CORE_LIB_TRANSPORT_METADATA_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include "src/core/lib/transport/metadata_batch.h"
21 
22 namespace grpc_core {
23 
24 // Server metadata type
25 // TODO(ctiller): This should be a bespoke instance of MetadataMap<>
26 using ServerMetadata = grpc_metadata_batch;
27 using ServerMetadataHandle = Arena::PoolPtr<ServerMetadata>;
28 
29 // Client initial metadata type
30 // TODO(ctiller): This should be a bespoke instance of MetadataMap<>
31 using ClientMetadata = grpc_metadata_batch;
32 using ClientMetadataHandle = Arena::PoolPtr<ClientMetadata>;
33 
34 // Ok/not-ok check for trailing metadata, so that it can be used as result types
35 // for TrySeq.
IsStatusOk(const ServerMetadataHandle & m)36 inline bool IsStatusOk(const ServerMetadataHandle& m) {
37   return m->get(GrpcStatusMetadata()).value_or(GRPC_STATUS_UNKNOWN) ==
38          GRPC_STATUS_OK;
39 }
40 
41 ServerMetadataHandle ServerMetadataFromStatus(
42     const absl::Status& status, Arena* arena = GetContext<Arena>());
43 
44 template <>
45 struct StatusCastImpl<ServerMetadataHandle, absl::Status> {
46   static ServerMetadataHandle Cast(const absl::Status& m) {
47     return ServerMetadataFromStatus(m);
48   }
49 };
50 
51 template <>
52 struct StatusCastImpl<ServerMetadataHandle, const absl::Status&> {
53   static ServerMetadataHandle Cast(const absl::Status& m) {
54     return ServerMetadataFromStatus(m);
55   }
56 };
57 
58 template <>
59 struct StatusCastImpl<ServerMetadataHandle, absl::Status&> {
60   static ServerMetadataHandle Cast(const absl::Status& m) {
61     return ServerMetadataFromStatus(m);
62   }
63 };
64 
65 // Anything that can be first cast to absl::Status can then be cast to
66 // ServerMetadataHandle.
67 template <typename T>
68 struct StatusCastImpl<
69     ServerMetadataHandle, T,
70     absl::void_t<decltype(&StatusCastImpl<absl::Status, T>::Cast)>> {
71   static ServerMetadataHandle Cast(const T& m) {
72     return ServerMetadataFromStatus(StatusCast<absl::Status>(m));
73   }
74 };
75 
76 }  // namespace grpc_core
77 
78 #endif  // GRPC_SRC_CORE_LIB_TRANSPORT_METADATA_H
79