-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add ffi::Expected<T> for exception-free error handling #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guan404ming
wants to merge
2
commits into
apache:main
Choose a base branch
from
guan404ming:feat/expected-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+640
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file tvm/ffi/expected.h | ||
| * \brief Runtime Expected container type for exception-free error handling. | ||
| */ | ||
| #ifndef TVM_FFI_EXPECTED_H_ | ||
| #define TVM_FFI_EXPECTED_H_ | ||
|
|
||
| #include <tvm/ffi/any.h> | ||
| #include <tvm/ffi/error.h> | ||
|
|
||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| namespace tvm { | ||
| namespace ffi { | ||
|
|
||
| /*! | ||
| * \brief Expected<T> provides exception-free error handling for FFI functions. | ||
| * | ||
| * Expected<T> is similar to Rust's Result<T, Error> or C++23's std::expected. | ||
| * It can hold either a success value of type T or an error of type Error. | ||
| * | ||
| * \tparam T The success type. Must be Any-compatible and cannot be Error. | ||
| * | ||
| * Usage: | ||
| * \code | ||
| * Expected<int> divide(int a, int b) { | ||
| * if (b == 0) { | ||
| * return ExpectedErr(Error("ValueError", "Division by zero")); | ||
| * } | ||
| * return ExpectedOk(a / b); | ||
| * } | ||
| * | ||
| * Expected<int> result = divide(10, 2); | ||
| * if (result.is_ok()) { | ||
| * int value = result.value(); | ||
| * } else { | ||
| * Error err = result.error(); | ||
| * } | ||
| * \endcode | ||
| */ | ||
| template <typename T> | ||
| class Expected { | ||
| public: | ||
| static_assert(!std::is_same_v<T, Error>, "Expected<Error> is not allowed. Use Error directly."); | ||
|
|
||
| /*! | ||
| * \brief Create an Expected with a success value. | ||
| * \param value The success value. | ||
| * \return Expected containing the success value. | ||
| */ | ||
| static Expected Ok(T value) { return Expected(Any(std::move(value))); } | ||
|
|
||
| /*! | ||
| * \brief Create an Expected with an error. | ||
| * \param error The error value. | ||
| * \return Expected containing the error. | ||
| */ | ||
| static Expected Err(Error error) { return Expected(Any(std::move(error))); } | ||
|
|
||
| /*! | ||
| * \brief Check if the Expected contains a success value. | ||
| * \return True if contains success value, false if contains error. | ||
| * \note Checks for Error first to handle cases where T is a base class of Error. | ||
| */ | ||
| TVM_FFI_INLINE bool is_ok() const { return !data_.as<Error>().has_value(); } | ||
|
|
||
| /*! | ||
| * \brief Check if the Expected contains an error. | ||
| * \return True if contains error, false if contains success value. | ||
| */ | ||
| TVM_FFI_INLINE bool is_err() const { return !is_ok(); } | ||
|
|
||
| /*! | ||
| * \brief Alias for is_ok(). | ||
| * \return True if contains success value. | ||
| */ | ||
| TVM_FFI_INLINE bool has_value() const { return is_ok(); } | ||
|
|
||
| /*! \brief Access the success value. Throws the contained error if is_err(). */ | ||
| TVM_FFI_INLINE T value() const& { | ||
| if (is_err()) throw data_.cast<Error>(); | ||
| return data_.cast<T>(); | ||
| } | ||
| /*! \brief Access the success value (rvalue). Throws the contained error if is_err(). */ | ||
| TVM_FFI_INLINE T value() && { | ||
| if (is_err()) throw std::move(data_).template cast<Error>(); | ||
| return std::move(data_).template cast<T>(); | ||
| } | ||
|
|
||
| /*! \brief Access the error. Throws RuntimeError if is_ok(). */ | ||
| TVM_FFI_INLINE Error error() const& { | ||
| if (!is_err()) TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains value, not error"; | ||
| return data_.cast<Error>(); | ||
| } | ||
| /*! \brief Access the error (rvalue). Throws RuntimeError if is_ok(). */ | ||
| TVM_FFI_INLINE Error error() && { | ||
| if (!is_err()) TVM_FFI_THROW(RuntimeError) << "Bad expected access: contains value, not error"; | ||
| return std::move(data_).template cast<Error>(); | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Get the success value or a default value. | ||
| * \param default_value The value to return if Expected contains an error. | ||
| * \return The success value if present, otherwise the default value. | ||
| */ | ||
| template <typename U = std::remove_cv_t<T>> | ||
| TVM_FFI_INLINE T value_or(U&& default_value) const { | ||
| if (is_ok()) { | ||
| return data_.cast<T>(); | ||
| } | ||
| return T(std::forward<U>(default_value)); | ||
| } | ||
|
|
||
| private: | ||
| friend struct TypeTraits<Expected<T>>; | ||
|
|
||
| /*! | ||
| * \brief Private constructor from Any. | ||
| * \param data The data containing either T or Error. | ||
| * \note This constructor is used by TypeTraits for conversion. | ||
| */ | ||
| explicit Expected(Any data) : data_(std::move(data)) { | ||
| TVM_FFI_ICHECK(data_.as<T>().has_value() || data_.as<Error>().has_value()) | ||
| << "Expected must contain either T or Error"; | ||
| } | ||
|
|
||
| Any data_; // Holds either T or Error | ||
| }; | ||
|
|
||
| /*! | ||
| * \brief Helper function to create Expected::Ok with type deduction. | ||
| * \tparam T The success type (deduced from argument). | ||
| * \param value The success value. | ||
| * \return Expected<T> containing the success value. | ||
| */ | ||
| template <typename T> | ||
| TVM_FFI_INLINE Expected<T> ExpectedOk(T value) { | ||
| return Expected<T>::Ok(std::move(value)); | ||
| } | ||
|
|
||
| /*! | ||
| * \brief Helper function to create Expected::Err. | ||
| * \param error The error value. | ||
| * \return Expected<Any> containing the error. | ||
| * \note Returns Expected<Any> to allow usage in contexts where T is inferred. | ||
| */ | ||
| template <typename T = Any> | ||
| TVM_FFI_INLINE Expected<T> ExpectedErr(Error error) { | ||
| return Expected<T>::Err(std::move(error)); | ||
| } | ||
|
|
||
| // TypeTraits specialization for Expected<T> | ||
| template <typename T> | ||
| inline constexpr bool use_default_type_traits_v<Expected<T>> = false; | ||
|
|
||
| template <typename T> | ||
| struct TypeTraits<Expected<T>> : public TypeTraitsBase { | ||
| TVM_FFI_INLINE static void CopyToAnyView(const Expected<T>& src, TVMFFIAny* result) { | ||
| const TVMFFIAny* src_any = reinterpret_cast<const TVMFFIAny*>(&src.data_); | ||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| TypeTraits<T>::MoveToAny(TypeTraits<T>::CopyFromAnyViewAfterCheck(src_any), result); | ||
| } else { | ||
| TypeTraits<Error>::MoveToAny(TypeTraits<Error>::CopyFromAnyViewAfterCheck(src_any), result); | ||
| } | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static void MoveToAny(Expected<T> src, TVMFFIAny* result) { | ||
| TVMFFIAny* src_any = reinterpret_cast<TVMFFIAny*>(&src.data_); | ||
| if (TypeTraits<T>::CheckAnyStrict(src_any)) { | ||
| TypeTraits<T>::MoveToAny(TypeTraits<T>::MoveFromAnyAfterCheck(src_any), result); | ||
| } else { | ||
| TypeTraits<Error>::MoveToAny(TypeTraits<Error>::MoveFromAnyAfterCheck(src_any), result); | ||
| } | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) { | ||
| return TypeTraits<T>::CheckAnyStrict(src) || TypeTraits<Error>::CheckAnyStrict(src); | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static Expected<T> CopyFromAnyViewAfterCheck(const TVMFFIAny* src) { | ||
| if (TypeTraits<T>::CheckAnyStrict(src)) { | ||
| return Expected<T>::Ok(TypeTraits<T>::CopyFromAnyViewAfterCheck(src)); | ||
| } | ||
| return Expected<T>::Err(TypeTraits<Error>::CopyFromAnyViewAfterCheck(src)); | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static Expected<T> MoveFromAnyAfterCheck(TVMFFIAny* src) { | ||
| if (TypeTraits<T>::CheckAnyStrict(src)) { | ||
| return Expected<T>::Ok(TypeTraits<T>::MoveFromAnyAfterCheck(src)); | ||
| } | ||
| return Expected<T>::Err(TypeTraits<Error>::MoveFromAnyAfterCheck(src)); | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static std::optional<Expected<T>> TryCastFromAnyView(const TVMFFIAny* src) { | ||
| if (auto opt = TypeTraits<T>::TryCastFromAnyView(src)) { | ||
| return Expected<T>::Ok(*std::move(opt)); | ||
| } | ||
| if (auto opt_err = TypeTraits<Error>::TryCastFromAnyView(src)) { | ||
| return Expected<T>::Err(*std::move(opt_err)); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) { | ||
| return TypeTraitsBase::GetMismatchTypeInfo(src); | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static std::string TypeStr() { | ||
| return "Expected<" + TypeTraits<T>::TypeStr() + ">"; | ||
| } | ||
|
|
||
| TVM_FFI_INLINE static std::string TypeSchema() { | ||
| return R"({"type":"Expected","args":[)" + details::TypeSchema<T>::v() + "]}"; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace ffi | ||
| } // namespace tvm | ||
| #endif // TVM_FFI_EXPECTED_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For large data, we can use move instead of copy, so I agree with Gemini that we can add an overload function here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree as well. I've added both const& and && qualified overloads for value():