Thanks for getting this landed! Hooray for better error handling! \o/ On Tue, Dec 20, 2016 at 5:46 AM, Jan de Mooij <jdemo...@mozilla.com> wrote:
> Hi all, > > A few weeks ago we added mozilla::Result<V, E> to MFBT [0][1]. I was asked > to inform dev-platform about this, so here's a quick overview. > mozilla::Result<V, E> is based on Rust's Result type [2]. It contains > either a success value of type V or an error value of type E. For example, > a function Foo that returns an `int` on success or some `Error` (enum, > pointer, etc) on failure, would have this signature: > > Result<int, Error> Foo(); > > mozilla::Ok is an empty struct that can be used when a function doesn't > return anything on success: > > Result<Ok, Error> Bar() { ... return Ok(); } > > The MOZ_TRY(expr) macro is similar to Rust's try! macro: if `expr` is an > error it propagates it to the caller, else it continues: > > Result<V, E> Baz() { MOZ_TRY(Bar()); ... } > > There's also a MOZ_TRY_VAR macro that can be used when you want to store > the return value on success. Result has isOk(), isErr(), unwrapOk(), > unwrapErr() methods that do what you'd expect. It also has the > MOZ_MUST_USE_TYPE annotation, so the static analysis builds will complain > if you ignore the return value of a function that returns Result. > > Internally, Result uses mozilla::Variant, but there are some cases that can > be stored more efficiently. For instance, Result<Ok, Error&> just stores an > Error* pointer and Ok is represented as nullptr. This is more efficient and > will also make it easier to call functions that return Result from JIT > code. The documentation [3] has more info. > > The long-term plan is to use Result in SpiderMonkey, to simplify our > error/OOM handling [4][5]. > > Many thanks to Jason Orendorff (jorendorff) for doing most of the work by > writing the initial Result patches, and to Jeff Walden (Waldo) for his > thorough code reviews. > > Jan > > [0] https://bugzilla.mozilla.org/show_bug.cgi?id=1283562 > [1] https://searchfox.org/mozilla-central/source/mfbt/Result.h > [2] https://doc.rust-lang.org/std/result/ > [3] > https://searchfox.org/mozilla-central/rev/cc2a84852bd4e6f6d8d4d5b17b8382 > bb5d005749/mfbt/Result.h#148-158 > [4] https://bugzilla.mozilla.org/show_bug.cgi?id=1277368 > [5] https://searchfox.org/mozilla-central/source/js/public/Result.h > _______________________________________________ > dev-platform mailing list > dev-platform@lists.mozilla.org > https://lists.mozilla.org/listinfo/dev-platform > _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform