1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:47:45 +00:00

LibCrypto: Use the new return-from-Variant::visit() mechanism

And simplify the code _even further_!
This commit is contained in:
Ali Mohammad Pur 2021-05-20 00:32:42 +04:30 committed by Andreas Kling
parent cdb0a08946
commit c94440860e

View file

@ -61,20 +61,16 @@ struct MultiHashDigestVariant {
const u8* immutable_data() const const u8* immutable_data() const
{ {
const u8* data = nullptr; return m_digest.visit(
m_digest.visit( [&](const Empty&) -> const u8* { VERIFY_NOT_REACHED(); },
[&](const Empty&) { VERIFY_NOT_REACHED(); }, [&](const auto& value) { return value.immutable_data(); });
[&](const auto& value) { data = value.immutable_data(); });
return data;
} }
size_t data_length() size_t data_length()
{ {
size_t length = 0; return m_digest.visit(
m_digest.visit( [&](const Empty&) -> size_t { VERIFY_NOT_REACHED(); },
[&](const Empty&) { VERIFY_NOT_REACHED(); }, [&](const auto& value) { return value.data_length(); });
[&](const auto& value) { length = value.data_length(); });
return length;
} }
using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>; using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
@ -109,20 +105,16 @@ public:
inline size_t digest_size() const inline size_t digest_size() const
{ {
size_t result = 0; return m_algorithm.visit(
m_algorithm.visit( [&](const Empty&) -> size_t { return 0; },
[&](const Empty&) {}, [&](const auto& hash) { return hash.digest_size(); });
[&](const auto& hash) { result = hash.digest_size(); });
return result;
} }
inline size_t block_size() const inline size_t block_size() const
{ {
size_t result = 0; return m_algorithm.visit(
m_algorithm.visit( [&](const Empty&) -> size_t { return 0; },
[&](const Empty&) {}, [&](const auto& hash) { return hash.block_size(); });
[&](const auto& hash) { result = hash.block_size(); });
return result;
} }
inline void initialize(HashKind kind) inline void initialize(HashKind kind)
@ -172,11 +164,9 @@ public:
virtual DigestType peek() override virtual DigestType peek() override
{ {
DigestType result = Empty {}; return m_algorithm.visit(
m_algorithm.visit( [&](Empty&) -> DigestType { VERIFY_NOT_REACHED(); },
[&](Empty&) { VERIFY_NOT_REACHED(); }, [&](auto& hash) -> DigestType { return hash.peek(); });
[&](auto& hash) { result = hash.peek(); });
return result;
} }
virtual DigestType digest() override virtual DigestType digest() override
@ -196,11 +186,9 @@ public:
virtual String class_name() const override virtual String class_name() const override
{ {
String result; return m_algorithm.visit(
m_algorithm.visit( [&](const Empty&) -> String { return "UninitializedHashManager"; },
[&](const Empty&) { result = "UninitializedHashManager"; }, [&](const auto& hash) { return hash.class_name(); });
[&](const auto& hash) { result = hash.class_name(); });
return result;
} }
inline bool is(HashKind kind) const inline bool is(HashKind kind) const