Files
Library/include/Void/Functional/Adapters.hpp
T

132 lines
3.7 KiB
C++

#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <cerrno>
#include <concepts>
#include <cstring>
#include <exception>
#include <expected>
#include <functional>
#include <new>
#include <optional>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
namespace Void::Result
{
namespace Detail
{
template <typename T> struct is_expected_type : std::false_type {};
template <typename T, typename E>
struct is_expected_type<std::expected<T, E>> : std::true_type {};
template <typename T>
inline constexpr bool is_expected_v =
is_expected_type<std::remove_cvref_t<T>>::value;
} // namespace Detail
/// Convert `std::expected<T, E>` into `Result<T>` by invoking `map_error`
/// only on the error path. Supports `void` and move-only value types.
template <typename T, typename E, typename Map>
requires std::invocable<Map, E> &&
std::convertible_to<std::invoke_result_t<Map, E>, Error>
[[nodiscard]] constexpr Result<T> adapt(std::expected<T, E> exp,
Map&& map_error)
{
if (exp.has_value()) {
if constexpr (std::is_void_v<T>) {
return {};
} else {
return Result<T>{std::move(*exp)};
}
}
return Failure<Error>{static_cast<Error>(
std::invoke(std::forward<Map>(map_error), std::move(exp).error()))};
}
/// Invoke `fn()`, catching only exceptions accepted by
/// `map_exception(std::exception_ptr) -> std::optional<Error>`.
/// `std::bad_alloc` and unrecognized exceptions ALWAYS propagate.
template <typename F, typename Map>
requires std::invocable<F> &&
std::invocable<Map, std::exception_ptr> &&
std::convertible_to<
std::invoke_result_t<Map, std::exception_ptr>,
std::optional<Error>>
[[nodiscard]] auto try_invoke(F&& fn, Map&& map_exception)
{
using Raw = std::invoke_result_t<F>;
using Out = std::conditional_t<Detail::is_expected_v<Raw>, Raw,
Result<Raw>>;
try {
if constexpr (std::is_void_v<Raw>) {
std::invoke(std::forward<F>(fn));
return Out{};
} else if constexpr (Detail::is_expected_v<Raw>) {
return std::invoke(std::forward<F>(fn));
} else {
return Out{std::invoke(std::forward<F>(fn))};
}
} catch (const std::bad_alloc&) {
throw;
} catch (...) {
std::optional<Error> mapped = std::invoke(
std::forward<Map>(map_exception), std::current_exception());
if (mapped.has_value()) {
return Out{std::unexpected{std::move(*mapped)}};
}
throw;
}
}
/// Map a POSIX errno value into a classified Error carrying `std::error_code`.
[[nodiscard]] inline Error from_errno(int errnum, std::string message = {})
{
ErrorKind kind = ErrorKind::io;
switch (errnum) {
case EINVAL:
case EDOM:
case ERANGE: kind = ErrorKind::invalid_argument; break;
case ENOENT:
case ESRCH: kind = ErrorKind::not_found; break;
case EEXIST:
case EALREADY:
case EBUSY: kind = ErrorKind::conflict; break;
case ECANCELED: kind = ErrorKind::cancelled; break;
case ETIMEDOUT: kind = ErrorKind::timeout; break;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
kind = ErrorKind::overloaded;
break;
case ENOSPC:
case ENOMEM:
case EMFILE:
case ENFILE: kind = ErrorKind::exhausted; break;
case ENOSYS:
case ENOTSUP:
#if defined(EOPNOTSUPP) && EOPNOTSUPP != ENOTSUP
case EOPNOTSUPP:
#endif
kind = ErrorKind::unsupported;
break;
case EILSEQ:
case EPROTO:
case EBADMSG: kind = ErrorKind::corrupt; break;
default: kind = ErrorKind::io; break;
}
if (message.empty()) {
message = std::strerror(errnum);
}
return Error{
Status{kind, ErrorCode{kSystemErrorDomain, errnum}},
std::move(message),
std::error_code{errnum, std::generic_category()}};
}
} // namespace Void::Result