Skip to content

Commit 806fe56

Browse files
committed
refactor: apply review comments
1 parent ad48e1b commit 806fe56

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

include/tvm/ffi/expected.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ TVM_FFI_INLINE Expected<T> ExpectedOk(T value) {
160160

161161
/*!
162162
* \brief Helper function to create Expected::Err.
163+
* \tparam T The success type (must be explicitly specified).
163164
* \param error The error value.
164-
* \return Expected<Any> containing the error.
165-
* \note Returns Expected<Any> to allow usage in contexts where T is inferred.
165+
* \return Expected<T> containing the error.
166166
*/
167-
template <typename T = Any>
167+
template <typename T>
168168
TVM_FFI_INLINE Expected<T> ExpectedErr(Error error) {
169169
return Expected<T>::Err(std::move(error));
170170
}
@@ -176,20 +176,18 @@ inline constexpr bool use_default_type_traits_v<Expected<T>> = false;
176176
template <typename T>
177177
struct TypeTraits<Expected<T>> : public TypeTraitsBase {
178178
TVM_FFI_INLINE static void CopyToAnyView(const Expected<T>& src, TVMFFIAny* result) {
179-
const TVMFFIAny* src_any = reinterpret_cast<const TVMFFIAny*>(&src.data_);
180-
if (TypeTraits<T>::CheckAnyStrict(src_any)) {
181-
TypeTraits<T>::MoveToAny(TypeTraits<T>::CopyFromAnyViewAfterCheck(src_any), result);
179+
if (src.is_err()) {
180+
TypeTraits<Error>::CopyToAnyView(src.error(), result);
182181
} else {
183-
TypeTraits<Error>::MoveToAny(TypeTraits<Error>::CopyFromAnyViewAfterCheck(src_any), result);
182+
TypeTraits<T>::CopyToAnyView(src.value(), result);
184183
}
185184
}
186185

187186
TVM_FFI_INLINE static void MoveToAny(Expected<T> src, TVMFFIAny* result) {
188-
TVMFFIAny* src_any = reinterpret_cast<TVMFFIAny*>(&src.data_);
189-
if (TypeTraits<T>::CheckAnyStrict(src_any)) {
190-
TypeTraits<T>::MoveToAny(TypeTraits<T>::MoveFromAnyAfterCheck(src_any), result);
187+
if (src.is_err()) {
188+
TypeTraits<Error>::MoveToAny(std::move(src).error(), result);
191189
} else {
192-
TypeTraits<Error>::MoveToAny(TypeTraits<Error>::MoveFromAnyAfterCheck(src_any), result);
190+
TypeTraits<T>::MoveToAny(std::move(src).value(), result);
193191
}
194192
}
195193

@@ -221,16 +219,13 @@ struct TypeTraits<Expected<T>> : public TypeTraitsBase {
221219
return std::nullopt;
222220
}
223221

224-
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
225-
return TypeTraitsBase::GetMismatchTypeInfo(src);
226-
}
227-
228222
TVM_FFI_INLINE static std::string TypeStr() {
229223
return "Expected<" + TypeTraits<T>::TypeStr() + ">";
230224
}
231225

232226
TVM_FFI_INLINE static std::string TypeSchema() {
233-
return R"({"type":"Expected","args":[)" + details::TypeSchema<T>::v() + "]}";
227+
return R"({"type":"Expected","args":[)" + details::TypeSchema<T>::v() +
228+
R"(,{"type":"ffi.Error"}]})";
234229
}
235230
};
236231

0 commit comments

Comments
 (0)