Result: Avoid double-checking when calling through to the underlying Option
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-30 15:00:02 +01:00
parent 8e59a0a79f
commit 400d0395a2
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 30 additions and 4 deletions

12
luna/include/luna/Badge.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
template <class T> struct Badge
{
private:
constexpr Badge() = default;
Badge(const Badge<T>&) = delete;
Badge(Badge<T>&&) = delete;
friend T;
};

View File

@ -1,8 +1,11 @@
#pragma once
#include <luna/Badge.h>
#include <luna/Check.h>
#include <luna/Move.h>
#include <luna/PlacementNew.h>
template <typename T> class Result;
template <typename T> class Option
{
public:
@ -42,6 +45,11 @@ template <typename T> class Option
return m_storage.fetch_reference();
}
T unchecked_value(Badge<Result<T>>) const
{
return m_storage.fetch_reference();
}
T release_value()
{
expect(has_value(), "Option::release_value called on an empty Option");
@ -49,6 +57,12 @@ template <typename T> class Option
return move(m_storage.fetch_reference());
}
T unchecked_release_value(Badge<Result<T>>)
{
m_has_value = false;
return move(m_storage.fetch_reference());
}
T value_or(const T& other) const
{
if (has_value()) return m_storage.fetch_reference();

View File

@ -72,13 +72,13 @@ template <typename T> class Result
T value() const
{
expect(has_value(), "Result::value() called on a Result that holds an error");
return m_value.value();
return m_value.unchecked_value({});
}
T expect_value(const char* reason) const
{
expect(has_value(), reason);
return m_value.value();
return m_value.unchecked_value({});
}
T value_or(const T& other) const
@ -101,13 +101,13 @@ template <typename T> class Result
T release_value()
{
expect(has_value(), "Result::release_value() called on a Result that holds an error");
return m_value.release_value();
return m_value.unchecked_release_value({});
}
T expect_release_value(const char* reason)
{
expect(has_value(), reason);
return m_value.release_value();
return m_value.unchecked_release_value({});
}
private: