Commit dc0479f3 by Ken Oslund Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 352628261
parent c192cb07
...@@ -106,8 +106,8 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> { ...@@ -106,8 +106,8 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> {
private: private:
template <typename CType> template <typename CType>
static handle cast_impl(CType src, return_value_policy policy, handle parent, static handle cast_impl(CType&& src, return_value_policy policy,
bool throw_exception) { handle parent, bool throw_exception) {
google::CheckStatusModuleImported(); google::CheckStatusModuleImported();
if (!throw_exception) { if (!throw_exception) {
// Use the built-in/standard pybind11 caster. // Use the built-in/standard pybind11 caster.
......
...@@ -16,7 +16,8 @@ namespace pybind11 { ...@@ -16,7 +16,8 @@ namespace pybind11 {
namespace google { namespace google {
// Wrapper type to signal to the type_caster that a non-ok status should not // Wrapper type to signal to the type_caster that a non-ok status should not
// be converted into an object rather than a thrown exception. // be converted into an object rather than a thrown exception. StatusType can
// encapsulate references, e.g. `NoThrowStatus<absl::Status&>`.
template <typename StatusType> template <typename StatusType>
struct NoThrowStatus { struct NoThrowStatus {
NoThrowStatus(StatusType status_in) NoThrowStatus(StatusType status_in)
...@@ -24,7 +25,11 @@ struct NoThrowStatus { ...@@ -24,7 +25,11 @@ struct NoThrowStatus {
StatusType status; StatusType status;
}; };
// Convert a absl::Status(Or) into a NoThrowStatus. // Convert a absl::Status(Or) into a NoThrowStatus. To use this with references,
// explicitly specify the template parameter rather deducing it, e.g.:
// `return DoNotThrowStatus<const absl::Status&>(my_status);`
// When returning a status by value (by far the most common case), deducing the
// template parameter is fine, e.g.: `return DoNotThrowStatus(my_status);`
template <typename StatusType> template <typename StatusType>
NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) { NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) {
return NoThrowStatus<StatusType>(std::forward<StatusType>(status)); return NoThrowStatus<StatusType>(std::forward<StatusType>(status));
...@@ -34,7 +39,7 @@ NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) { ...@@ -34,7 +39,7 @@ NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) {
template <typename StatusType, typename... Args> template <typename StatusType, typename... Args>
std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus( std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
std::function<StatusType(Args...)> f) { std::function<StatusType(Args...)> f) {
return [f = std::move(f)](Args... args) { return [f = std::move(f)](Args&&... args) {
return NoThrowStatus<StatusType>( return NoThrowStatus<StatusType>(
std::forward<StatusType>(f(std::forward<Args>(args)...))); std::forward<StatusType>(f(std::forward<Args>(args)...)));
}; };
...@@ -42,7 +47,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus( ...@@ -42,7 +47,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
template <typename StatusType, typename... Args> template <typename StatusType, typename... Args>
std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus( std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
StatusType (*f)(Args...)) { StatusType (*f)(Args...)) {
return [f](Args... args) { return [f](Args&&... args) {
return NoThrowStatus<StatusType>( return NoThrowStatus<StatusType>(
std::forward<StatusType>(f(std::forward<Args>(args)...))); std::forward<StatusType>(f(std::forward<Args>(args)...)));
}; };
...@@ -50,7 +55,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus( ...@@ -50,7 +55,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
template <typename StatusType, typename Class, typename... Args> template <typename StatusType, typename Class, typename... Args>
std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus( std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus(
StatusType (Class::*f)(Args...)) { StatusType (Class::*f)(Args...)) {
return [f](Class* c, Args... args) { return [f](Class* c, Args&&... args) {
return NoThrowStatus<StatusType>( return NoThrowStatus<StatusType>(
std::forward<StatusType>((c->*f)(std::forward<Args>(args)...))); std::forward<StatusType>((c->*f)(std::forward<Args>(args)...)));
}; };
...@@ -58,7 +63,7 @@ std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus( ...@@ -58,7 +63,7 @@ std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus(
template <typename StatusType, typename Class, typename... Args> template <typename StatusType, typename Class, typename... Args>
std::function<NoThrowStatus<StatusType>(const Class*, Args...)> std::function<NoThrowStatus<StatusType>(const Class*, Args...)>
DoNotThrowStatus(StatusType (Class::*f)(Args...) const) { DoNotThrowStatus(StatusType (Class::*f)(Args...) const) {
return [f](const Class* c, Args... args) { return [f](const Class* c, Args&&... args) {
return NoThrowStatus<StatusType>( return NoThrowStatus<StatusType>(
std::forward<StatusType>((c->*f)(std::forward<Args>(args)...))); std::forward<StatusType>((c->*f)(std::forward<Args>(args)...)));
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment