Commit 641a5030 authored by Pranav Thulasiram Bhat's avatar Pranav Thulasiram Bhat Committed by Facebook GitHub Bot

Async traits

Summary: This diff implements traits to aid in the development of templated functions accepting annotated functions.

Reviewed By: yfeldblum, A5he

Differential Revision: D21770066

fbshipit-source-id: 40ae340aeeb484c4f9769c13b5c9a92a97723801
parent d1f59dd5
......@@ -16,6 +16,7 @@
#pragma once
#include <folly/Traits.h>
#include <glog/logging.h>
#include <utility>
......@@ -65,6 +66,8 @@ T&& await(Async<T>&&);
template <typename T>
class [[nodiscard]] Async {
public:
typedef T inner_type;
// General use constructor
template <typename... Us>
/* implicit */ Async(Us && ... val) : val_(std::forward<Us>(val)...) {}
......@@ -87,6 +90,8 @@ class [[nodiscard]] Async {
template <>
class [[nodiscard]] Async<void> {
public:
typedef void inner_type;
/* implicit */ Async() {}
Async(const Async&) = delete;
Async(Async && other) = default;
......@@ -124,6 +129,24 @@ inline void init_await(Async<void>&& async) {
await(std::move(async));
}
// is_async
template <typename T>
constexpr bool is_async_v = folly::detail::is_instantiation_of_v<Async, T>;
// async_inner_type
template <typename T>
struct async_inner_type {
using type = T;
};
template <typename T>
struct async_inner_type<Async<T>> {
using type = T;
};
template <typename T>
using async_inner_type_t = typename async_inner_type<T>::type;
} // namespace async
} // namespace fibers
} // namespace folly
......@@ -2791,3 +2791,12 @@ TEST(FiberManager, asyncTask) {
.getVia(&evb);
}
#endif
TEST(FiberManager, asyncTraits) {
static_assert(!async::is_async_v<int>);
static_assert(async::is_async_v<async::Async<int>>);
static_assert(
std::is_same<int, async::async_inner_type_t<async::Async<int>>>::value);
static_assert(
std::is_same<int&, async::async_inner_type_t<async::Async<int&>>>::value);
}
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