Commit e8c71ac7 authored by Kenton Varda's avatar Kenton Varda

Fix Maybe::orDefault() on move-only types.

parent d2ec1420
......@@ -143,6 +143,13 @@ TEST(Common, Maybe) {
EXPECT_EQ(456, m.orDefault(456));
}
{
// Verify orDefault() works with move-only types.
Maybe<kj::String> m = nullptr;
kj::String s = kj::mv(m).orDefault(kj::str("foo"));
EXPECT_EQ("foo", s);
}
{
Maybe<int> m = &i;
EXPECT_FALSE(m == nullptr);
......
......@@ -1089,20 +1089,34 @@ public:
inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; }
inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; }
T& orDefault(T& defaultValue) {
T& orDefault(T& defaultValue) & {
if (ptr == nullptr) {
return defaultValue;
} else {
return *ptr;
}
}
const T& orDefault(const T& defaultValue) const {
const T& orDefault(const T& defaultValue) const & {
if (ptr == nullptr) {
return defaultValue;
} else {
return *ptr;
}
}
T&& orDefault(T&& defaultValue) && {
if (ptr == nullptr) {
return kj::mv(defaultValue);
} else {
return kj::mv(*ptr);
}
}
const T&& orDefault(const T&& defaultValue) const && {
if (ptr == nullptr) {
return kj::mv(defaultValue);
} else {
return kj::mv(*ptr);
}
}
template <typename Func>
auto map(Func&& f) & -> Maybe<decltype(f(instance<T&>()))> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment