Use negative error codes in Result.

GitOrigin-RevId: 3ee4ccc424062be87198b2d706b791d5bf8e2020
This commit is contained in:
levlam 2018-07-08 22:00:54 +03:00
parent 0c907b38a8
commit 6e57c13f43
2 changed files with 26 additions and 24 deletions

View File

@ -58,26 +58,6 @@ class PromiseInterface {
}
};
template <class T = Unit>
class FutureInterface {
public:
FutureInterface() = default;
FutureInterface(const FutureInterface &) = delete;
FutureInterface &operator=(const FutureInterface &) = delete;
FutureInterface(FutureInterface &&) = default;
FutureInterface &operator=(FutureInterface &&) = default;
virtual ~FutureInterface() = default;
virtual bool is_ready() = 0;
virtual bool is_ok() = 0;
virtual bool is_error() = 0;
virtual const T &ok() = 0;
virtual T move_as_ok() = 0;
virtual const Status &error() = 0;
virtual Status move_as_error() TD_WARN_UNUSED_RESULT = 0;
virtual const Result<T> &result() = 0;
virtual Result<T> move_as_result() TD_WARN_UNUSED_RESULT = 0;
};
template <class T>
class SafePromise;

View File

@ -173,6 +173,18 @@ class Status {
return ptr_ != nullptr;
}
#ifdef TD_STATUS_NO_ENSURE
void ensure() const {
if (!is_ok()) {
LOG(FATAL) << "Unexpected Status " << to_string();
}
}
void ensure_error() const {
if (is_ok()) {
LOG(FATAL) << "Unexpected Status::OK";
}
}
#else
void ensure_impl(CSlice file_name, int line) const {
if (!is_ok()) {
LOG(FATAL) << "Unexpected Status " << to_string() << " in file " << file_name << " at line " << line;
@ -183,6 +195,7 @@ class Status {
LOG(FATAL) << "Unexpected Status::OK in file " << file_name << " at line " << line;
}
}
#endif
void ignore() const {
// nop
@ -311,7 +324,7 @@ class Status {
template <class T = Unit>
class Result {
public:
Result() : status_(Status::Error<1>()) {
Result() : status_(Status::Error<-1>()) {
}
template <class S, std::enable_if_t<!std::is_same<std::decay_t<S>, Result>::value, int> = 0>
Result(S &&x) : status_(), value_(std::forward<S>(x)) {
@ -326,7 +339,7 @@ class Result {
new (&value_) T(std::move(other.value_));
other.value_.~T();
}
other.status_ = Status::Error<2>();
other.status_ = Status::Error<-2>();
}
Result &operator=(Result &&other) {
if (status_.is_ok()) {
@ -344,7 +357,7 @@ class Result {
other.value_.~T();
}
status_ = std::move(other.status_);
other.status_ = Status::Error<3>();
other.status_ = Status::Error<-3>();
return *this;
}
~Result() {
@ -353,12 +366,21 @@ class Result {
}
}
#ifdef TD_STATUS_NO_ENSURE
void ensure() const {
status_.ensure();
}
void ensure_error() const {
status_.ensure_error();
}
#else
void ensure_impl(CSlice file_name, int line) const {
status_.ensure_impl(file_name, line);
}
void ensure_error_impl(CSlice file_name, int line) const {
status_.ensure_error_impl(file_name, line);
}
#endif
void ignore() const {
status_.ignore();
}
@ -375,7 +397,7 @@ class Result {
Status move_as_error() TD_WARN_UNUSED_RESULT {
CHECK(status_.is_error());
SCOPE_EXIT {
status_ = Status::Error<4>();
status_ = Status::Error<-4>();
};
return std::move(status_);
}