Skip to content

Commit fb300eb

Browse files
authored
[libc] add LIBC_INLINE for expected, use CTAD in abs_timeout (llvm#94348)
1 parent 78f5d9c commit fb300eb

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

libc/src/__support/CPP/expected.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CPP_EXPECTED_H
1111

12+
#include "src/__support/macros/attributes.h"
13+
1214
namespace LIBC_NAMESPACE::cpp {
1315

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

1921
public:
20-
constexpr explicit unexpected(T value) : value(value) {}
21-
constexpr T error() { return value; }
22+
LIBC_INLINE constexpr explicit unexpected(T value) : value(value) {}
23+
LIBC_INLINE constexpr T error() { return value; }
2224
};
2325

26+
template <class T> explicit unexpected(T) -> unexpected<T>;
27+
2428
template <class T, class E> class expected {
2529
union {
2630
T exp;
@@ -29,23 +33,23 @@ template <class T, class E> class expected {
2933
bool is_expected;
3034

3135
public:
32-
constexpr expected(T exp) : exp(exp), is_expected(true) {}
33-
constexpr expected(unexpected<E> unexp)
36+
LIBC_INLINE constexpr expected(T exp) : exp(exp), is_expected(true) {}
37+
LIBC_INLINE constexpr expected(unexpected<E> unexp)
3438
: unexp(unexp.error()), is_expected(false) {}
3539

36-
constexpr bool has_value() const { return is_expected; }
40+
LIBC_INLINE constexpr bool has_value() const { return is_expected; }
3741

38-
constexpr T &value() { return exp; }
39-
constexpr E &error() { return unexp; }
40-
constexpr const T &value() const { return exp; }
41-
constexpr const E &error() const { return unexp; }
42+
LIBC_INLINE constexpr T &value() { return exp; }
43+
LIBC_INLINE constexpr E &error() { return unexp; }
44+
LIBC_INLINE constexpr const T &value() const { return exp; }
45+
LIBC_INLINE constexpr const E &error() const { return unexp; }
4246

43-
constexpr operator bool() const { return is_expected; }
47+
LIBC_INLINE constexpr operator bool() const { return is_expected; }
4448

45-
constexpr T &operator*() { return exp; }
46-
constexpr const T &operator*() const { return exp; }
47-
constexpr T *operator->() { return &exp; }
48-
constexpr const T *operator->() const { return &exp; }
49+
LIBC_INLINE constexpr T &operator*() { return exp; }
50+
LIBC_INLINE constexpr const T &operator*() const { return exp; }
51+
LIBC_INLINE constexpr T *operator->() { return &exp; }
52+
LIBC_INLINE constexpr const T *operator->() const { return &exp; }
4953
};
5054

5155
} // namespace LIBC_NAMESPACE::cpp

libc/src/__support/time/linux/abs_timeout.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ class AbsTimeout {
3333
from_timespec(timespec ts, bool realtime) {
3434
using namespace time_units;
3535
if (ts.tv_nsec < 0 || ts.tv_nsec >= 1_s_ns)
36-
return cpp::unexpected<Error>(Error::Invalid);
36+
return cpp::unexpected(Error::Invalid);
3737

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

4343
return AbsTimeout{ts, realtime};
4444
}

0 commit comments

Comments
 (0)