| 1 |
// Copyright 2021 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_CORE_LIB_PROMISE_DETAIL_STATUS_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_DETAIL_STATUS_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <utility> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "absl/status/statusor.h" |
| 24 |
|
| 25 |
// Helpers for dealing with absl::Status/StatusOr generically |
| 26 |
|
| 27 |
namespace grpc_core { |
| 28 |
namespace promise_detail { |
| 29 |
|
| 30 |
// Convert with a move the input status to an absl::Status. |
| 31 |
template <typename T> |
| 32 |
absl::Status IntoStatus(absl::StatusOr<T>* status) { |
| 33 |
return std::move(status->status()); |
| 34 |
} |
| 35 |
|
| 36 |
// Convert with a move the input status to an absl::Status. |
| 37 |
inline absl::Status IntoStatus(absl::Status* status) { |
| 38 |
return std::move(*status); |
| 39 |
} |
| 40 |
|
| 41 |
} // namespace promise_detail |
| 42 |
|
| 43 |
// Return true if the status represented by the argument is ok, false if not. |
| 44 |
// By implementing this function for other, non-absl::Status types, those types |
| 45 |
// can participate in TrySeq as result types that affect control flow. |
| 46 |
inline bool IsStatusOk(const absl::Status& status) { return status.ok(); } |
| 47 |
|
| 48 |
template <typename To, typename From> |
| 49 |
struct StatusCastImpl; |
| 50 |
|
| 51 |
template <typename To> |
| 52 |
struct StatusCastImpl<To, To> { |
| 53 |
static To Cast(To&& t) { return std::move(t); } |
| 54 |
}; |
| 55 |
|
| 56 |
template <typename To> |
| 57 |
struct StatusCastImpl<To, const To&> { |
| 58 |
static To Cast(const To& t) { return t; } |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename T> |
| 62 |
struct StatusCastImpl<absl::StatusOr<T>, absl::Status> { |
| 63 |
static absl::StatusOr<T> Cast(absl::Status&& t) { return std::move(t); } |
| 64 |
}; |
| 65 |
|
| 66 |
template <typename T> |
| 67 |
struct StatusCastImpl<absl::StatusOr<T>, const absl::Status&> { |
| 68 |
static absl::StatusOr<T> Cast(const absl::Status& t) { return t; } |
| 69 |
}; |
| 70 |
|
| 71 |
template <typename To, typename From> |
| 72 |
To StatusCast(From&& from) { |
| 73 |
return StatusCastImpl<To, From>::Cast(std::forward<From>(from)); |
| 74 |
} |
| 75 |
|
| 76 |
} // namespace grpc_core |
| 77 |
|
| 78 |
#endif // GRPC_CORE_LIB_PROMISE_DETAIL_STATUS_H |