diff --git a/include/aws/lambda-runtime/outcome.h b/include/aws/lambda-runtime/outcome.h index e587e39..b5d0b8b 100644 --- a/include/aws/lambda-runtime/outcome.h +++ b/include/aws/lambda-runtime/outcome.h @@ -23,48 +23,72 @@ namespace lambda_runtime { template class outcome { public: - outcome(TResult const& s) : s(s), m_success(true) {} + outcome(TResult const& s) : m_s(s), m_success(true) {} + outcome(TResult&& s) : m_s(std::move(s)), m_success(true) {} - outcome(TFailure const& f) : f(f), m_success(false) {} + outcome(TFailure const& f) : m_f(f), m_success(false) {} + outcome(TFailure&& f) : m_f(std::move(f)), m_success(false) {} + + outcome(outcome const& other) : m_success(other.m_success) + { + if (m_success) { + new (&m_s) TResult(other.m_s); + } + else { + new (&m_f) TFailure(other.m_f); + } + } outcome(outcome&& other) noexcept : m_success(other.m_success) { if (m_success) { - s = std::move(other.s); + new (&m_s) TResult(std::move(other.m_s)); } else { - f = std::move(other.f); + new (&m_f) TFailure(std::move(other.m_f)); } } ~outcome() { if (m_success) { - s.~TResult(); + m_s.~TResult(); } else { - f.~TFailure(); + m_f.~TFailure(); } } - TResult const& get_result() const + TResult const& get_result() const& { assert(m_success); - return s; + return m_s; + } + + TResult&& get_result() && + { + assert(m_success); + return std::move(m_s); + } + + TFailure const& get_failure() const& + { + assert(!m_success); + return m_f; } - TFailure const& get_failure() const + TFailure&& get_failure() && { assert(!m_success); - return f; + return std::move(m_f); } bool is_success() const { return m_success; } private: union { - TResult s; - TFailure f; + TResult m_s; + TFailure m_f; }; bool m_success; }; diff --git a/include/aws/logging/logging.h b/include/aws/logging/logging.h index a3d9f50..0b5d0ef 100644 --- a/include/aws/logging/logging.h +++ b/include/aws/logging/logging.h @@ -27,7 +27,7 @@ enum class verbosity { void log(verbosity v, char const* tag, char const* msg, va_list args); -[[gnu::format(printf, 2, 3)]] static inline void log_error(char const* tag, char const* msg, ...) +[[gnu::format(printf, 2, 3)]] inline void log_error(char const* tag, char const* msg, ...) { va_list args; va_start(args, msg); @@ -37,7 +37,7 @@ void log(verbosity v, char const* tag, char const* msg, va_list args); (void)msg; } -[[gnu::format(printf, 2, 3)]] static inline void log_info(char const* tag, char const* msg, ...) +[[gnu::format(printf, 2, 3)]] inline void log_info(char const* tag, char const* msg, ...) { #if AWS_LAMBDA_LOG >= 1 va_list args; @@ -50,7 +50,7 @@ void log(verbosity v, char const* tag, char const* msg, va_list args); #endif } -[[gnu::format(printf, 2, 3)]] static inline void log_debug(char const* tag, char const* msg, ...) +[[gnu::format(printf, 2, 3)]] inline void log_debug(char const* tag, char const* msg, ...) { #if AWS_LAMBDA_LOG >= 2 va_list args; diff --git a/src/runtime.cpp b/src/runtime.cpp index 2c12542..36fe22b 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -412,7 +412,7 @@ void run_handler(std::function c size_t const max_retries = 3; while (retries < max_retries) { - const auto next_outcome = rt.get_next(); + auto next_outcome = rt.get_next(); if (!next_outcome.is_success()) { if (next_outcome.get_failure() == aws::http::response_code::REQUEST_NOT_MADE) { ++retries; @@ -429,7 +429,7 @@ void run_handler(std::function c retries = 0; - auto const& req = next_outcome.get_result(); + auto const req = std::move(next_outcome).get_result(); logging::log_info(LOG_TAG, "Invoking user handler"); invocation_response res = handler(req); logging::log_info(LOG_TAG, "Invoking user handler completed.");