From 85a18174868e40202a0280bc26b64f3b39f84b19 Mon Sep 17 00:00:00 2001 From: Amber Ehrlich Date: Thu, 5 Oct 2023 12:24:20 -0400 Subject: [PATCH] javascript wins this one --- include/dpp/snowflake.h | 21 ++++++++++++--------- src/dpp/snowflake.cpp | 7 ------- src/unittest/test.cpp | 4 ++-- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/dpp/snowflake.h b/include/dpp/snowflake.h index 1f947ed73d..a68d4cc009 100644 --- a/include/dpp/snowflake.h +++ b/include/dpp/snowflake.h @@ -108,7 +108,7 @@ class DPP_EXPORT snowflake final { */ template >> snowflake(const T &string_value) noexcept : snowflake(std::string_view{string_value}) {} - /* ^ this exists to preserve `message_cache.find(std::get(event.get_parameter("message_id")));`*/ + /* ^ this exists to preserve `message_cache.find(std::get(event.get_parameter("message_id")));` */ /** * @brief Copy value from another snowflake @@ -124,14 +124,6 @@ class DPP_EXPORT snowflake final { */ constexpr dpp::snowflake &operator=(dpp::snowflake&& rhs) noexcept = default; - /** - * @brief Assign value converted from a string to the snowflake - * - * On invalid string the value will be 0 - * @param snowflake_val snowflake value as a string - */ - dpp::snowflake &operator=(std::string_view snowflake_val) noexcept; - /** * @brief Assign integer value to the snowflake * @@ -143,6 +135,17 @@ class DPP_EXPORT snowflake final { return *this = dpp::snowflake{snowflake_val}; } + /** + * @brief Assign value converted from a string to the snowflake + * + * On invalid string the value will be 0 + * @param snowflake_val snowflake value as a string + */ + template >> + constexpr dpp::snowflake &operator=(T&& snowflake_val) noexcept { + return *this = dpp::snowflake{std::forward(snowflake_val)}; + } + /** * @brief Returns true if the snowflake holds an empty value (is 0) * diff --git a/src/dpp/snowflake.cpp b/src/dpp/snowflake.cpp index 2606375da5..72d566756c 100644 --- a/src/dpp/snowflake.cpp +++ b/src/dpp/snowflake.cpp @@ -31,13 +31,6 @@ snowflake::snowflake(std::string_view string_value) noexcept { value = 0; } -snowflake& snowflake::operator=(std::string_view string_value) noexcept { - auto [_, err] = std::from_chars(string_value.data(), string_value.data() + string_value.size(), value); - if (err != std::errc{}) - value = 0; - return *this; -} - bool snowflake::operator==(std::string_view snowflake_val) const noexcept { uint64_t v; auto [end, err] = std::from_chars(snowflake_val.data(), snowflake_val.data() + snowflake_val.size(), v); diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index be46b6cb68..84442f5e77 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -300,10 +300,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b s = "69420"; success = success && s == 69420; auto conversion_test = [](dpp::snowflake sl) { - return sl; + return sl.str(); }; s = conversion_test(std::string{"1337"}); - success = success && s == 1337; + success = success && s == 1337; /* THIS BREAKS (and i do not care very much): && s == conversion_test(dpp::snowflake{"1337"}); */ success = success && dpp::snowflake{0} == 0; set_test(SNOWFLAKE, success); }