Replace unstable coral::optional functions with operators
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2023-02-20 15:30:00 +00:00
parent c36bf5b76c
commit 685e09412b
1 changed files with 6 additions and 16 deletions

View File

@ -283,7 +283,7 @@ export namespace coral {
optional(optional const & that) : buffer{0} {
if (that.has_value()) {
(*reinterpret_cast<element *>(this->buffer)) = that.value();
(*reinterpret_cast<element *>(this->buffer)) = *that;
this->buffer[sizeof(element)] = 1;
} else {
this->buffer[sizeof(element)] = 0;
@ -303,7 +303,7 @@ export namespace coral {
* If the optional is empty, an empty optional will always be returned.
*/
template<typename functor> std::invoke_result_t<functor, element> map(functor const & apply) const {
if (this->has_value()) return apply(this->value());
if (this->has_value()) return apply(**this);
return {};
}
@ -315,25 +315,15 @@ export namespace coral {
return this->has_value() ? *reinterpret_cast<element const *>(this->buffer) : fallback;
}
/**
* Returns a reference to the contained value.
*
* *Note*: attempting to access the value of an empty optional will trigger safety-checked
* behavior.
*/
element & value() {
element & operator *() {
if (!this->has_value()) unreachable();
return *reinterpret_cast<element *>(this->buffer);
}
/**
* Returns the contained value.
*
* *Note*: attempting to access the value of an empty optional will trigger safety-checked
* behavior.
*/
element const & value() const {
element const & operator *() const {
if (!this->has_value()) unreachable();
return *reinterpret_cast<element const *>(this->buffer);
}