summaryrefslogtreecommitdiff
path: root/AK/Try.h
blob: 8db09a950a2d2fb36420b341e5db70bc73ae64c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

// NOTE: This macro works with any result type that has the expected APIs.
//       It's designed with AK::Result and AK::Error in mind.

#define TRY(expression)                               \
    ({                                                \
        auto _temporary_result = (expression);        \
        if (_temporary_result.is_error())             \
            return _temporary_result.release_error(); \
        _temporary_result.release_value();            \
    })

#define MUST(expression)                       \
    ({                                         \
        auto _temporary_result = (expression); \
        VERIFY(!_temporary_result.is_error()); \
        _temporary_result.release_value();     \
    })