Bug 1666416 - Allow conversion from Result with convertible error type. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D90960
This commit is contained in:
Simon Giesecke 2020-09-22 11:24:08 +00:00
parent ee5a559a7d
commit 8804eb9be3
2 changed files with 19 additions and 0 deletions

View File

@ -489,6 +489,15 @@ class MOZ_MUST_USE_TYPE Result final {
MOZ_ASSERT(isErr());
}
/**
* Create a (success/error) result from another (success/error) result with a
* different but convertible error type. */
template <typename E2,
typename = std::enable_if_t<std::is_convertible_v<E2, E>>>
MOZ_IMPLICIT Result(Result<V, E2>&& aOther)
: mImpl(aOther.isOk() ? Impl{aOther.unwrap()}
: Impl{aOther.unwrapErr()}) {}
/**
* Implementation detail of MOZ_TRY().
* Create an error result from another error result.

View File

@ -268,6 +268,16 @@ static Result<Ok, Failed*> ErrorGeneralization() {
static void TypeConversionTests() {
MOZ_RELEASE_ASSERT(ErrorGeneralization().isErr());
{
const Result<Ok, Failed*> res = Explode();
MOZ_RELEASE_ASSERT(res.isErr());
}
{
const Result<Ok, Failed*> res = Result<Ok, Snafu*>{Ok{}};
MOZ_RELEASE_ASSERT(res.isOk());
}
}
static void EmptyValueTest() {