зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1669421 - Make andThen accept a function that accepts a rvalue reference. r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D92552
This commit is contained in:
Родитель
2a20517cff
Коммит
bd5cfda423
|
@ -737,8 +737,8 @@ class MOZ_MUST_USE_TYPE Result final {
|
|||
* MOZ_ASSERT(res.unwrapErr() == res2.unwrapErr());
|
||||
*/
|
||||
template <typename F, typename = std::enable_if_t<detail::IsResult<
|
||||
decltype((*((F*)nullptr))(*((V*)nullptr)))>::value>>
|
||||
auto andThen(F f) -> decltype(f(*((V*)nullptr))) {
|
||||
std::invoke_result_t<F, V&&>>::value>>
|
||||
auto andThen(F f) -> std::invoke_result_t<F, V&&> {
|
||||
return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -532,13 +532,16 @@ static void OrElseTest() {
|
|||
|
||||
static void AndThenTest() {
|
||||
// `andThen`ing over success results.
|
||||
{
|
||||
Result<int, const char*> r1(10);
|
||||
Result<int, const char*> r2 =
|
||||
r1.andThen([](int x) { return Result<int, const char*>(x + 1); });
|
||||
MOZ_RELEASE_ASSERT(r2.isOk());
|
||||
MOZ_RELEASE_ASSERT(r2.unwrap() == 11);
|
||||
}
|
||||
|
||||
// `andThen`ing over error results.
|
||||
{
|
||||
Result<int, const char*> r3("error");
|
||||
Result<int, const char*> r4 = r3.andThen([](int x) {
|
||||
MOZ_RELEASE_ASSERT(false);
|
||||
|
@ -548,6 +551,16 @@ static void AndThenTest() {
|
|||
MOZ_RELEASE_ASSERT(r3.unwrapErr() == r4.unwrapErr());
|
||||
}
|
||||
|
||||
// andThen with a function accepting an rvalue
|
||||
{
|
||||
Result<int, const char*> r1(10);
|
||||
Result<int, const char*> r2 =
|
||||
r1.andThen([](int&& x) { return Result<int, const char*>(x + 1); });
|
||||
MOZ_RELEASE_ASSERT(r2.isOk());
|
||||
MOZ_RELEASE_ASSERT(r2.unwrap() == 11);
|
||||
}
|
||||
}
|
||||
|
||||
using UniqueResult = Result<UniquePtr<int>, const char*>;
|
||||
|
||||
static UniqueResult UniqueTask() { return mozilla::MakeUnique<int>(3); }
|
||||
|
|
Загрузка…
Ссылка в новой задаче