Skip to content

[libc] add LIBC_INLINE for expected, use CTAD in abs_timeout #94348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions libc/src/__support/CPP/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H
#define LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H

#include "src/__support/macros/attributes.h"

namespace LIBC_NAMESPACE::cpp {

// This is used to hold an unexpected value so that a different constructor is
Expand All @@ -17,10 +19,12 @@ template <class T> class unexpected {
T value;

public:
constexpr explicit unexpected(T value) : value(value) {}
constexpr T error() { return value; }
LIBC_INLINE constexpr explicit unexpected(T value) : value(value) {}
LIBC_INLINE constexpr T error() { return value; }
};

template <class T> explicit unexpected(T) -> unexpected<T>;

template <class T, class E> class expected {
union {
T exp;
Expand All @@ -29,23 +33,23 @@ template <class T, class E> class expected {
bool is_expected;

public:
constexpr expected(T exp) : exp(exp), is_expected(true) {}
constexpr expected(unexpected<E> unexp)
LIBC_INLINE constexpr expected(T exp) : exp(exp), is_expected(true) {}
LIBC_INLINE constexpr expected(unexpected<E> unexp)
: unexp(unexp.error()), is_expected(false) {}

constexpr bool has_value() const { return is_expected; }
LIBC_INLINE constexpr bool has_value() const { return is_expected; }

constexpr T &value() { return exp; }
constexpr E &error() { return unexp; }
constexpr const T &value() const { return exp; }
constexpr const E &error() const { return unexp; }
LIBC_INLINE constexpr T &value() { return exp; }
LIBC_INLINE constexpr E &error() { return unexp; }
LIBC_INLINE constexpr const T &value() const { return exp; }
LIBC_INLINE constexpr const E &error() const { return unexp; }

constexpr operator bool() const { return is_expected; }
LIBC_INLINE constexpr operator bool() const { return is_expected; }

constexpr T &operator*() { return exp; }
constexpr const T &operator*() const { return exp; }
constexpr T *operator->() { return &exp; }
constexpr const T *operator->() const { return &exp; }
LIBC_INLINE constexpr T &operator*() { return exp; }
LIBC_INLINE constexpr const T &operator*() const { return exp; }
LIBC_INLINE constexpr T *operator->() { return &exp; }
LIBC_INLINE constexpr const T *operator->() const { return &exp; }
};

} // namespace LIBC_NAMESPACE::cpp
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/time/linux/abs_timeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class AbsTimeout {
from_timespec(timespec ts, bool realtime) {
using namespace time_units;
if (ts.tv_nsec < 0 || ts.tv_nsec >= 1_s_ns)
return cpp::unexpected<Error>(Error::Invalid);
return cpp::unexpected(Error::Invalid);

// POSIX allows tv_sec to be negative. We interpret this as an expired
// timeout.
if (ts.tv_sec < 0)
return cpp::unexpected<Error>(Error::BeforeEpoch);
return cpp::unexpected(Error::BeforeEpoch);

return AbsTimeout{ts, realtime};
}
Expand Down
Loading