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> {
private:
template <typename CType>
static handle cast_impl(CType src, return_value_policy policy, handle parent,
bool throw_exception) {
static handle cast_impl(CType&& src, return_value_policy policy,
handle parent, bool throw_exception) {
google::CheckStatusModuleImported();
if (!throw_exception) {
// Use the built-in/standard pybind11 caster.
......
......@@ -16,7 +16,8 @@ namespace pybind11 {
namespace google {
// 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>
struct NoThrowStatus {
NoThrowStatus(StatusType status_in)
......@@ -24,7 +25,11 @@ struct NoThrowStatus {
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>
NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) {
return NoThrowStatus<StatusType>(std::forward<StatusType>(status));
......@@ -34,7 +39,7 @@ NoThrowStatus<StatusType> DoNotThrowStatus(StatusType status) {
template <typename StatusType, typename... Args>
std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
std::function<StatusType(Args...)> f) {
return [f = std::move(f)](Args... args) {
return [f = std::move(f)](Args&&... args) {
return NoThrowStatus<StatusType>(
std::forward<StatusType>(f(std::forward<Args>(args)...)));
};
......@@ -42,7 +47,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
template <typename StatusType, typename... Args>
std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
StatusType (*f)(Args...)) {
return [f](Args... args) {
return [f](Args&&... args) {
return NoThrowStatus<StatusType>(
std::forward<StatusType>(f(std::forward<Args>(args)...)));
};
......@@ -50,7 +55,7 @@ std::function<NoThrowStatus<StatusType>(Args...)> DoNotThrowStatus(
template <typename StatusType, typename Class, typename... Args>
std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus(
StatusType (Class::*f)(Args...)) {
return [f](Class* c, Args... args) {
return [f](Class* c, Args&&... args) {
return NoThrowStatus<StatusType>(
std::forward<StatusType>((c->*f)(std::forward<Args>(args)...)));
};
......@@ -58,7 +63,7 @@ std::function<NoThrowStatus<StatusType>(Class*, Args...)> DoNotThrowStatus(
template <typename StatusType, typename Class, typename... Args>
std::function<NoThrowStatus<StatusType>(const Class*, Args...)>
DoNotThrowStatus(StatusType (Class::*f)(Args...) const) {
return [f](const Class* c, Args... args) {
return [f](const Class* c, Args&&... args) {
return NoThrowStatus<StatusType>(
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