From b4d696073c48172823c43cdd49100705057c65bc Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Mon, 15 Jan 2024 19:02:34 +0800 Subject: [PATCH 1/6] [struct_pack][breakchange] use struct_pack::error_code instead of struct_pack::errc as return value --- include/ylt/coro_rpc/impl/context.hpp | 2 +- include/ylt/coro_rpc/impl/coro_rpc_client.hpp | 10 +- include/ylt/coro_rpc/impl/errno.h | 48 ++--- .../impl/protocol/struct_pack_protocol.hpp | 4 +- include/ylt/struct_pack.hpp | 155 ++++++------- include/ylt/struct_pack/derived_helper.hpp | 8 +- include/ylt/struct_pack/error_code.hpp | 57 +++-- include/ylt/struct_pack/reflection.hpp | 4 +- include/ylt/struct_pack/unpacker.hpp | 115 +++++----- include/ylt/struct_pack/user_helper.hpp | 8 +- include/ylt/struct_pack/varint.hpp | 6 +- src/coro_rpc/tests/ServerTester.hpp | 7 +- src/coro_rpc/tests/test_coro_rpc_client.cpp | 23 +- src/coro_rpc/tests/test_coro_rpc_server.cpp | 21 +- src/coro_rpc/tests/test_router.cpp | 12 +- src/struct_pack/examples/basic_usage.cpp | 4 +- .../examples/user_defined_serialization.cpp | 8 +- src/struct_pack/tests/test_alignas.cpp | 10 +- src/struct_pack/tests/test_compatible.cpp | 4 +- .../tests/test_compile_time_calculate.cpp | 4 +- src/struct_pack/tests/test_data_struct.cpp | 26 +-- src/struct_pack/tests/test_data_struct2.cpp | 48 ++--- src/struct_pack/tests/test_derived.cpp | 4 +- .../tests/test_disable_meta_info.cpp | 12 +- src/struct_pack/tests/test_pragma_pack.cpp | 8 +- .../test_pragma_pack_and_alignas_mix.cpp | 6 +- src/struct_pack/tests/test_serialize.cpp | 56 ++--- src/struct_pack/tests/test_stream.cpp | 10 +- .../tests/test_user_defined_type.cpp | 32 ++- .../docs/en/struct_pack/struct_pack_intro.md | 14 +- website/docs/zh/coro_rpc/coro_rpc_doc.hpp | 8 +- .../docs/zh/struct_pack/struct_pack_doc.hpp | 204 ++++++++++++------ .../docs/zh/struct_pack/struct_pack_intro.md | 14 +- 33 files changed, 517 insertions(+), 435 deletions(-) diff --git a/include/ylt/coro_rpc/impl/context.hpp b/include/ylt/coro_rpc/impl/context.hpp index c61e5bd79..1d1b046ae 100644 --- a/include/ylt/coro_rpc/impl/context.hpp +++ b/include/ylt/coro_rpc/impl/context.hpp @@ -83,7 +83,7 @@ class context_base { error_code, error_msg, self_->req_head_, self_->is_delay_); } void response_error(coro_rpc::err_code error_code) { - response_error(error_code, make_error_message(error_code)); + response_error(error_code, error_code.message()); } /*! * Send response message diff --git a/include/ylt/coro_rpc/impl/coro_rpc_client.hpp b/include/ylt/coro_rpc/impl/coro_rpc_client.hpp index 0cace0822..9038fc391 100644 --- a/include/ylt/coro_rpc/impl/coro_rpc_client.hpp +++ b/include/ylt/coro_rpc/impl/coro_rpc_client.hpp @@ -744,12 +744,12 @@ class coro_rpc_client { uint8_t rpc_errc, bool &error_happen) { rpc_return_type_t ret; - struct_pack::errc ec; + struct_pack::err_code ec; coro_rpc_protocol::rpc_error err; if (rpc_errc == 0) AS_LIKELY { ec = struct_pack::deserialize_to(ret, buffer); - if (ec == struct_pack::errc::ok) { + if SP_LIKELY (!ec) { if constexpr (std::is_same_v) { return {}; } @@ -759,17 +759,17 @@ class coro_rpc_client { } } else { - err.code = rpc_errc; + err.val() = rpc_errc; if (rpc_errc != UINT8_MAX) { ec = struct_pack::deserialize_to(err.msg, buffer); - if (ec == struct_pack::errc::ok) { + if SP_LIKELY (!ec) { error_happen = true; return rpc_result{unexpect_t{}, std::move(err)}; } } else { ec = struct_pack::deserialize_to(err, buffer); - if (ec == struct_pack::errc::ok) { + if SP_LIKELY (!ec) { return rpc_result{unexpect_t{}, std::move(err)}; } } diff --git a/include/ylt/coro_rpc/impl/errno.h b/include/ylt/coro_rpc/impl/errno.h index 851e7f5bc..dd033a47e 100644 --- a/include/ylt/coro_rpc/impl/errno.h +++ b/include/ylt/coro_rpc/impl/errno.h @@ -33,30 +33,7 @@ enum class errc : uint16_t { message_too_large, server_has_ran, }; -struct err_code { - public: - errc ec; - err_code() : ec(errc::ok) {} - explicit err_code(uint16_t ec) : ec{ec} {}; - err_code(errc ec) : ec(ec){}; - err_code& operator=(errc ec) { - this->ec = ec; - return *this; - } - err_code& operator=(uint16_t ec) { - this->ec = errc{ec}; - return *this; - } - err_code(const err_code& err_code) = default; - err_code& operator=(const err_code& o) = default; - bool operator!() const { return ec == errc::ok; } - operator errc() const { return ec; } - operator bool() const { return static_cast(ec); } - explicit operator uint16_t() const { return static_cast(ec); } - uint16_t val() const { return static_cast(ec); } -}; -inline bool operator!(errc ec) { return ec == errc::ok; } -inline std::string_view make_error_message(errc ec) { +inline std::string_view make_error_message(errc ec) noexcept { switch (ec) { case errc::ok: return "ok"; @@ -86,4 +63,27 @@ inline std::string_view make_error_message(errc ec) { return "unknown_user-defined_error"; } } +struct err_code { + public: + errc ec; + err_code() noexcept : ec(errc::ok) {} + explicit err_code(uint16_t ec) noexcept : ec{ec} {}; + err_code(errc ec) noexcept : ec(ec){}; + err_code& operator=(errc ec) noexcept { + this->ec = ec; + return *this; + } + err_code(const err_code& err_code) noexcept = default; + err_code& operator=(const err_code& o) noexcept = default; + operator errc() const noexcept { return ec; } + operator bool() const noexcept { return static_cast(ec); } + explicit operator uint16_t() const noexcept { + return static_cast(ec); + } + uint16_t val() const noexcept { return static_cast(ec); } + std::string_view message() const noexcept { return make_error_message(ec); } +}; + +inline bool operator!(errc ec) noexcept { return ec == errc::ok; } + }; // namespace coro_rpc \ No newline at end of file diff --git a/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp b/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp index e90725272..c7618323e 100644 --- a/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp +++ b/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp @@ -20,7 +20,7 @@ namespace coro_rpc::protocol { struct struct_pack_protocol { template static bool deserialize_to(T& t, std::string_view buffer) { - struct_pack::errc ok{}; + struct_pack::err_code ok{}; if constexpr (std::tuple_size_v == 1) { ok = struct_pack::deserialize_to(std::get<0>(t), buffer); } @@ -28,7 +28,7 @@ struct struct_pack_protocol { ok = struct_pack::deserialize_to(t, buffer); } - return ok == struct_pack::errc::ok; + return !ok; } template static std::string serialize(const T& t) { diff --git a/include/ylt/struct_pack.hpp b/include/ylt/struct_pack.hpp index 5afa10274..9e3d229fc 100644 --- a/include/ylt/struct_pack.hpp +++ b/include/ylt/struct_pack.hpp @@ -88,8 +88,8 @@ inline std::error_code make_error_code(struct_pack::errc err) { * @param err error code. * @return error message. */ -inline std::string error_message(struct_pack::errc err) { - return struct_pack::make_error_code(err).message(); +inline std::string_view error_message(struct_pack::errc err) noexcept { + return struct_pack::detail::make_error_message(err); } template @@ -278,8 +278,8 @@ template < typename View, typename = std::enable_if_t>> #endif -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const View &v, - Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const View &v, + Args &...args) { detail::memory_reader reader{(const char *)v.data(), (const char *)v.data() + v.size()}; detail::unpacker in(reader); @@ -287,8 +287,8 @@ template < } template -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const char *data, - size_t size, Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const char *data, + size_t size, Args &...args) { detail::memory_reader reader{data, data + size}; detail::unpacker in(reader); return in.deserialize(t, args...); @@ -302,8 +302,8 @@ template >> #endif -[[nodiscard]] struct_pack::errc deserialize_to(T &t, Reader &reader, - Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, Reader &reader, + Args &...args) { detail::unpacker in(reader); std::size_t consume_len; auto old_pos = reader.tellg(); @@ -330,14 +330,14 @@ template < typename View, typename = std::enable_if_t>> #endif -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const View &v, - size_t &consume_len, - Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const View &v, + size_t &consume_len, + Args &...args) { detail::memory_reader reader{(const char *)v.data(), (const char *)v.data() + v.size()}; detail::unpacker in(reader); auto ret = in.deserialize_with_len(consume_len, t, args...); - if SP_LIKELY (ret == errc{}) { + if SP_LIKELY (!ret) { consume_len = (std::max)((size_t)(reader.now - v.data()), consume_len); } else { @@ -347,13 +347,14 @@ template < } template -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const char *data, - size_t size, size_t &consume_len, - Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const char *data, + size_t size, + size_t &consume_len, + Args &...args) { detail::memory_reader reader{data, data + size}; detail::unpacker in(reader); auto ret = in.deserialize_with_len(consume_len, t, args...); - if SP_LIKELY (ret == errc{}) { + if SP_LIKELY (!ret) { consume_len = (std::max)((size_t)(reader.now - data), consume_len); } else { @@ -369,9 +370,10 @@ template #endif -[[nodiscard]] struct_pack::errc deserialize_to_with_offset(T &t, const View &v, - size_t &offset, - Args &...args) { +[[nodiscard]] struct_pack::err_code deserialize_to_with_offset(T &t, + const View &v, + size_t &offset, + Args &...args) { size_t sz; auto ret = deserialize_to(t, v.data() + offset, v.size() - offset, sz, args...); @@ -380,7 +382,7 @@ template -[[nodiscard]] struct_pack::errc deserialize_to_with_offset( +[[nodiscard]] struct_pack::err_code deserialize_to_with_offset( T &t, const char *data, size_t size, size_t &offset, Args &...args) { size_t sz; auto ret = deserialize_to(t, data + offset, size - offset, sz, args...); @@ -396,20 +398,19 @@ template < typename = std::enable_if_t>> #endif [[nodiscard]] auto deserialize(const View &v) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } template [[nodiscard]] auto deserialize(const char *data, size_t size) { - expected, struct_pack::errc> ret; - if (auto errc = deserialize_to(ret.value(), data, size); - errc != struct_pack::errc{}) { - ret = unexpected{errc}; + expected, struct_pack::err_code> ret; + if (auto errc = deserialize_to(ret.value(), data, size); errc) { + ret = unexpected{errc}; } return ret; } @@ -420,10 +421,10 @@ template >> #endif [[nodiscard]] auto deserialize(Reader &v) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -434,10 +435,10 @@ template template #endif [[nodiscard]] auto deserialize(const View &v, size_t &consume_len) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v, consume_len); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -445,10 +446,10 @@ template template [[nodiscard]] auto deserialize(const char *data, size_t size, size_t &consume_len) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), data, size, consume_len); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -462,20 +463,19 @@ template < typename = std::enable_if_t>> #endif [[nodiscard]] auto deserialize(const View &v) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } template [[nodiscard]] auto deserialize(const char *data, size_t size) { - expected, struct_pack::errc> ret; - if (auto errc = deserialize_to(ret.value(), data, size); - errc != struct_pack::errc{}) { - ret = unexpected{errc}; + expected, struct_pack::err_code> ret; + if (auto errc = deserialize_to(ret.value(), data, size); errc) { + ret = unexpected{errc}; } return ret; } @@ -487,10 +487,10 @@ template >> #endif [[nodiscard]] auto deserialize(Reader &v) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -502,10 +502,10 @@ template #endif [[nodiscard]] auto deserialize(const View &v, size_t &consume_len) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), v, consume_len); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -513,10 +513,10 @@ template template [[nodiscard]] auto deserialize(const char *data, size_t size, size_t &consume_len) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to(ret.value(), data, size, consume_len); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -527,10 +527,10 @@ template template #endif [[nodiscard]] auto deserialize_with_offset(const View &v, size_t &offset) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to_with_offset(ret.value(), v, offset); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -538,10 +538,10 @@ template template [[nodiscard]] auto deserialize_with_offset(const char *data, size_t size, size_t &offset) { - expected, struct_pack::errc> ret; + expected, struct_pack::err_code> ret; auto errc = deserialize_to_with_offset(ret.value(), data, size, offset); - if SP_UNLIKELY (errc != struct_pack::errc{}) { - ret = unexpected{errc}; + if SP_UNLIKELY (errc) { + ret = unexpected{errc}; } return ret; } @@ -555,7 +555,7 @@ template < typename View, typename = std::enable_if_t>> #endif -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, const View &v) { +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const View &v) { using T_Field = std::tuple_element_t())>; static_assert(std::is_same_v, "The dst's type is not correct. It should be as same as the " @@ -568,8 +568,8 @@ template < template -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, const char *data, - size_t size) { +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const char *data, + size_t size) { using T_Field = std::tuple_element_t())>; static_assert(std::is_same_v, "The dst's type is not correct. It should be as same as the " @@ -587,7 +587,7 @@ template >> #endif -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, Reader &reader) { +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, Reader &reader) { using T_Field = std::tuple_element_t())>; static_assert(std::is_same_v, "The dst's type is not correct. It should be as same as the " @@ -606,10 +606,10 @@ template < #endif [[nodiscard]] auto get_field(const View &v) { using T_Field = std::tuple_element_t())>; - expected ret; + expected ret; auto ec = get_field_to(ret.value(), v); - if SP_UNLIKELY (ec != struct_pack::errc{}) { - ret = unexpected{ec}; + if SP_UNLIKELY (ec) { + ret = unexpected{ec}; } return ret; } @@ -617,10 +617,10 @@ template < template [[nodiscard]] auto get_field(const char *data, size_t size) { using T_Field = std::tuple_element_t())>; - expected ret; + expected ret; auto ec = get_field_to(ret.value(), data, size); - if SP_UNLIKELY (ec != struct_pack::errc{}) { - ret = unexpected{ec}; + if SP_UNLIKELY (ec) { + ret = unexpected{ec}; } return ret; } @@ -634,10 +634,10 @@ template ())>; - expected ret; + expected ret; auto ec = get_field_to(ret.value(), reader); - if SP_UNLIKELY (ec != struct_pack::errc{}) { - ret = unexpected{ec}; + if SP_UNLIKELY (ec) { + ret = unexpected{ec}; } return ret; } @@ -649,7 +649,7 @@ template >> #endif [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(Reader &reader) { static_assert(sizeof...(DerivedClasses) > 0, "There must have a least one derived class"); @@ -666,12 +666,13 @@ deserialize_derived_class(Reader &reader) { "constexpr uint64_t struct_pack_id` for collision type. "); } else { - struct_pack::expected, struct_pack::errc> ret; + struct_pack::expected, struct_pack::err_code> + ret; auto ec = struct_pack::detail::deserialize_derived_class( ret.value(), reader); - if SP_UNLIKELY (ec != struct_pack::errc{}) { - ret = unexpected{ec}; + if SP_UNLIKELY (ec) { + ret = unexpected{ec}; } return ret; } @@ -685,7 +686,7 @@ template < typename = std::enable_if_t>> #endif [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(const View &v) { detail::memory_reader reader{v.data(), v.data() + v.size()}; if constexpr (std::is_abstract_v) { @@ -698,7 +699,7 @@ deserialize_derived_class(const View &v) { } template [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(const char *data, size_t size) { detail::memory_reader reader{data, data + size}; if constexpr (std::is_abstract_v) { diff --git a/include/ylt/struct_pack/derived_helper.hpp b/include/ylt/struct_pack/derived_helper.hpp index def1b1541..22e1ea04f 100644 --- a/include/ylt/struct_pack/derived_helper.hpp +++ b/include/ylt/struct_pack/derived_helper.hpp @@ -98,11 +98,11 @@ struct public_base_class_checker { template struct deserialize_derived_class_helper { template - static STRUCT_PACK_INLINE constexpr struct_pack::errc run( + static STRUCT_PACK_INLINE constexpr struct_pack::err_code run( std::unique_ptr &base, unpack &unpacker) { if constexpr (index >= std::tuple_size_v) { unreachable(); - return struct_pack::errc{}; + return struct_pack::err_code{}; } else { using derived_class = std::tuple_element_t; @@ -154,8 +154,8 @@ template struct deserialize_one_derived_class_helper { template - static STRUCT_PACK_INLINE constexpr struct_pack::errc run(unpacker *self, - Pointer &base) { + static STRUCT_PACK_INLINE constexpr struct_pack::err_code run(unpacker *self, + Pointer &base) { if constexpr (index >= std::tuple_size_v) { unreachable(); } diff --git a/include/ylt/struct_pack/error_code.hpp b/include/ylt/struct_pack/error_code.hpp index 88f4ec5de..a75191333 100644 --- a/include/ylt/struct_pack/error_code.hpp +++ b/include/ylt/struct_pack/error_code.hpp @@ -25,33 +25,58 @@ enum class errc { hash_conflict, invalid_width_of_container_length, }; +namespace detail { +inline std::string_view make_error_message(errc ec) noexcept { + switch (ec) { + case errc::ok: + return "ok"; + case errc::no_buffer_space: + return "no buffer space"; + case errc::invalid_buffer: + return "invalid argument"; + case errc::hash_conflict: + return "hash conflict"; + case errc::invalid_width_of_container_length: + return "invalid width of container length"; + default: + return "(unrecognized error)"; + } +} +} // namespace detail + +struct err_code { + public: + errc ec; + err_code() noexcept : ec(errc::ok) {} + err_code(errc ec) noexcept : ec(ec){}; + err_code& operator=(errc ec) noexcept { + this->ec = ec; + return *this; + } + err_code(const err_code& err_code) noexcept = default; + err_code& operator=(const err_code& o) noexcept = default; + bool operator==(const err_code& o) const noexcept { return ec == o.ec; } + bool operator!=(const err_code& o) const noexcept { return ec != o.ec; } + operator bool() const noexcept { return ec != errc::ok; } + int val() const noexcept { return static_cast(ec); } + std::string_view message() const noexcept { + return detail::make_error_message(ec); + } +}; namespace detail { class struct_pack_category : public std::error_category { public: - virtual const char *name() const noexcept override { + virtual const char* name() const noexcept override { return "struct_pack::category"; } virtual std::string message(int err_val) const override { - switch (static_cast(err_val)) { - case errc::ok: - return "ok"; - case errc::no_buffer_space: - return "no buffer space"; - case errc::invalid_buffer: - return "invalid argument"; - case errc::hash_conflict: - return "hash conflict"; - case errc::invalid_width_of_container_length: - return "invalid width of container length"; - default: - return "(unrecognized error)"; - } + return std::string{make_error_message(static_cast(err_val))}; } }; -inline const std::error_category &category() { +inline const std::error_category& category() { static struct_pack::detail::struct_pack_category instance; return instance; } diff --git a/include/ylt/struct_pack/reflection.hpp b/include/ylt/struct_pack/reflection.hpp index 482e0135e..e20346b6b 100644 --- a/include/ylt/struct_pack/reflection.hpp +++ b/include/ylt/struct_pack/reflection.hpp @@ -608,7 +608,7 @@ struct memory_reader; template concept user_defined_serialization = requires (Type& t) { sp_serialize_to(std::declval(),(const Type&)t); - {sp_deserialize_to(std::declval(),t)} -> std::same_as; + {sp_deserialize_to(std::declval(),t)} -> std::same_as; {sp_get_needed_size((const Type&)t)}->std::same_as; }; template @@ -623,7 +623,7 @@ struct memory_reader; template struct user_defined_serialization_impl(),std::declval())), - std::enable_if(),std::declval())), struct_pack::errc>, + std::enable_if(),std::declval())), struct_pack::err_code>, std::enable_if())), std::string_view>>>>> : std::true_type {}; diff --git a/include/ylt/struct_pack/unpacker.hpp b/include/ylt/struct_pack/unpacker.hpp index a724b13ca..3efe7ca37 100644 --- a/include/ylt/struct_pack/unpacker.hpp +++ b/include/ylt/struct_pack/unpacker.hpp @@ -95,10 +95,11 @@ class unpacker { } template - friend STRUCT_PACK_INLINE struct_pack::errc read(Reader &reader, T &t); + friend STRUCT_PACK_INLINE struct_pack::err_code read(Reader &reader, T &t); template - STRUCT_PACK_MAY_INLINE struct_pack::errc deserialize(T &t, Args &...args) { + STRUCT_PACK_MAY_INLINE struct_pack::err_code deserialize(T &t, + Args &...args) { using Type = get_args_type; constexpr bool has_compatible = check_if_compatible_element_exist())>(); @@ -106,7 +107,7 @@ class unpacker { data_len_ = reader_.tellg(); } auto &&[err_code, buffer_len] = deserialize_metainfo(); - if SP_UNLIKELY (err_code != struct_pack::errc{}) { + if SP_UNLIKELY (err_code != struct_pack::err_code{}) { return err_code; } if constexpr (has_compatible) { @@ -146,7 +147,7 @@ class unpacker { unreachable(); } if constexpr (has_compatible) { - if SP_UNLIKELY (err_code != errc::ok) { + if SP_UNLIKELY (err_code) { return err_code; } constexpr std::size_t sz = compatible_version_number.size(); @@ -157,7 +158,7 @@ class unpacker { } template - STRUCT_PACK_MAY_INLINE struct_pack::errc deserialize_with_len( + STRUCT_PACK_MAY_INLINE struct_pack::err_code deserialize_with_len( std::size_t &len, T &t, Args &...args) { using Type = get_args_type; constexpr bool has_compatible = @@ -167,7 +168,7 @@ class unpacker { } auto &&[err_code, buffer_len] = deserialize_metainfo(); len = buffer_len; - if SP_UNLIKELY (err_code != struct_pack::errc{}) { + if SP_UNLIKELY (err_code != struct_pack::err_code{}) { return err_code; } if constexpr (has_compatible) { @@ -206,7 +207,7 @@ class unpacker { unreachable(); } if constexpr (has_compatible) { - if SP_UNLIKELY (err_code != errc::ok) { + if SP_UNLIKELY (err_code) { return err_code; } constexpr std::size_t sz = compatible_version_number.size(); @@ -217,7 +218,7 @@ class unpacker { } template - STRUCT_PACK_MAY_INLINE struct_pack::errc get_field( + STRUCT_PACK_MAY_INLINE struct_pack::err_code get_field( std::tuple_element_t())> &field) { using T = remove_cvref_t; using Type = get_args_type; @@ -229,7 +230,7 @@ class unpacker { } auto &&[err_code, buffer_len] = deserialize_metainfo(); - if SP_UNLIKELY (err_code != struct_pack::errc{}) { + if SP_UNLIKELY (err_code != struct_pack::err_code{}) { return err_code; } if constexpr (has_compatible) { @@ -268,7 +269,7 @@ class unpacker { unreachable(); } if constexpr (has_compatible) { - if SP_UNLIKELY (err_code != errc::ok) { + if SP_UNLIKELY (err_code) { return err_code; } constexpr std::size_t sz = compatible_version_number.size(); @@ -279,10 +280,10 @@ class unpacker { } template - STRUCT_PACK_INLINE struct_pack::errc deserialize_compatibles( + STRUCT_PACK_INLINE struct_pack::err_code deserialize_compatibles( T &t, std::index_sequence, Args &...args) { using Type = get_args_type; - struct_pack::errc err_code; + struct_pack::err_code err_code; switch (size_type_) { case 0: ([&] { @@ -355,12 +356,12 @@ class unpacker { } template - STRUCT_PACK_INLINE struct_pack::errc deserialize_compatible_fields( + STRUCT_PACK_INLINE struct_pack::err_code deserialize_compatible_fields( std::tuple_element_t())> &field, std::index_sequence) { using T = remove_cvref_t; using Type = get_args_type; - struct_pack::errc err_code; + struct_pack::err_code err_code; switch (size_type_) { case 0: ([&] { @@ -432,12 +433,12 @@ class unpacker { } template - STRUCT_PACK_INLINE struct_pack::errc get_field_impl( + STRUCT_PACK_INLINE struct_pack::err_code get_field_impl( std::tuple_element_t())> &field) { using T = remove_cvref_t; T t; - struct_pack::errc err_code; + struct_pack::err_code err_code; if constexpr (tuple) { err_code = std::apply( [&](auto &&...items) CONSTEXPR_INLINE_LAMBDA { @@ -479,7 +480,7 @@ class unpacker { } }; - STRUCT_PACK_INLINE std::pair + STRUCT_PACK_INLINE std::pair deserialize_compatible(unsigned compatible_sz_len) { constexpr std::size_t sz[] = {0, 2, 4, 8}; auto len_sz = sz[compatible_sz_len]; @@ -513,7 +514,7 @@ class unpacker { } template - STRUCT_PACK_INLINE struct_pack::errc deserialize_type_literal() { + STRUCT_PACK_INLINE struct_pack::err_code deserialize_type_literal() { constexpr auto literal = struct_pack::get_type_literal(); if constexpr (view_reader_t) { const char *buffer = reader_.read_view(literal.size() + 1); @@ -538,7 +539,7 @@ class unpacker { } template - STRUCT_PACK_INLINE std::pair + STRUCT_PACK_INLINE std::pair deserialize_metainfo() { uint32_t current_types_code; if constexpr (check_if_disable_hash_head()) { @@ -590,18 +591,18 @@ class unpacker { (char *)&metainfo)) { return {struct_pack::errc::no_buffer_space, 0}; } - std::pair ret; + std::pair ret; auto compatible_sz_len = metainfo & 0b11; if (compatible_sz_len) { ret = deserialize_compatible(compatible_sz_len); - if SP_UNLIKELY (ret.first != errc{}) { + if SP_UNLIKELY (ret.first) { return ret; } } auto has_type_literal = metainfo & 0b100; if (has_type_literal) { auto ec = deserialize_type_literal(); - if SP_UNLIKELY (ec != errc{}) { + if SP_UNLIKELY (ec) { return {ec, 0}; } } @@ -611,16 +612,16 @@ class unpacker { } template - constexpr struct_pack::errc STRUCT_PACK_INLINE deserialize_many() { + constexpr struct_pack::err_code STRUCT_PACK_INLINE deserialize_many() { return {}; } template - constexpr struct_pack::errc STRUCT_PACK_INLINE + constexpr struct_pack::err_code STRUCT_PACK_INLINE deserialize_many(First &&first_item, Args &&...items) { auto code = deserialize_one(first_item); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } if constexpr (sizeof...(items) > 0) { @@ -632,7 +633,7 @@ class unpacker { } } - constexpr struct_pack::errc STRUCT_PACK_INLINE + constexpr struct_pack::err_code STRUCT_PACK_INLINE ignore_padding(std::size_t sz) { if (sz > 0) { return reader_.ignore(sz) ? errc{} : errc::no_buffer_space; @@ -644,8 +645,9 @@ class unpacker { template - constexpr struct_pack::errc STRUCT_PACK_INLINE deserialize_one_fast_varint( - char (&vec)[bitset_width], unsigned int &i, Arg &item) { + constexpr struct_pack::err_code STRUCT_PACK_INLINE + deserialize_one_fast_varint(char (&vec)[bitset_width], unsigned int &i, + Arg &item) { if constexpr (varint_t) { constexpr auto real_width = (std::min)(width, sizeof(Arg)); auto index = i++; @@ -682,12 +684,13 @@ class unpacker { } template - constexpr struct_pack::errc STRUCT_PACK_INLINE deserialize_fast_varint_helper( - char (&vec)[bitset_width], unsigned int &i, Arg &item, Args &...items) { + constexpr struct_pack::err_code STRUCT_PACK_INLINE + deserialize_fast_varint_helper(char (&vec)[bitset_width], unsigned int &i, + Arg &item, Args &...items) { auto ec = deserialize_one_fast_varint(vec, i, item); if constexpr (sizeof...(items) > 0) { - if SP_UNLIKELY (ec != errc{}) { + if SP_UNLIKELY (ec) { return ec; } else { @@ -701,7 +704,7 @@ class unpacker { } template - constexpr struct_pack::errc STRUCT_PACK_INLINE + constexpr struct_pack::err_code STRUCT_PACK_INLINE deserialize_fast_varint(Args &...items) { constexpr auto cnt = calculate_fast_varint_count(); constexpr auto bitset_size = ((cnt + 2) + 7) / 8; @@ -717,7 +720,7 @@ class unpacker { int width = !!(vec[cnt / 8] & (0b1 << (cnt % 8))) + !!(vec[(cnt + 1) / 8] & (0b1 << ((cnt + 1) % 8))) * 2; unsigned int i = 0; - struct_pack::errc ec{}; + struct_pack::err_code ec{}; switch (width) { case 0: ec = deserialize_fast_varint_helper(vec, i, @@ -749,8 +752,8 @@ class unpacker { template - constexpr struct_pack::errc inline deserialize_one(T &item) { - struct_pack::errc code{}; + constexpr struct_pack::err_code inline deserialize_one(T &item) { + struct_pack::err_code code{}; using type = remove_cvref_t; static_assert(!std::is_pointer_v); constexpr auto id = get_type_id(); @@ -864,7 +867,7 @@ class unpacker { else { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -967,7 +970,7 @@ class unpacker { item.clear(); for (uint64_t i = 0; i < size; ++i) { code = deserialize_one(value); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } if constexpr (NotSkip) { @@ -993,7 +996,7 @@ class unpacker { item.clear(); for (uint64_t i = 0; i < size; ++i) { code = deserialize_one(value); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } if constexpr (NotSkip) { @@ -1040,7 +1043,7 @@ class unpacker { else { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1069,7 +1072,7 @@ class unpacker { for (size_t j = i; j < i + len; ++j) { code = deserialize_one( item[j]); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1097,7 +1100,7 @@ class unpacker { item.emplace_back(); code = deserialize_one(item.back()); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { if constexpr (can_reserve) { if constexpr (can_shrink_to_fit) { item.shrink_to_fit(); // release reserve memory @@ -1111,7 +1114,7 @@ class unpacker { value_type useless; for (size_t i = 0; i < size; ++i) { code = deserialize_one(useless); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1197,12 +1200,12 @@ class unpacker { is_trivial_serializable::value) { visit_members(item, [&](auto &&...items) CONSTEXPR_INLINE_LAMBDA { int i = 1; - auto f = [&](auto &&item) { + auto f = [&](auto &&item) -> bool { code = deserialize_one(item); - if SP_LIKELY (code == errc::ok) { + if SP_LIKELY (!code) { code = ignore_padding(align::padding_size[i++]); } - return code == errc::ok; + return !code; }; [[maybe_unused]] bool op = (f(items) && ...); }); @@ -1216,7 +1219,7 @@ class unpacker { get_parent_tag(); // to pass msvc with c++17 return deserialize_fast_varint(items...); }); - if SP_UNLIKELY (code != errc::ok) { + if SP_UNLIKELY (code) { return code; } } @@ -1281,7 +1284,7 @@ class unpacker { else if constexpr (id == type_id::array_t) { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1299,7 +1302,7 @@ class unpacker { if constexpr (NotSkip) { for (auto &e : item) { code = deserialize_one(e.second); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1316,7 +1319,7 @@ class unpacker { if constexpr (NotSkip) { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } @@ -1373,7 +1376,7 @@ class unpacker { // partial deserialize_to template - STRUCT_PACK_INLINE constexpr bool set_value(struct_pack::errc &err_code, + STRUCT_PACK_INLINE constexpr bool set_value(struct_pack::err_code &err_code, FieldType &field, T &&t) { if constexpr (FieldIndex == I) { static_assert( @@ -1394,7 +1397,7 @@ class unpacker { template - STRUCT_PACK_INLINE constexpr void for_each_helper(struct_pack::errc &code, + STRUCT_PACK_INLINE constexpr void for_each_helper(struct_pack::err_code &code, FieldType &field, std::index_sequence, Args &&...items) { @@ -1406,9 +1409,9 @@ class unpacker { template - STRUCT_PACK_INLINE constexpr struct_pack::errc for_each(FieldType &field, - Args &&...items) { - struct_pack::errc code{}; + STRUCT_PACK_INLINE constexpr struct_pack::err_code for_each(FieldType &field, + Args &&...items) { + struct_pack::err_code code{}; for_each_helper( code, field, std::make_index_sequence(), items...); return code; @@ -1445,8 +1448,8 @@ struct MD5_reader_wrapper : public Reader { }; template -[[nodiscard]] STRUCT_PACK_INLINE struct_pack::errc deserialize_derived_class( - std::unique_ptr &base, Reader &reader) { +[[nodiscard]] STRUCT_PACK_INLINE struct_pack::err_code +deserialize_derived_class(std::unique_ptr &base, Reader &reader) { MD5_reader_wrapper wrapper{std::move(reader)}; if (wrapper.is_failed) { return struct_pack::errc::no_buffer_space; diff --git a/include/ylt/struct_pack/user_helper.hpp b/include/ylt/struct_pack/user_helper.hpp index 4a44c6b71..5c13790d4 100644 --- a/include/ylt/struct_pack/user_helper.hpp +++ b/include/ylt/struct_pack/user_helper.hpp @@ -29,15 +29,15 @@ STRUCT_PACK_INLINE void write(Writer& writer, const T* t, std::size_t len) { }; template -STRUCT_PACK_INLINE struct_pack::errc read(Reader& reader, T& t) { +STRUCT_PACK_INLINE struct_pack::err_code read(Reader& reader, T& t) { struct_pack::detail::unpacker unpacker{ reader}; return unpacker.template deserialize_one(t); }; template -STRUCT_PACK_INLINE struct_pack::errc read(Reader& reader, T* t, - std::size_t len) { +STRUCT_PACK_INLINE struct_pack::err_code read(Reader& reader, T* t, + std::size_t len) { if constexpr (detail::is_trivial_serializable::value && detail::is_little_endian_copyable) { if constexpr (!ifSkip) { @@ -56,7 +56,7 @@ STRUCT_PACK_INLINE struct_pack::errc read(Reader& reader, T* t, auto code = unpacker.template deserialize_one( t[i]); - if SP_UNLIKELY (code != struct_pack::errc{}) { + if SP_UNLIKELY (code != struct_pack::err_code{}) { return code; } } diff --git a/include/ylt/struct_pack/varint.hpp b/include/ylt/struct_pack/varint.hpp index 9e2037346..2a6346e78 100644 --- a/include/ylt/struct_pack/varint.hpp +++ b/include/ylt/struct_pack/varint.hpp @@ -275,7 +275,7 @@ template #else template #endif -[[nodiscard]] STRUCT_PACK_INLINE struct_pack::errc deserialize_varint_impl( +[[nodiscard]] STRUCT_PACK_INLINE struct_pack::err_code deserialize_varint_impl( Reader& reader, uint64_t& v) { #if __cpp_concepts < 201907L static_assert(reader_t, "The writer type must satisfy requirements!"); @@ -300,7 +300,7 @@ template -[[nodiscard]] STRUCT_PACK_INLINE struct_pack::errc deserialize_varint( +[[nodiscard]] STRUCT_PACK_INLINE struct_pack::err_code deserialize_varint( Reader& reader, T& t) { #if __cpp_concepts < 201907L static_assert(reader_t, "The writer type must satisfy requirements!"); @@ -308,7 +308,7 @@ template ) { t = decode_zigzag(v); } diff --git a/src/coro_rpc/tests/ServerTester.hpp b/src/coro_rpc/tests/ServerTester.hpp index 606be2e35..67f33b6cd 100644 --- a/src/coro_rpc/tests/ServerTester.hpp +++ b/src/coro_rpc/tests/ServerTester.hpp @@ -368,14 +368,13 @@ struct ServerTester : TesterConfig { }; auto client = init_client(); ELOGV(INFO, "run %s, client_id %d", __func__, client->get_client_id()); - coro_rpc::errc ec; + coro_rpc::err_code ec; // ec = syncAwait(client->connect("127.0.0.1", port, 0ms)); // CHECK_MESSAGE(ec == std::errc::timed_out, make_error_code(ec).message()); auto client2 = init_client(); ec = syncAwait(client2->connect("10.255.255.1", port_, 5ms)); - CHECK_MESSAGE( - ec, - std::to_string(client->get_client_id()).append(make_error_message(ec))); + CHECK_MESSAGE(ec, + std::to_string(client->get_client_id()).append(ec.message())); } template diff --git a/src/coro_rpc/tests/test_coro_rpc_client.cpp b/src/coro_rpc/tests/test_coro_rpc_client.cpp index e3da3b7ae..77df32dc3 100644 --- a/src/coro_rpc/tests/test_coro_rpc_client.cpp +++ b/src/coro_rpc/tests/test_coro_rpc_client.cpp @@ -278,8 +278,7 @@ class SSLClientTester { if (client_crt == ssl_type::fake || client_crt == ssl_type::no) { REQUIRE(ok == false); auto ec = syncAwait(client->connect("127.0.0.1", port_)); - REQUIRE_MESSAGE(ec == coro_rpc::errc::not_connected, - make_error_message(ec)); + REQUIRE_MESSAGE(ec == coro_rpc::errc::not_connected, ec.message()); auto ret = syncAwait(client->template call()); REQUIRE_MESSAGE(ret.error().code == coro_rpc::errc::not_connected, ret.error().msg); @@ -292,13 +291,12 @@ class SSLClientTester { if (ec) { ELOGV(INFO, "%s", gen_err().data()); } - REQUIRE_MESSAGE(!ec, make_error_message(ec)); + REQUIRE_MESSAGE(!ec, ec.message()); auto ret = co_await client->template call(); CHECK(ret.has_value()); } else { - REQUIRE_MESSAGE(ec == coro_rpc::errc::not_connected, - make_error_message(ec)); + REQUIRE_MESSAGE(ec == coro_rpc::errc::not_connected, ec.message()); } }; syncAwait(f()); @@ -371,7 +369,7 @@ TEST_CASE("testing client with eof") { REQUIRE_MESSAGE(res, "server start failed"); coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto ec = client.sync_connect("127.0.0.1", "8801"); - REQUIRE_MESSAGE(!ec, make_error_message(ec)); + REQUIRE_MESSAGE(!ec, ec.message()); server.register_handler(); auto ret = client.sync_call(); @@ -393,7 +391,7 @@ TEST_CASE("testing client with attachment") { REQUIRE_MESSAGE(res, "server start failed"); coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto ec = client.sync_connect("127.0.0.1", "8801"); - REQUIRE_MESSAGE(!ec, make_error_message(ec)); + REQUIRE_MESSAGE(!ec, ec.message()); server.register_handler(); @@ -437,7 +435,7 @@ TEST_CASE("testing client with shutdown") { CHECK_MESSAGE(res, "server start timeout"); coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto ec = client.sync_connect("127.0.0.1", "8801"); - REQUIRE_MESSAGE(!ec, make_error_message(ec)); + REQUIRE_MESSAGE(!ec, ec.message()); server.register_handler(); g_action = inject_action::nothing; @@ -469,7 +467,7 @@ TEST_CASE("testing client timeout") { coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto ret = client.connect("10.255.255.1", "8801", 5ms); auto val = syncAwait(ret); - CHECK_MESSAGE(val == coro_rpc::errc::timed_out, make_error_message(val)); + CHECK_MESSAGE(val == coro_rpc::errc::timed_out, val.message()); } // SUBCASE("call, 0ms timeout") { // coro_rpc_server server(2, 8801); @@ -487,13 +485,13 @@ TEST_CASE("testing client timeout") { TEST_CASE("testing client connect err") { coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto val = syncAwait(client.connect("127.0.0.1", "8801")); - CHECK_MESSAGE(val == coro_rpc::errc::not_connected, make_error_message(val)); + CHECK_MESSAGE(val == coro_rpc::errc::not_connected, val.message()); } #ifdef UNIT_TEST_INJECT TEST_CASE("testing client sync connect, unit test inject only") { coro_rpc_client client(*coro_io::get_global_executor(), g_client_id++); auto val = client.sync_connect("127.0.0.1", "8801"); - CHECK_MESSAGE(val == coro_rpc::errc::not_connected, make_error_message(val)); + CHECK_MESSAGE(val == coro_rpc::errc::not_connected, val.message()); #ifdef YLT_ENABLE_SSL SUBCASE("client use ssl but server don't use ssl") { g_action = {}; @@ -504,8 +502,7 @@ TEST_CASE("testing client sync connect, unit test inject only") { bool ok = client2.init_ssl("../openssl_files", "server.crt"); CHECK(ok == true); val = client2.sync_connect("127.0.0.1", "8801"); - CHECK_MESSAGE(val == coro_rpc::errc::not_connected, - make_error_message(val)); + CHECK_MESSAGE(val == coro_rpc::errc::not_connected, val.message()); } #endif } diff --git a/src/coro_rpc/tests/test_coro_rpc_server.cpp b/src/coro_rpc/tests/test_coro_rpc_server.cpp index af096fe73..f40848aaf 100644 --- a/src/coro_rpc/tests/test_coro_rpc_server.cpp +++ b/src/coro_rpc/tests/test_coro_rpc_server.cpp @@ -120,7 +120,7 @@ struct CoroServerTester : ServerTester { ELOGV(INFO, "run %s", __func__); auto ec = server.start(); - REQUIRE_MESSAGE(ec == coro_rpc::errc::io_error, make_error_message(ec)); + REQUIRE_MESSAGE(ec == coro_rpc::errc::io_error, ec.message()); } void test_start_new_server_with_same_port() { @@ -130,7 +130,7 @@ struct CoroServerTester : ServerTester { auto ec = new_server.async_start(); REQUIRE(!ec); REQUIRE_MESSAGE(ec.error() == coro_rpc::errc::address_in_use, - make_error_message(ec.error())); + ec.error().message()); } ELOGV(INFO, "OH NO"); } @@ -251,18 +251,16 @@ TEST_CASE("test server accept error") { ELOGV(INFO, "run test server accept error, client_id %d", client.get_client_id()); auto ec = syncAwait(client.connect("127.0.0.1", "8810")); - REQUIRE_MESSAGE( - !ec, - std::to_string(client.get_client_id()).append(make_error_message(ec))); + REQUIRE_MESSAGE(!ec, + std::to_string(client.get_client_id()).append(ec.message())); auto ret = syncAwait(client.call()); REQUIRE_MESSAGE(ret.error().code == coro_rpc::errc::io_error, ret.error().msg); REQUIRE(client.has_closed() == true); ec = syncAwait(client.connect("127.0.0.1", "8810")); - REQUIRE_MESSAGE( - ec == coro_rpc::errc::io_error, - std::to_string(client.get_client_id()).append(make_error_message(ec))); + REQUIRE_MESSAGE(ec == coro_rpc::errc::io_error, + std::to_string(client.get_client_id()).append(ec.message())); ret = syncAwait(client.call()); CHECK(!ret); REQUIRE(client.has_closed() == true); @@ -320,7 +318,7 @@ TEST_CASE("test server write queue") { std::size_t sz; auto ret = struct_pack::deserialize_to(r2, buffer_read.data(), body_len, sz); - CHECK(ret == struct_pack::errc::ok); + CHECK(!ret); CHECK(sz == body_len); CHECK(r2 == r); } @@ -345,9 +343,8 @@ TEST_CASE("testing coro rpc write error") { ELOGV(INFO, "run testing coro rpc write error, client_id %d", client.get_client_id()); auto ec = syncAwait(client.connect("127.0.0.1", "8810")); - REQUIRE_MESSAGE( - !ec, - std::to_string(client.get_client_id()).append(make_error_message(ec))); + REQUIRE_MESSAGE(!ec, + std::to_string(client.get_client_id()).append(ec.message())); auto ret = syncAwait(client.call()); REQUIRE_MESSAGE( ret.error().code == coro_rpc::errc::io_error, diff --git a/src/coro_rpc/tests/test_router.cpp b/src/coro_rpc/tests/test_router.cpp index fd6780196..b2c6a2503 100644 --- a/src/coro_rpc/tests/test_router.cpp +++ b/src/coro_rpc/tests/test_router.cpp @@ -73,11 +73,11 @@ get_result(const auto &pair) { using return_type = rpc_result, coro_rpc_protocol>; rpc_return_type_t ret; - struct_pack::errc ec; + struct_pack::err_code ec; coro_rpc_protocol::rpc_error err; if (!rpc_errc) { ec = struct_pack::deserialize_to(ret, buffer); - if (ec == struct_pack::errc::ok) { + if (!ec) { if constexpr (std::is_same_v) { return {}; } @@ -89,7 +89,7 @@ get_result(const auto &pair) { else { err.code = rpc_errc; ec = struct_pack::deserialize_to(err.msg, buffer); - if (ec == struct_pack::errc::ok) { + if (!ec) { return return_type{unexpect_t{}, std::move(err)}; } } @@ -106,10 +106,10 @@ void check_result(const auto &pair, size_t offset = 0) { std::string_view data(buffer.data(), buffer.size()); typename RPC_trait::return_type r; auto res = struct_pack::deserialize_to(r, data); - if (res != struct_pack::errc{}) { + if (res) { coro_rpc_protocol::rpc_error r; auto res = struct_pack::deserialize_to(r, data); - CHECK(res == struct_pack::errc{}); + CHECK(!res); } } @@ -345,7 +345,7 @@ TEST_CASE("testing object arguments") { auto buf = struct_pack::serialize(p); person p1; auto ec = struct_pack::deserialize_to(p1, buf); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); test_route_and_check(ctx, p); router.register_handler(); diff --git a/src/struct_pack/examples/basic_usage.cpp b/src/struct_pack/examples/basic_usage.cpp index c2985219f..28c0571bc 100644 --- a/src/struct_pack/examples/basic_usage.cpp +++ b/src/struct_pack/examples/basic_usage.cpp @@ -113,7 +113,7 @@ void basic_usage() { { person p2; [[maybe_unused]] auto ec = struct_pack::deserialize_to(p2, buffer); - assert(ec == struct_pack::errc{}); + assert(!ec); assert(p == p2); } // api 3. partial deserialize @@ -137,7 +137,7 @@ void basic_usage() { auto buffer = struct_pack::serialize(p.age, p2.name); [[maybe_unused]] auto result = struct_pack::deserialize_to(p3.age, buffer, p3.name); - assert(result == struct_pack::errc{}); + assert(!result); assert(p3.age == p.age); assert(p3.name == p2.name); } diff --git a/src/struct_pack/examples/user_defined_serialization.cpp b/src/struct_pack/examples/user_defined_serialization.cpp index 261737097..1f0701ac3 100644 --- a/src/struct_pack/examples/user_defined_serialization.cpp +++ b/src/struct_pack/examples/user_defined_serialization.cpp @@ -62,11 +62,11 @@ void sp_serialize_to(Writer& writer, const array2D& ar) { } // 3. sp_deserialize_to: deserilize object from reader template -struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { - if (auto ec = struct_pack::read(reader, ar.x); ec != struct_pack::errc{}) { +struct_pack::err_code sp_deserialize_to(Reader& reader, array2D& ar) { + if (auto ec = struct_pack::read(reader, ar.x); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.y); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.y); ec) { return ec; } auto length = 1ull * ar.x * ar.y * sizeof(float); @@ -78,7 +78,7 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { } ar.p = (float*)malloc(length); auto ec = struct_pack::read(reader, ar.p, 1ull * ar.x * ar.y); - if (ec != struct_pack::errc{}) { + if (ec) { free(ar.p); } return ec; diff --git a/src/struct_pack/tests/test_alignas.cpp b/src/struct_pack/tests/test_alignas.cpp index 3131cbbd1..eafd0fa8e 100644 --- a/src/struct_pack/tests/test_alignas.cpp +++ b/src/struct_pack/tests/test_alignas.cpp @@ -52,7 +52,7 @@ TEST_CASE("testing no alignas") { REQUIRE(literal == val); auto buf = struct_pack::serialize(t); auto ret = struct_pack::deserialize(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); T d_t = ret.value(); CHECK(t == d_t); } @@ -81,14 +81,14 @@ TEST_CASE("testing alignas(2)") { SUBCASE("deserialize to dummy") { using DT = test_alignas::dummy; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } SUBCASE("deserialize to dummy_2") { using DT = test_alignas::dummy_2; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } @@ -128,7 +128,7 @@ TEST_CASE("testing alignas(4)") { SUBCASE("deserialize to dummy_4") { using DT = test_alignas::dummy_4; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } @@ -173,7 +173,7 @@ TEST_CASE("testing alignas(8)") { SUBCASE("deserialize to dummy_8") { using DT = test_alignas::dummy_8; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } diff --git a/src/struct_pack/tests/test_compatible.cpp b/src/struct_pack/tests/test_compatible.cpp index 0e74abeb6..eb759ad9c 100644 --- a/src/struct_pack/tests/test_compatible.cpp +++ b/src/struct_pack/tests/test_compatible.cpp @@ -145,7 +145,7 @@ TEST_CASE("test compatible") { person p; auto res = deserialize_to(p, buffer.data(), buffer.size()); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(p.name == p1.name); CHECK(p.age == p1.age); @@ -171,7 +171,7 @@ TEST_CASE("test compatible") { person1 p1, p0 = {20, "tom"}; auto ec = struct_pack::deserialize_to(p1, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(p1 == p0); } SUBCASE("big compatible metainfo") { diff --git a/src/struct_pack/tests/test_compile_time_calculate.cpp b/src/struct_pack/tests/test_compile_time_calculate.cpp index e83956053..3b3625c08 100644 --- a/src/struct_pack/tests/test_compile_time_calculate.cpp +++ b/src/struct_pack/tests/test_compile_time_calculate.cpp @@ -169,14 +169,14 @@ TEST_CASE("type calculate") { "T[sz] with different T should get different MD5"); int ar[5] = {}; float ar2[5] = {}; - CHECK(deserialize_to(ar2, serialize(ar)) != struct_pack::errc{}); + CHECK(deserialize_to(ar2, serialize(ar))); } { static_assert(get_type_code() != get_type_code(), "T[sz] with different sz should get different MD5"); int ar[5] = {}; int ar2[6] = {}; - CHECK(deserialize_to(ar2, serialize(ar)) != struct_pack::errc{}); + CHECK(deserialize_to(ar2, serialize(ar))); } { static_assert(get_type_code>() != diff --git a/src/struct_pack/tests/test_data_struct.cpp b/src/struct_pack/tests/test_data_struct.cpp index f9cc0259a..23b6de757 100644 --- a/src/struct_pack/tests/test_data_struct.cpp +++ b/src/struct_pack/tests/test_data_struct.cpp @@ -11,14 +11,14 @@ void test_container(T &v) { auto ret = serialize(v); T v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v == v1); v.clear(); v1.clear(); ret = serialize(v); ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v1.empty()); CHECK(v == v1); } @@ -162,7 +162,7 @@ void test_tuple_like(T &v) { T v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v == v1); } @@ -225,11 +225,11 @@ TEST_CASE("test_trivial_copy_tuple") { std::tuple v{}; auto ec = deserialize_to(v, buf); - CHECK(ec != struct_pack::errc{}); + CHECK(ec); decltype(tp) tp1; auto ec2 = deserialize_to(tp1, buf); - CHECK(ec2 == struct_pack::errc{}); + CHECK(!ec2); CHECK(tp == tp1); } @@ -246,7 +246,7 @@ TEST_CASE("test_trivial_copy_tuple in an object") { test_obj obj1; auto ec = deserialize_to(obj1, buf); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(obj.tp == obj1.tp); } #endif @@ -256,7 +256,7 @@ void test_c_array(T &v) { T v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); auto size = std::extent_v; for (decltype(size) i = 0; i < size; ++i) { @@ -286,7 +286,7 @@ TEST_CASE("testing enum") { auto ret = serialize(e); std::size_t sz; auto ec = deserialize_to(e2, ret.data(), ret.size(), sz); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(sz == ret.size()); CHECK(e == e2); } @@ -347,14 +347,14 @@ TEST_CASE("test variant") { std::variant var = 1.4, var2; auto ret = struct_pack::serialize(var); auto ec = struct_pack::deserialize_to(var2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(var2 == var); } { std::variant var = std::monostate{}, var2; auto ret = struct_pack::serialize(var); auto ec = struct_pack::deserialize_to(var2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(var2 == var); } { @@ -363,7 +363,7 @@ TEST_CASE("test variant") { var2; auto ret = struct_pack::serialize(var); auto ec = struct_pack::deserialize_to(var2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(var2 == var); CHECK(var2.index() == 1); } @@ -371,7 +371,7 @@ TEST_CASE("test variant") { std::variant var{"hello"}, var2; auto ret = struct_pack::serialize(var); auto ec = struct_pack::deserialize_to(var2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(var2 == var); } { @@ -398,7 +398,7 @@ TEST_CASE("test variant") { big_variant2; auto ret = struct_pack::serialize(big_variant); auto ec = struct_pack::deserialize_to(big_variant2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(big_variant2 == big_variant); } } diff --git a/src/struct_pack/tests/test_data_struct2.cpp b/src/struct_pack/tests/test_data_struct2.cpp index cfb7662e6..3c9b820af 100644 --- a/src/struct_pack/tests/test_data_struct2.cpp +++ b/src/struct_pack/tests/test_data_struct2.cpp @@ -16,7 +16,7 @@ TEST_CASE("test monostate") { #endif auto ret = struct_pack::serialize(var); auto ec = struct_pack::deserialize_to(var2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(var2 == var); } } @@ -26,7 +26,7 @@ TEST_CASE("test expected") { tl::expected exp{42}, exp2; auto ret = serialize(exp); auto res = deserialize_to(exp2, ret.data(), ret.size()); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(exp2 == exp); } { @@ -35,7 +35,7 @@ TEST_CASE("test expected") { exp2; auto ret = serialize(exp); auto res = deserialize_to(exp2, ret.data(), ret.size()); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(exp2 == exp); } { @@ -45,7 +45,7 @@ TEST_CASE("test expected") { auto ret = serialize(exp); auto res = deserialize_to(exp2, ret.data(), ret.size()); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(exp2 == exp); } } @@ -56,7 +56,7 @@ TEST_CASE("testing object with containers, enum, tuple array, and pair") { complicated_object v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v.a == v1.a); CHECK(v == v1); @@ -67,7 +67,7 @@ TEST_CASE("testing object with containers, enum, tuple array, and pair") { nested_object nested1{}; auto ec = deserialize_to(nested1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(nested == nested1); } @@ -76,7 +76,7 @@ TEST_CASE("testing object with containers, enum, tuple array, and pair") { complicated_object v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); auto pair = get_field(ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(pair); CHECK(pair.value() == "hello"); pair = get_field(ret); @@ -98,17 +98,17 @@ TEST_CASE("testing object with containers, enum, tuple array, and pair") { SUBCASE("test get_field_to") { std::string res1; auto ec = get_field_to(res1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res1 == "hello"); ec = get_field_to(res1, ret); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res1 == "hello"); std::pair res2; ec = get_field_to(res2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res2 == v.o); ec = get_field_to(res2, ret); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res2 == v.o); auto res = get_field_to(res2, ret.data(), 24); @@ -131,7 +131,7 @@ TEST_CASE("testing string_view deserialize") { auto ret = serialize(sv); std::wstring str; auto ec = deserialize_to(str, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(str == sv); } { @@ -167,7 +167,7 @@ TEST_CASE("test wide string") { auto ret = serialize(sv); std::wstring str; auto ec = deserialize_to(str, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(str == sv); } #if __cpp_char8_t >= 201811L @@ -176,7 +176,7 @@ TEST_CASE("test wide string") { auto ret = serialize(sv); std::u8string str; auto ec = deserialize_to(str, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(str == sv); } #endif @@ -185,7 +185,7 @@ TEST_CASE("test wide string") { auto ret = serialize(sv); std::u16string str; auto ec = deserialize_to(str, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(str == sv); } { @@ -193,7 +193,7 @@ TEST_CASE("test wide string") { auto ret = serialize(sv); std::u32string str; auto ec = deserialize_to(str, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(str == sv); } } @@ -203,28 +203,28 @@ TEST_CASE("char test") { char ch = '1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } { signed char ch = '1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } { unsigned char ch = '1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } { wchar_t ch = L'1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } #ifdef __cpp_lib_char8_t @@ -232,7 +232,7 @@ TEST_CASE("char test") { char8_t ch = u8'1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } #endif @@ -240,14 +240,14 @@ TEST_CASE("char test") { char16_t ch = u'1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } { char32_t ch = U'1', ch2; auto ret = serialize(ch); auto ec = deserialize_to(ch2, ret.data(), ret.size()); - REQUIRE(ec == struct_pack::errc{}); + REQUIRE(!ec); CHECK(ch == ch2); } } @@ -257,7 +257,7 @@ TEST_CASE("test deque") { auto ret = struct_pack::serialize(raw); std::deque res; auto ec = struct_pack::deserialize_to(res, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(raw == res); } diff --git a/src/struct_pack/tests/test_derived.cpp b/src/struct_pack/tests/test_derived.cpp index 5a69afe0f..ce826e43d 100644 --- a/src/struct_pack/tests/test_derived.cpp +++ b/src/struct_pack/tests/test_derived.cpp @@ -3,7 +3,7 @@ namespace test1 { struct Base { int id = 17; Base(){}; - static struct_pack::expected, struct_pack::errc> + static struct_pack::expected, struct_pack::err_code> deserialize(std::string_view sv); virtual std::string get_name() const { return "Base"; }; friend bool operator==(const Base& a, const Base& b) { return a.id == b.id; } @@ -67,7 +67,7 @@ struct gua : Base { }; STRUCT_PACK_REFL(gua, id, a, b); -struct_pack::expected, struct_pack::errc> +struct_pack::expected, struct_pack::err_code> Base::deserialize(std::string_view sv) { return struct_pack::deserialize_derived_class(sv); diff --git a/src/struct_pack/tests/test_disable_meta_info.cpp b/src/struct_pack/tests/test_disable_meta_info.cpp index e55b29406..f7a1991bc 100644 --- a/src/struct_pack/tests/test_disable_meta_info.cpp +++ b/src/struct_pack/tests/test_disable_meta_info.cpp @@ -15,7 +15,7 @@ TEST_CASE("test serialize/deserialize rect") { CHECK(buffer.size() == sizeof(rect)); auto ec = struct_pack::deserialize_to< struct_pack::sp_config::DISABLE_ALL_META_INFO>(r2, buffer); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { @@ -39,7 +39,7 @@ TEST_CASE("test serialize/deserialize rect") { auto ec = struct_pack::deserialize_to< struct_pack::sp_config::DISABLE_ALL_META_INFO>(r2, buffer.data(), buffer.size()); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { @@ -66,7 +66,7 @@ TEST_CASE("test serialize/deserialize rect by ADL") { auto buffer = struct_pack::serialize(r); CHECK(buffer.size() == sizeof(rect)); auto ec = struct_pack::deserialize_to(r2, buffer); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { @@ -83,7 +83,7 @@ TEST_CASE("test serialize/deserialize rect by ADL") { auto buffer = struct_pack::serialize(r); CHECK(buffer.size() == sizeof(rect)); auto ec = struct_pack::deserialize_to(r2, buffer.data(), buffer.size()); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { @@ -106,7 +106,7 @@ TEST_CASE("test serialize/deserialize person") { CHECK(buffer.size() == 11); auto ec = struct_pack::deserialize_to< struct_pack::sp_config::DISABLE_ALL_META_INFO>(r2, buffer); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { @@ -130,7 +130,7 @@ TEST_CASE("test serialize/deserialize person") { auto ec = struct_pack::deserialize_to< struct_pack::sp_config::DISABLE_ALL_META_INFO>(r2, buffer.data(), buffer.size()); - CHECK(ec == struct_pack::errc::ok); + CHECK(!ec); CHECK(r == r2); } { diff --git a/src/struct_pack/tests/test_pragma_pack.cpp b/src/struct_pack/tests/test_pragma_pack.cpp index 04e9206e3..c2dbb7c80 100644 --- a/src/struct_pack/tests/test_pragma_pack.cpp +++ b/src/struct_pack/tests/test_pragma_pack.cpp @@ -73,7 +73,7 @@ TEST_CASE("testing no #pragam pack") { REQUIRE(literal == val); auto buf = struct_pack::serialize(t); auto ret = struct_pack::deserialize(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); T d_t = ret.value(); CHECK(t == d_t); } @@ -113,7 +113,7 @@ TEST_CASE("testing #pragma pack(1)") { SUBCASE("deserialize to dummy_1") { using DT = test_pragma_pack::dummy_1; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } @@ -149,7 +149,7 @@ TEST_CASE("testing #pragma pack(2)") { SUBCASE("deserialize to dummy") { using DT = test_pragma_pack::dummy; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); } SUBCASE("deserialize to dummy_1") { using DT = test_pragma_pack::dummy_1; @@ -159,7 +159,7 @@ TEST_CASE("testing #pragma pack(2)") { SUBCASE("deserialize to dummy_2") { using DT = test_pragma_pack::dummy_2; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } diff --git a/src/struct_pack/tests/test_pragma_pack_and_alignas_mix.cpp b/src/struct_pack/tests/test_pragma_pack_and_alignas_mix.cpp index e2a867051..652cdc163 100644 --- a/src/struct_pack/tests/test_pragma_pack_and_alignas_mix.cpp +++ b/src/struct_pack/tests/test_pragma_pack_and_alignas_mix.cpp @@ -52,7 +52,7 @@ TEST_CASE("testing no one") { REQUIRE(literal == val); auto buf = struct_pack::serialize(t); auto ret = struct_pack::deserialize(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); T d_t = ret.value(); CHECK(t == d_t); } @@ -100,7 +100,7 @@ TEST_CASE("testing #pragam pack(1), alignas(2)") { SUBCASE("deserialize to dummy_1_2") { using DT = test_pragma_pack_and_alignas_mix::dummy_1_2; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } @@ -164,7 +164,7 @@ TEST_CASE("testing #pragam pack(1), alignas(2)") { SUBCASE("deserialize to dummy_1_4") { using DT = test_pragma_pack_and_alignas_mix::dummy_1_4; auto ret = struct_pack::deserialize
(buf); - REQUIRE_MESSAGE(ret, struct_pack::error_message(ret.error())); + REQUIRE_MESSAGE(ret, ret.error().message()); DT d_t = ret.value(); CHECK(t == d_t); } diff --git a/src/struct_pack/tests/test_serialize.cpp b/src/struct_pack/tests/test_serialize.cpp index 0edab4546..208de7b8f 100644 --- a/src/struct_pack/tests/test_serialize.cpp +++ b/src/struct_pack/tests/test_serialize.cpp @@ -64,14 +64,14 @@ TEST_CASE("testing deserialize") { { person p2; auto res = deserialize_to(p2, ret); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(p2 == p); } { size_t len = 114514; person p2; auto res = deserialize_to(p2, ret, len); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(p2 == p); CHECK(len == ret.size()); } @@ -79,7 +79,7 @@ TEST_CASE("testing deserialize") { size_t offset = 0; person p2; auto res = deserialize_to(p2, ret, offset); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(p2 == p); CHECK(offset == ret.size()); } @@ -115,10 +115,10 @@ TEST_CASE("testing api") { serialize_to((char *)my_buffer2.data() + offset, size, p); person p1, p2; auto ec1 = deserialize_to(p1, my_buffer); - CHECK(ec1 == struct_pack::errc{}); + CHECK(!ec1); auto ec2 = deserialize_to(p2, (const char *)my_buffer2.data() + offset, my_buffer2.size() - offset); - CHECK(ec2 == struct_pack::errc{}); + CHECK(!ec2); CHECK(p == p1); CHECK(p == p2); } @@ -150,7 +150,7 @@ TEST_CASE("testing pack object") { person p1{}; auto ec = deserialize_to(p1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(p == p1); CHECK(deserialize_to(p1, ret.data(), 4) == @@ -161,7 +161,7 @@ TEST_CASE("testing pack object") { ret = serialize(p); person p2{}; ec = deserialize_to(p2, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(p == p2); } } @@ -172,14 +172,14 @@ void test_container(T &v) { T v1{}; auto ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v == v1); v.clear(); v1.clear(); ret = serialize(v); ec = deserialize_to(v1, ret.data(), ret.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(v1.empty()); CHECK(v == v1); } @@ -295,7 +295,7 @@ TEST_CASE("testing serialize/deserialize variadic params") { int a[5]; auto res = struct_pack::deserialize_to(a[0], ret.data(), ret.size(), a[1], a[2], a[3], a[4]); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(a[0] == 1); CHECK(a[1] == 2); CHECK(a[2] == 3); @@ -306,7 +306,7 @@ TEST_CASE("testing serialize/deserialize variadic params") { auto ret = struct_pack::serialize(1, 2, 3, 4, 5); int a[5]; auto res = struct_pack::deserialize_to(a[0], ret, a[1], a[2], a[3], a[4]); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(a[0] == 1); CHECK(a[1] == 2); CHECK(a[2] == 3); @@ -477,7 +477,7 @@ TEST_CASE("testing serialize/deserialize variadic params") { std::get<0>(t), ret.data(), ret.size(), std::get<1>(t), std::get<2>(t), std::get<3>(t), std::get<4>(t), std::get<5>(t), std::get<6>(t), std::get<7>(t)); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(std::get<0>(t) == 42); CHECK(std::get<1>(t) == 2.71828f); CHECK(std::get<2>(t) == 3.1415926); @@ -504,7 +504,7 @@ TEST_CASE("testing serialize/deserialize variadic params") { auto res = struct_pack::deserialize_to( std::get<0>(t), ret, std::get<1>(t), std::get<2>(t), std::get<3>(t), std::get<4>(t), std::get<5>(t), std::get<6>(t), std::get<7>(t)); - CHECK(res == struct_pack::errc{}); + CHECK(!res); CHECK(std::get<0>(t) == 42); CHECK(std::get<1>(t) == 2.71828f); CHECK(std::get<2>(t) == 3.1415926); @@ -657,13 +657,13 @@ TEST_CASE("array test") { { ar3[117] = test_str; [[maybe_unused]] auto ret = deserialize_to(ar3_1, serialize(ar3)); - CHECK(ret == struct_pack::errc{}); + CHECK(!ret); CHECK(ar3_1[117] == test_str); } { ar4[15472] = test_str; auto ret = deserialize_to(ar4_1, serialize(ar4)); - CHECK(ret == struct_pack::errc{}); + CHECK(!ret); CHECK(ar4_1[15472] == test_str); } } @@ -849,7 +849,7 @@ TEST_CASE("test set_value") { detail::memory_reader reader(ret.data(), ret.data() + ret.size()); detail::unpacker in(reader); person p2; - struct_pack::errc ec; + struct_pack::err_code ec; std::string s; int v; int v2 = -1; @@ -871,12 +871,12 @@ TEST_CASE("test free functions") { std::optional op1{}; auto buf1 = serialize(op1); std::optional op2{}; - CHECK(deserialize_to(op2, buf1.data(), buf1.size()) == struct_pack::errc{}); + CHECK(!deserialize_to(op2, buf1.data(), buf1.size())); CHECK(!op2.has_value()); op1 = 42; auto buf2 = serialize(op1); - CHECK(deserialize_to(op2, buf2.data(), buf2.size()) == struct_pack::errc{}); + CHECK(!deserialize_to(op2, buf2.data(), buf2.size())); CHECK(op2.has_value()); CHECK(op2.value() == 42); @@ -1003,14 +1003,14 @@ TEST_CASE("test de_serialize offset") { std::string res; auto ec = struct_pack::deserialize_to_with_offset(res, ho.data(), ho.size(), offset); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res == hi + std::to_string(i)); CHECK(offset == sizes[i]); } for (size_t i = 0, offset = 0; i < 100; ++i) { std::string res; auto ec = struct_pack::deserialize_to_with_offset(res, ho, offset); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(res == hi + std::to_string(i)); CHECK(offset == sizes[i]); } @@ -1099,7 +1099,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(ar == ar2); } @@ -1109,7 +1109,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(s.ar == ar2); } @@ -1130,7 +1130,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test3 s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(ar == ar2); } @@ -1140,7 +1140,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test3 s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(s.ar == ar2); } @@ -1166,7 +1166,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test5 s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(ar == ar2); } @@ -1178,7 +1178,7 @@ TEST_CASE("test static span") { auto buffer = struct_pack::serialize(s); span_test5 s2 = {"", ar2}; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(s.ar == ar2); } @@ -1233,7 +1233,7 @@ TEST_CASE("test dynamic span") { auto buffer = struct_pack::serialize(s); dspan_test s2; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(std::equal(ar.begin(), ar.end(), s2.sp.begin())); } @@ -1242,7 +1242,7 @@ TEST_CASE("test dynamic span") { auto buffer = struct_pack::serialize(s); dspan_test s2; auto ec = struct_pack::deserialize_to(s2, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(s.hello == s2.hello); CHECK(std::equal(s.ar.begin(), s.ar.end(), s2.sp.begin())); } diff --git a/src/struct_pack/tests/test_stream.cpp b/src/struct_pack/tests/test_stream.cpp index fa8525e39..f39c8a0a5 100644 --- a/src/struct_pack/tests/test_stream.cpp +++ b/src/struct_pack/tests/test_stream.cpp @@ -68,21 +68,21 @@ TEST_CASE("test serialize_to/deserialize file") { for (int i = 0; i < 100; ++i) { person person1; auto ec = struct_pack::deserialize_to(person1, ifs); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(person1 == p1); nested_object nested1; ec = struct_pack::deserialize_to(nested1, ifs); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(nested1 == nested); } for (int i = 0; i < 100; ++i) { person person2; auto ec = struct_pack::deserialize_to(person2, ifs); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(person2 == p2); nested_object nested1; ec = struct_pack::deserialize_to(nested1, ifs); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(nested1 == nested); } } @@ -130,7 +130,7 @@ TEST_CASE("test get_field file") { CHECK(ifs.read((char *)&sz, 4)); std::string name; auto ec = struct_pack::get_field_to(name, ifs); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(name == p1.name); CHECK(ifs.seekg(pos + sz)); } diff --git a/src/struct_pack/tests/test_user_defined_type.cpp b/src/struct_pack/tests/test_user_defined_type.cpp index 6ebab01be..81875b24b 100644 --- a/src/struct_pack/tests/test_user_defined_type.cpp +++ b/src/struct_pack/tests/test_user_defined_type.cpp @@ -66,22 +66,20 @@ void sp_serialize_to(Writer& writer, const array2D& ar) { } template -struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { - if (auto ec = struct_pack::read(reader, ar.name); ec != struct_pack::errc{}) { +struct_pack::err_code sp_deserialize_to(Reader& reader, array2D& ar) { + if (auto ec = struct_pack::read(reader, ar.name); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.values); - ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.values); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.values2); - ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.values2); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.x); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.x); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.y); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.y); ec) { return ec; } if constexpr (struct_pack::checkable_reader_t) { @@ -93,37 +91,37 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { } ar.p = (float*)malloc(1ull * ar.x * ar.y * sizeof(float)); auto ec = struct_pack::read(reader, ar.p, 1ull * ar.x * ar.y); - if (ec != struct_pack::errc{}) { + if (ec) { free(ar.p); } return ec; } template -struct_pack::errc sp_deserialize_to_with_skip(Reader& reader, array2D& ar) { +struct_pack::err_code sp_deserialize_to_with_skip(Reader& reader, array2D& ar) { if (auto ec = struct_pack::read( reader, ar.name); // skip this field - ec != struct_pack::errc{}) { + ec) { return ec; } if (auto ec = struct_pack::read(reader, ar.values); - ec != struct_pack::errc{}) { // skip this field + ec) { // skip this field return ec; } if (auto ec = struct_pack::read(reader, ar.values2); - ec != struct_pack::errc{}) { // skip this field + ec) { // skip this field return ec; } - if (auto ec = struct_pack::read(reader, ar.x); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.x); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.y); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.y); ec) { return ec; } if (auto ec = struct_pack::read(reader, ar.p, 1ull * ar.x * ar.y); - ec != struct_pack::errc{}) { + ec) { return ec; } return {}; @@ -233,7 +231,7 @@ void sp_serialize_to(Writer& writer, const test& t) { } template -struct_pack::errc sp_deserialize_to(Reader& reader, test& ar) { +struct_pack::err_code sp_deserialize_to(Reader& reader, test& ar) { return struct_pack::read(reader, ar.x); } constexpr std::string_view sp_set_type_name(test*) { return "myint"; } diff --git a/website/docs/en/struct_pack/struct_pack_intro.md b/website/docs/en/struct_pack/struct_pack_intro.md index f3dbe0473..c8ea2b80c 100644 --- a/website/docs/en/struct_pack/struct_pack_intro.md +++ b/website/docs/en/struct_pack/struct_pack_intro.md @@ -92,8 +92,8 @@ assert(person2.value()==person1); ```cpp // deserialize to an existing object person person2; -std::errc ec = deserialize_to(person2, buffer); -assert(ec==std::errc{}); // person2.has_value() == true +auto ec = deserialize_to(person2, buffer); +assert(!ec); // no error assert(person2==person1); ``` @@ -327,11 +327,11 @@ void sp_serialize_to(Writer& writer, const array2D& ar) { } // 3. sp_deserialize_to: deserilize object from reader template -struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { - if (auto ec = struct_pack::read(reader, ar.x); ec != struct_pack::errc{}) { +struct_pack::err_code sp_deserialize_to(Reader& reader, array2D& ar) { + if (auto ec = struct_pack::read(reader, ar.x); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.y); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.y); ec) { return ec; } auto length = 1ull * ar.x * ar.y * sizeof(float); @@ -343,7 +343,7 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { } ar.p = (float*)malloc(length); auto ec = struct_pack::read(reader, ar.p, 1ull * ar.x * ar.y); - if (ec != struct_pack::errc{}) { + if (ec) { free(ar.p); } return ec; @@ -357,7 +357,7 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { // need also define this function to skip deserialization of your type: // template -// struct_pack::errc sp_deserialize_to_with_skip(Reader& reader, array2D& ar); +// struct_pack::err_code sp_deserialize_to_with_skip(Reader& reader, array2D& ar); } diff --git a/website/docs/zh/coro_rpc/coro_rpc_doc.hpp b/website/docs/zh/coro_rpc/coro_rpc_doc.hpp index 9807645fa..3c36592cd 100644 --- a/website/docs/zh/coro_rpc/coro_rpc_doc.hpp +++ b/website/docs/zh/coro_rpc/coro_rpc_doc.hpp @@ -65,13 +65,13 @@ class coro_rpc_server { * * @return 正常启动返回空,否则返回错误码 */ - async_simple::coro::Lazy async_start() noexcept; + async_simple::coro::Lazy async_start() noexcept; /*! * 阻塞方式启动server, 如果端口被占用将会返回非空的错误码 * * @return 正常启动返回空,否则返回错误码 */ - coro_rpc::errc start(); + coro_rpc::err_code start(); /*! * 停止server,阻塞等待直到server停止; */ @@ -159,7 +159,7 @@ class coro_rpc_server { * */ struct rpc_error { - coro_rpc::errc code; + coro_rpc::err_code code; std::string msg; }; @@ -215,7 +215,7 @@ class coro_rpc_client { * @param timeout_duration rpc调用超时时间 * @return 连接错误码,为空表示连接失败 */ - async_simple::coro::Lazy connect( + async_simple::coro::Lazy connect( const std::string &host, const std::string &port, std::chrono::steady_clock::duration timeout_duration = std::chrono::seconds(5)); diff --git a/website/docs/zh/struct_pack/struct_pack_doc.hpp b/website/docs/zh/struct_pack/struct_pack_doc.hpp index 557f32d0f..75c2df369 100644 --- a/website/docs/zh/struct_pack/struct_pack_doc.hpp +++ b/website/docs/zh/struct_pack/struct_pack_doc.hpp @@ -39,7 +39,7 @@ namespace struct_pack { int test() { person p{20, "tom"}; auto buffer = struct_pack::serialize(p); - struct_pack::expected p2 = + struct_pack::expected p2 = struct_pack::deserialize(buffer.data(), buffer.size()); assert(p2); assert(p == p2.value()); @@ -194,13 +194,13 @@ struct trivial_view { person_v1 p1; // deserialize person_v2 as person_v1 auto ec = struct_pack::deserialize_to(p1, buf.data(), buf.size()); - CHECK(ec == std::errc{}); + CHECK(!ec); auto buf1 = struct_pack::serialize(p1); person_v2 p3; // deserialize person_v1 as person_v2 auto ec = struct_pack::deserialize_to(p3, buf1.data(), buf1.size()); - CHECK(ec == std::errc{}); + CHECK(!ec); @endcode * * 升级接口时应注意保持版本号递增 原则: @@ -378,11 +378,72 @@ struct string_literal { CharType ar[Size + 1]; }; +/*! + * \ingroup struct_pack + * \struct err_code + * \brief + * struct_pack的错误码,存储了一个枚举值`struct_pack::errc`,可用于判断序列化是否成功。 + */ + +struct err_code { + public: + struct_pack::errc ec; + /** + * @brief err_code的默认构造函数,默认情况下无错误 + * + */ + err_code() noexcept; + /** + * @brief 通过错误值枚举`struct_pack::errc`构造错误码 + * + * @param ec 错误值枚举,`struct_pack::errc`类型 + */ + err_code(struct_pack::errc ec) noexcept; + err_code &operator=(errc ec); + err_code(const err_code &err_code) noexcept; + err_code &operator=(const err_code &o) noexcept; + /** + * @brief 比较错误码是否相同 + * + * @param o 另外一个错误码 + * @return true 两个错误码相同 + * @return false 两个错误码不同 + */ + bool operator==(const err_code &o) const noexcept { return ec == o.ec; } + /** + * @brief 比较错误码是否不同 + * + * @param o 另外一个错误码 + * @return true 两个错误码不同 + * @return false 两个错误码相同 + */ + bool operator!=(const err_code &o); + /** + * @brief 判断是否有错误 + * + * @return true 有错误 + * @return false 无错误 + */ + operator bool() const noexcept { return ec != errc::ok; } + /** + * @brief 将错误码转换为整数 + * + * @return int + */ + int val() const noexcept; + /** + * @brief 返回错误码对应的错误消息 + * + * @return std::string_view + */ + std::string_view message() const noexcept; +}; + /** * \ingroup struct_pack - * @brief struct_pack的错误码 + * @brief struct_pack的错误值枚举 * - * struct_pack的错误码枚举,各枚举值解释如下: + * struct_pack的错误值枚举,各枚举值解释如下: * 1. ok,代表无错误。 * 2. no_buffer_space,缓冲区耗尽,未能完成所有字段的反序列化。 * 3. invalid_buffer,读取到非法数据,无法将数据反序列化到指定类型。 @@ -701,7 +762,7 @@ template , * @param t 待反序列化的对象t * @param v 存有struct_pack序列化数据的视图 * @param args 待反序列化的对象args - * @return struct_pack::errc + * @return struct_pack::err_code * 返回错误码,如果返回值不等于`struct_pack::errc::ok`,说明反序列化失败 * * 当反序列化失败时,t的值可能被部分修改。 @@ -711,14 +772,14 @@ template , auto buffer = struct_pack::serialize(p); person p2; auto ec = struct_pack::deserialize_to(p2, buffer); - assert(ec == struct_pack::errc{}); + assert(!ec); assert(p == p2); @endcode */ template -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const View &v, - Args &...args); +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const View &v, + Args &...args); /** * \ingroup struct_pack @@ -732,7 +793,7 @@ template -[[nodiscard]] struct_pack::errc deserialize_to(T &t, const char *data, - size_t size, Args &...args); +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, const char *data, + size_t size, Args &...args); /** * \ingroup struct_pack * @brief 将输入流中的数据反序列化到目的对象 @@ -761,15 +822,15 @@ template * @param t 待反序列化的对象t * @param reader 输入流 * @param args - * @return struct_pack::errc + * @return struct_pack::err_code * 返回错误码,如果返回值不等于`struct_pack::errc::ok`,说明反序列化失败。 * * 当反序列化失败时,t的值可能被部分修改。 */ template -[[nodiscard]] struct_pack::errc deserialize_to(T &t, Reader &reader, - Args &...args); +[[nodiscard]] struct_pack::err_code deserialize_to(T &t, Reader &reader, + Args &...args); /** * \ingroup struct_pack @@ -784,16 +845,17 @@ template -[[nodiscard]] struct_pack::errc deserialize_to_with_offset(T &t, const View &v, - size_t &offset, - Args &...args); +[[nodiscard]] struct_pack::err_code deserialize_to_with_offset(T &t, + const View &v, + size_t &offset, + Args &...args); /** * \ingroup struct_pack @@ -808,13 +870,13 @@ template -[[nodiscard]] struct_pack::errc deserialize_to_with_offset( +[[nodiscard]] struct_pack::err_code deserialize_to_with_offset( T &t, const char *data, size_t size, size_t &offset, Args &...args); /** @@ -825,9 +887,9 @@ template * 反序列化对象的类型,至少应填写一个,当填写多个时,按`std::tuple`类型反序列化 * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param v 存有struct_pack序列化数据的视图 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -850,9 +912,9 @@ template * 反序列化对象的类型,至少应填写一个,当填写多个时,按`std::tuple`类型反序列化 * @param data 起始地址 * @param size 数据长度 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -874,9 +936,9 @@ template 反序列化对象的类型,至少应填写一个,当填写多个时,按`std::tuple`类型反序列化 * @tparam Reader 输入流类型Reader,该类型需要满足约束`struct_pack::reader_t` * @param v 输入流 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -900,9 +962,9 @@ template * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param v 存有struct_pack序列化数据的视图 * @param consume_len 出参,保存消耗的数据长度。 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 当错误发生时,consume_len会被设为0。 */ @@ -918,9 +980,9 @@ template * @param data 起始地址 * @param size 数据长度 * @param consume_len 出参,保存消耗的数据长度。 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 当错误发生时,consume_len会被设为0。 */ @@ -937,9 +999,9 @@ template * 反序列化对象的类型,至少应填写一个,当填写多个时,按`std::tuple`类型反序列化 * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param v 存有struct_pack序列化数据的视图 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -964,9 +1026,9 @@ template `类型反序列化 * @param data 起始地址 * @param size 数据长度 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -989,9 +1051,9 @@ template 反序列化对象的类型,至少应填写一个,当填写多个时,按`std::tuple`类型反序列化 * @tparam Reader 输入流类型Reader,该类型需要满足约束`struct_pack::reader_t` * @param v 输入流 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: @code{.cpp} @@ -1017,9 +1079,9 @@ template * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param v 存有struct_pack序列化数据的视图 * @param consume_len 出参,保存消耗的数据长度。 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 当错误发生时,consume_len会被设为0。 */ @@ -1038,9 +1100,9 @@ template + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 当错误发生时,consume_len会被设为0。 */ @@ -1058,9 +1120,9 @@ template * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param v 存有struct_pack序列化数据的视图 * @param offset 反序列化起始位置的偏移量 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` */ template [[nodiscard]] auto deserialize_with_offset(const View &v, size_t &offset); @@ -1075,9 +1137,9 @@ template * @param data 起始地址 * @param size 数据长度 * @param offset 反序列化起始位置的偏移量 - * @return struct_pack::expected + * @return struct_pack::expected * 类型,当Args有多个参数时,返回值类型为`struct_pack::expected, - * struct_pack::errc>`。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * struct_pack::err_code>`。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码: * @@ -1089,7 +1151,7 @@ template * } * for (size_t i = 0, offset = 0; i < 100; ++i) { * auto ret = struct_pack::deserialize_with_offset(ho, offset); - * CHECK(ret.errc == std::errc{}); + * CHECK(ret.has_value()); * CHECK(ret.value == hi + std::to_string(i)); * } @endcode @@ -1111,7 +1173,7 @@ template * @tparam View 视图类型,需满足`struct_pack::detail::deserialize_view`约束 * @param dst 目的对象 * @param v 数据视图 - * @return struct_pack::errc + * @return struct_pack::err_code * 返回错误码,如果返回值不等于`struct_pack::errc::ok`,说明反序列化失败 * * 当反序列化失败时,t的值可能被部分修改。 @@ -1122,14 +1184,14 @@ template auto buffer = serialize(p); int age; auto ec = get_field_to(age, buffer); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(age == 20); @endcode * */ template -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, const View &v); +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const View &v); /** * \ingroup struct_pack * @brief 从内存中反序列化一个字段并保存到目的对象 @@ -1142,7 +1204,7 @@ template (name, buffer.data(), buffer.size()); - CHECK(ec == struct_pack::errc{}); + CHECK(!ec); CHECK(name == "tom"); @endcode */ template -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, const char *data, - size_t size); +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, const char *data, + size_t size); /** * \ingroup struct_pack @@ -1172,14 +1234,14 @@ template -[[nodiscard]] struct_pack::errc get_field_to(Field &dst, Reader &reader); +[[nodiscard]] struct_pack::err_code get_field_to(Field &dst, Reader &reader); /** * \ingroup struct_pack @@ -1190,8 +1252,8 @@ template - * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * @return struct_pack::expected + * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码 @code{.cpp} @@ -1216,8 +1278,8 @@ template - * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * @return struct_pack::expected + * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 样例代码 @code{.cpp} @@ -1241,8 +1303,8 @@ template * @tparam conf 序列化配置,详见`struct_pack::sp_config` * @tparam Reader 输入流类型,需要满足`struct_pack::reader_t`约束 * @param reader 输入流 - * @return struct_pack::expected - * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + * @return struct_pack::expected + * 类型,其中TI代表T的第I个字段。该类型存储了反序列化结果或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` */ template @@ -1257,8 +1319,8 @@ template , - struct_pack::errc> - * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + struct_pack::err_code> + * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 本函数用于在不知道派生类具体类型的情况下,将其反序列化到基类的指针。 * @@ -1280,7 +1342,7 @@ template [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(const View &v); /** @@ -1292,8 +1354,8 @@ deserialize_derived_class(const View &v); * @param data 起始地址 * @param size 数据长度 * @return struct_pack::expected, - struct_pack::errc> - * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + struct_pack::err_code> + * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` * * 本函数用于在不知道派生类具体类型的情况下,将其反序列化到基类的指针。 * @@ -1315,7 +1377,7 @@ deserialize_derived_class(const View &v); */ template [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(const char *data, size_t size); /** @@ -1327,13 +1389,13 @@ deserialize_derived_class(const char *data, size_t size); * @tparam Reader 输入流类型,需要满足`struct_pack::reader_t`约束 * @param reader * @return struct_pack::expected, - struct_pack::errc> - * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::errc`类型的错误码。详见`struct_pack::expected` + struct_pack::err_code> + * 类型,该类型存储了反序列化结果(`std::unique_ptr`)或`struct_pack::err_code`类型的错误码。详见`struct_pack::expected` */ template [[nodiscard]] struct_pack::expected, - struct_pack::errc> + struct_pack::err_code> deserialize_derived_class(Reader &reader); } // namespace struct_pack diff --git a/website/docs/zh/struct_pack/struct_pack_intro.md b/website/docs/zh/struct_pack/struct_pack_intro.md index 1ba0f2a8e..dc5d9202a 100644 --- a/website/docs/zh/struct_pack/struct_pack_intro.md +++ b/website/docs/zh/struct_pack/struct_pack_intro.md @@ -87,8 +87,8 @@ assert(person2.value()==person1); ```cpp // 将结果保存到已有的对象中 person person2; -std::errc ec = deserialize_to(person2, buffer); -assert(ec==std::errc{}); // person2.has_value() == true +auto ec = deserialize_to(person2, buffer); +assert(!ec); // no error assert(person2==person1); ``` @@ -336,11 +336,11 @@ void sp_serialize_to(Writer& writer, const array2D& ar) { } // 3. sp_deserialize_to: 从reader反序列化对象 template -struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { - if (auto ec = struct_pack::read(reader, ar.x); ec != struct_pack::errc{}) { +struct_pack::err_code sp_deserialize_to(Reader& reader, array2D& ar) { + if (auto ec = struct_pack::read(reader, ar.x); ec) { return ec; } - if (auto ec = struct_pack::read(reader, ar.y); ec != struct_pack::errc{}) { + if (auto ec = struct_pack::read(reader, ar.y); ec) { return ec; } auto length = 1ull * ar.x * ar.y * sizeof(float); @@ -352,7 +352,7 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { } ar.p = (float*)malloc(length); auto ec = struct_pack::read(reader, ar.p, 1ull * ar.x * ar.y); - if (ec != struct_pack::errc{}) { + if (ec) { free(ar.p); } return ec; @@ -364,7 +364,7 @@ struct_pack::errc sp_deserialize_to(Reader& reader, array2D& ar) { // 5. 如果你想使用 struct_pack::get_field/struct_pack::get_field_to, 还需要定义下面的函数以跳过自定义类型的反序列化。 // template -// struct_pack::errc sp_deserialize_to_with_skip(Reader& reader, array2D& ar); +// struct_pack::err_code sp_deserialize_to_with_skip(Reader& reader, array2D& ar); } From 330bc4429e19657f1af3acafc6bb0345325227cf Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Mon, 15 Jan 2024 20:17:33 +0800 Subject: [PATCH 2/6] fix --- include/ylt/coro_rpc/impl/errno.h | 27 ++++++++++--------- include/ylt/struct_pack/error_code.hpp | 26 ++++++++++-------- .../examples/base_examples/client.cpp | 2 +- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/include/ylt/coro_rpc/impl/errno.h b/include/ylt/coro_rpc/impl/errno.h index dd033a47e..d785b30db 100644 --- a/include/ylt/coro_rpc/impl/errno.h +++ b/include/ylt/coro_rpc/impl/errno.h @@ -33,7 +33,7 @@ enum class errc : uint16_t { message_too_large, server_has_ran, }; -inline std::string_view make_error_message(errc ec) noexcept { +inline constexpr std::string_view make_error_message(errc ec) noexcept { switch (ec) { case errc::ok: return "ok"; @@ -66,24 +66,27 @@ inline std::string_view make_error_message(errc ec) noexcept { struct err_code { public: errc ec; - err_code() noexcept : ec(errc::ok) {} - explicit err_code(uint16_t ec) noexcept : ec{ec} {}; - err_code(errc ec) noexcept : ec(ec){}; - err_code& operator=(errc ec) noexcept { + constexpr err_code() noexcept : ec(errc::ok) {} + explicit constexpr err_code(uint16_t ec) noexcept : ec{ec} {}; + constexpr err_code(errc ec) noexcept : ec(ec){}; + constexpr err_code& operator=(errc ec) noexcept { this->ec = ec; return *this; } - err_code(const err_code& err_code) noexcept = default; - err_code& operator=(const err_code& o) noexcept = default; - operator errc() const noexcept { return ec; } - operator bool() const noexcept { return static_cast(ec); } - explicit operator uint16_t() const noexcept { + constexpr err_code(const err_code& err_code) noexcept = default; + constexpr err_code& operator=(const err_code& o) noexcept = default; + constexpr operator errc() const noexcept { return ec; } + constexpr operator bool() const noexcept { return static_cast(ec); } + constexpr explicit operator uint16_t() const noexcept { return static_cast(ec); } - uint16_t val() const noexcept { return static_cast(ec); } - std::string_view message() const noexcept { return make_error_message(ec); } + constexpr uint16_t val() const noexcept { return static_cast(ec); } + constexpr std::string_view message() const noexcept { + return make_error_message(ec); + } }; +inline bool operator!(err_code ec) noexcept { return ec == errc::ok; } inline bool operator!(errc ec) noexcept { return ec == errc::ok; } }; // namespace coro_rpc \ No newline at end of file diff --git a/include/ylt/struct_pack/error_code.hpp b/include/ylt/struct_pack/error_code.hpp index a75191333..eb573f402 100644 --- a/include/ylt/struct_pack/error_code.hpp +++ b/include/ylt/struct_pack/error_code.hpp @@ -26,7 +26,7 @@ enum class errc { invalid_width_of_container_length, }; namespace detail { -inline std::string_view make_error_message(errc ec) noexcept { +inline constexpr std::string_view make_error_message(errc ec) noexcept { switch (ec) { case errc::ok: return "ok"; @@ -47,19 +47,23 @@ inline std::string_view make_error_message(errc ec) noexcept { struct err_code { public: errc ec; - err_code() noexcept : ec(errc::ok) {} - err_code(errc ec) noexcept : ec(ec){}; - err_code& operator=(errc ec) noexcept { + constexpr err_code() noexcept : ec(errc::ok) {} + constexpr err_code(errc ec) noexcept : ec(ec){}; + constexpr err_code& operator=(errc ec) noexcept { this->ec = ec; return *this; } - err_code(const err_code& err_code) noexcept = default; - err_code& operator=(const err_code& o) noexcept = default; - bool operator==(const err_code& o) const noexcept { return ec == o.ec; } - bool operator!=(const err_code& o) const noexcept { return ec != o.ec; } - operator bool() const noexcept { return ec != errc::ok; } - int val() const noexcept { return static_cast(ec); } - std::string_view message() const noexcept { + constexpr err_code(const err_code& err_code) noexcept = default; + constexpr err_code& operator=(const err_code& o) noexcept = default; + constexpr bool operator==(const err_code& o) const noexcept { + return ec == o.ec; + } + constexpr bool operator!=(const err_code& o) const noexcept { + return ec != o.ec; + } + constexpr operator bool() const noexcept { return ec != errc::ok; } + constexpr int val() const noexcept { return static_cast(ec); } + constexpr std::string_view message() const noexcept { return detail::make_error_message(ec); } }; diff --git a/src/coro_rpc/examples/base_examples/client.cpp b/src/coro_rpc/examples/base_examples/client.cpp index efdf03833..8e1ff4482 100644 --- a/src/coro_rpc/examples/base_examples/client.cpp +++ b/src/coro_rpc/examples/base_examples/client.cpp @@ -61,7 +61,7 @@ Lazy show_rpc_call() { assert(ret.value() == "HelloService::hello_with_delay"s); ret = co_await client.call(); - assert(ret.error().code = 404); + assert(ret.error().code == 404); assert(ret.error().msg == "404 Not Found."); ret = co_await client.call(); From 298c08e80966cfa6f47b4a84cda7492ec2aa99d1 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Mon, 15 Jan 2024 20:33:41 +0800 Subject: [PATCH 3/6] fix --- src/coro_rpc/examples/base_examples/client.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/coro_rpc/examples/base_examples/client.cpp b/src/coro_rpc/examples/base_examples/client.cpp index 8e1ff4482..11ae6e72c 100644 --- a/src/coro_rpc/examples/base_examples/client.cpp +++ b/src/coro_rpc/examples/base_examples/client.cpp @@ -16,6 +16,8 @@ #include #include "rpc_service.h" +#include "ylt/coro_rpc/impl/errno.h" +#include "ylt/coro_rpc/impl/protocol/coro_rpc_protocol.hpp" using namespace coro_rpc; using namespace async_simple::coro; using namespace std::string_literals; @@ -61,7 +63,8 @@ Lazy show_rpc_call() { assert(ret.value() == "HelloService::hello_with_delay"s); ret = co_await client.call(); - assert(ret.error().code == 404); + + assert(ret.error().value() == 404); assert(ret.error().msg == "404 Not Found."); ret = co_await client.call(); From 85c814ede47d357ecd7c6f5b7d6be858bd377dac Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 16 Jan 2024 10:58:59 +0800 Subject: [PATCH 4/6] fix --- src/coro_rpc/examples/base_examples/client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coro_rpc/examples/base_examples/client.cpp b/src/coro_rpc/examples/base_examples/client.cpp index 11ae6e72c..9460a2b78 100644 --- a/src/coro_rpc/examples/base_examples/client.cpp +++ b/src/coro_rpc/examples/base_examples/client.cpp @@ -64,7 +64,7 @@ Lazy show_rpc_call() { ret = co_await client.call(); - assert(ret.error().value() == 404); + assert(ret.error().code == 404); assert(ret.error().msg == "404 Not Found."); ret = co_await client.call(); From d26df050282e5898f42d4deccf97430b5846c716 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 16 Jan 2024 11:57:41 +0800 Subject: [PATCH 5/6] simply code --- .../impl/protocol/struct_pack_protocol.hpp | 7 ++--- include/ylt/struct_pack/derived_helper.hpp | 1 - include/ylt/struct_pack/unpacker.hpp | 28 +++++++++---------- include/ylt/struct_pack/user_helper.hpp | 2 +- src/struct_pack/tests/test_compatible.cpp | 3 +- src/struct_pack/tests/test_serialize.cpp | 11 +++----- 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp b/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp index c7618323e..1f50752e9 100644 --- a/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp +++ b/include/ylt/coro_rpc/impl/protocol/struct_pack_protocol.hpp @@ -20,15 +20,12 @@ namespace coro_rpc::protocol { struct struct_pack_protocol { template static bool deserialize_to(T& t, std::string_view buffer) { - struct_pack::err_code ok{}; if constexpr (std::tuple_size_v == 1) { - ok = struct_pack::deserialize_to(std::get<0>(t), buffer); + return !struct_pack::deserialize_to(std::get<0>(t), buffer); } else { - ok = struct_pack::deserialize_to(t, buffer); + return !struct_pack::deserialize_to(t, buffer); } - - return !ok; } template static std::string serialize(const T& t) { diff --git a/include/ylt/struct_pack/derived_helper.hpp b/include/ylt/struct_pack/derived_helper.hpp index 22e1ea04f..8a78f06f9 100644 --- a/include/ylt/struct_pack/derived_helper.hpp +++ b/include/ylt/struct_pack/derived_helper.hpp @@ -102,7 +102,6 @@ struct deserialize_derived_class_helper { std::unique_ptr &base, unpack &unpacker) { if constexpr (index >= std::tuple_size_v) { unreachable(); - return struct_pack::err_code{}; } else { using derived_class = std::tuple_element_t; diff --git a/include/ylt/struct_pack/unpacker.hpp b/include/ylt/struct_pack/unpacker.hpp index 3efe7ca37..6319f6b03 100644 --- a/include/ylt/struct_pack/unpacker.hpp +++ b/include/ylt/struct_pack/unpacker.hpp @@ -107,7 +107,7 @@ class unpacker { data_len_ = reader_.tellg(); } auto &&[err_code, buffer_len] = deserialize_metainfo(); - if SP_UNLIKELY (err_code != struct_pack::err_code{}) { + if SP_UNLIKELY (err_code) { return err_code; } if constexpr (has_compatible) { @@ -168,7 +168,7 @@ class unpacker { } auto &&[err_code, buffer_len] = deserialize_metainfo(); len = buffer_len; - if SP_UNLIKELY (err_code != struct_pack::err_code{}) { + if SP_UNLIKELY (err_code) { return err_code; } if constexpr (has_compatible) { @@ -230,7 +230,7 @@ class unpacker { } auto &&[err_code, buffer_len] = deserialize_metainfo(); - if SP_UNLIKELY (err_code != struct_pack::err_code{}) { + if SP_UNLIKELY (err_code) { return err_code; } if constexpr (has_compatible) { @@ -621,7 +621,7 @@ class unpacker { deserialize_many(First &&first_item, Args &&...items) { auto code = deserialize_one(first_item); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } if constexpr (sizeof...(items) > 0) { @@ -867,7 +867,7 @@ class unpacker { else { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -970,7 +970,7 @@ class unpacker { item.clear(); for (uint64_t i = 0; i < size; ++i) { code = deserialize_one(value); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } if constexpr (NotSkip) { @@ -996,7 +996,7 @@ class unpacker { item.clear(); for (uint64_t i = 0; i < size; ++i) { code = deserialize_one(value); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } if constexpr (NotSkip) { @@ -1043,7 +1043,7 @@ class unpacker { else { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -1072,7 +1072,7 @@ class unpacker { for (size_t j = i; j < i + len; ++j) { code = deserialize_one( item[j]); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -1100,7 +1100,7 @@ class unpacker { item.emplace_back(); code = deserialize_one(item.back()); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { if constexpr (can_reserve) { if constexpr (can_shrink_to_fit) { item.shrink_to_fit(); // release reserve memory @@ -1114,7 +1114,7 @@ class unpacker { value_type useless; for (size_t i = 0; i < size; ++i) { code = deserialize_one(useless); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -1284,7 +1284,7 @@ class unpacker { else if constexpr (id == type_id::array_t) { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -1302,7 +1302,7 @@ class unpacker { if constexpr (NotSkip) { for (auto &e : item) { code = deserialize_one(e.second); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } @@ -1319,7 +1319,7 @@ class unpacker { if constexpr (NotSkip) { for (auto &i : item) { code = deserialize_one(i); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } diff --git a/include/ylt/struct_pack/user_helper.hpp b/include/ylt/struct_pack/user_helper.hpp index 5c13790d4..25f6cd29c 100644 --- a/include/ylt/struct_pack/user_helper.hpp +++ b/include/ylt/struct_pack/user_helper.hpp @@ -56,7 +56,7 @@ STRUCT_PACK_INLINE struct_pack::err_code read(Reader& reader, T* t, auto code = unpacker.template deserialize_one( t[i]); - if SP_UNLIKELY (code != struct_pack::err_code{}) { + if SP_UNLIKELY (code) { return code; } } diff --git a/src/struct_pack/tests/test_compatible.cpp b/src/struct_pack/tests/test_compatible.cpp index eb759ad9c..59ae1abe9 100644 --- a/src/struct_pack/tests/test_compatible.cpp +++ b/src/struct_pack/tests/test_compatible.cpp @@ -158,8 +158,7 @@ TEST_CASE("test compatible") { serialize_to(buffer.data(), size2, p); person1 p2; - CHECK(deserialize_to(p2, buffer.data(), buffer.size()) == - struct_pack::errc{}); + CHECK(!deserialize_to(p2, buffer.data(), buffer.size())); CHECK((p2.age == p.age && p2.name == p.name)); } SUBCASE("serialize person 2 person1") { diff --git a/src/struct_pack/tests/test_serialize.cpp b/src/struct_pack/tests/test_serialize.cpp index 208de7b8f..d98e5dc59 100644 --- a/src/struct_pack/tests/test_serialize.cpp +++ b/src/struct_pack/tests/test_serialize.cpp @@ -523,7 +523,7 @@ TEST_CASE("testing deserialization") { auto ret = serialize(p); person p1{}; - CHECK(deserialize_to(p1, ret.data(), ret.size()) == struct_pack::errc{}); + CHECK(!deserialize_to(p1, ret.data(), ret.size())); } TEST_CASE("testing deserialization with invalid data") { @@ -865,8 +865,7 @@ TEST_CASE("test free functions") { CHECK(!buffer.empty()); person p1{}; - CHECK(deserialize_to(p1, buffer.data(), buffer.size()) == - struct_pack::errc{}); + CHECK(!deserialize_to(p1, buffer.data(), buffer.size())); std::optional op1{}; auto buf1 = serialize(op1); @@ -962,8 +961,7 @@ TEST_CASE("test serialize offset") { buffer.resize(info.size() + offset); serialize_to(buffer.data() + offset, info, p); person p2; - CHECK(deserialize_to(p2, buffer.data() + offset, info.size()) == - struct_pack::errc{}); + CHECK(!deserialize_to(p2, buffer.data() + offset, info.size())); CHECK(p2 == p); std::vector buffer2; @@ -973,8 +971,7 @@ TEST_CASE("test serialize offset") { serialize_to_with_offset(buffer2, offset, p); CHECK(data_offset + info.size() == buffer2.size()); person p3; - CHECK(deserialize_to(p3, buffer2.data() + data_offset, info.size()) == - struct_pack::errc{}); + CHECK(!deserialize_to(p3, buffer2.data() + data_offset, info.size())); CHECK(p3 == p); } From b7a793d41cd3a70ff34a831e2a688083b9274fc5 Mon Sep 17 00:00:00 2001 From: ZezhengLi Date: Wed, 17 Jan 2024 15:57:15 +0800 Subject: [PATCH 6/6] fix --- include/ylt/struct_pack/error_code.hpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/include/ylt/struct_pack/error_code.hpp b/include/ylt/struct_pack/error_code.hpp index eb573f402..8fd9acadc 100644 --- a/include/ylt/struct_pack/error_code.hpp +++ b/include/ylt/struct_pack/error_code.hpp @@ -55,12 +55,7 @@ struct err_code { } constexpr err_code(const err_code& err_code) noexcept = default; constexpr err_code& operator=(const err_code& o) noexcept = default; - constexpr bool operator==(const err_code& o) const noexcept { - return ec == o.ec; - } - constexpr bool operator!=(const err_code& o) const noexcept { - return ec != o.ec; - } + constexpr operator errc() const noexcept { return ec; } constexpr operator bool() const noexcept { return ec != errc::ok; } constexpr int val() const noexcept { return static_cast(ec); } constexpr std::string_view message() const noexcept {