Commit da24dd6f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Extended constructibility for co_error

Summary:
[Folly] Extended constructibility for `co_error` - constructible from everything `exception_wrapper` is constructible from, but not default-constructible. This permits:

```lang=c++
co_yield folly::coro::co_error(std::runtime_error("foo"));
```

Reviewed By: lewissbaker

Differential Revision: D19172210

fbshipit-source-id: df21c4f2a15ad2286e7cb34aae7c1be05e797a94
parent e6eff650
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#pragma once #pragma once
#include <type_traits>
#include <folly/ExceptionWrapper.h> #include <folly/ExceptionWrapper.h>
namespace folly { namespace folly {
...@@ -23,7 +25,14 @@ namespace coro { ...@@ -23,7 +25,14 @@ namespace coro {
class co_error { class co_error {
public: public:
explicit co_error(exception_wrapper ex) : ex_(std::move(ex)) {} template <
typename... A,
std::enable_if_t<
sizeof...(A) && std::is_constructible<exception_wrapper, A...>::value,
int> = 0>
explicit co_error(A&&... a) noexcept(
std::is_nothrow_constructible<exception_wrapper, A...>::value)
: ex_(static_cast<A&&>(a)...) {}
const exception_wrapper& exception() const { const exception_wrapper& exception() const {
return ex_; return ex_;
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/Portability.h>
#if FOLLY_HAS_COROUTINES
#include <folly/experimental/coro/Error.h>
#include <type_traits>
#include <folly/ExceptionWrapper.h>
#include <folly/Utility.h>
#include <folly/portability/GTest.h>
class CoErrorTest : public testing::Test {};
TEST_F(CoErrorTest, constructible) {
using namespace folly;
using namespace folly::coro;
EXPECT_TRUE((std::is_constructible_v<co_error, exception_wrapper>));
EXPECT_TRUE((std::is_constructible_v<co_error, std::runtime_error>));
EXPECT_TRUE((std::is_constructible_v<
co_error,
in_place_type_t<std::runtime_error>,
std::string>));
EXPECT_FALSE((std::is_constructible_v<co_error, int>));
}
#endif
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