Skip to content

Commit

Permalink
Merge pull request Snapchat#166 from jb-gcx/gcx/use-std-expected
Browse files Browse the repository at this point in the history
Use std::expected for outcome when available
  • Loading branch information
li-feng-sc authored Apr 4, 2024
2 parents 9fcbec8 + fd1b314 commit ecffe7e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ outcome<RESULT, ERROR>
Where RESULT and ERROR can be any Djinni types (primitives or records).


In C++, the `outcome<>` type maps to the template class `djinni::expected<>`.
In C++, the `outcome<>` type maps to the template `djinni::expected<>`,
which is an alias of `std::expected<>` if supported or a drop-in replacement otherwise.
In Java, it maps to the generic class `com.snapchat.djinni.Outcome<>`. In ObjC,
it maps to the generic class `DJOutcome<>`.

Expand Down
32 changes: 30 additions & 2 deletions support-lib/cpp/expected.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
#pragma once

#ifdef __has_include
#if __has_include(<version>)
#include <version>
#endif
#endif

#include <utility>
#include <type_traits>

#ifdef __cpp_lib_expected

#include <expected>

namespace djinni {

using ::std::expected;
using ::std::unexpected;

}

#else

#include "tl_expected.hpp"

namespace djinni {

using ::tl::unexpected;
using ::tl::expected;

}

#endif

namespace djinni {

template <class E>
unexpected<typename std::decay<E>::type> make_unexpected(E &&e) {
return tl::unexpected<typename std::decay<E>::type>(std::forward<E>(e));
djinni::unexpected<typename std::decay<E>::type> make_unexpected(E &&e) {
return djinni::unexpected<typename std::decay<E>::type>(std::forward<E>(e));
}

}

0 comments on commit ecffe7e

Please sign in to comment.