Use C++23 std::expected (#8823)

This commit is contained in:
Gold856
2026-07-02 02:10:52 -04:00
committed by GitHub
parent 1299abc173
commit 4e7dd4cfbb
37 changed files with 133 additions and 2709 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@
#include <climits>
#include <concepts>
#include <expected>
#include <ranges>
#include <string>
#include <string_view>
@@ -27,7 +28,6 @@
#include <vector>
#include "wpi/util/StringMap.hpp"
#include "wpi/util/expected"
namespace wpi::util {
@@ -153,7 +153,7 @@ class json
};
public:
static wpi::util::expected<json, const char*> parse(std::string_view);
static std::expected<json, const char*> parse(std::string_view);
static json parse_or_throw(std::string_view);
json(const json&);

View File

@@ -1435,7 +1435,7 @@ json::parse(json& j, const char*& p, const char* e, int context, int depth)
return unexpected_eof;
}
wpi::util::expected<json, const char*>
std::expected<json, const char*>
json::parse(std::string_view s)
{
const char* p = s.data();
@@ -1443,12 +1443,12 @@ json::parse(std::string_view s)
json j;
Status result = parse(j, p, e, 0, DEPTH);
if (result != success) {
return wpi::util::unexpected(StatusToString(result));
return std::unexpected(StatusToString(result));
}
json j2;
Status s2 = parse(j2, p, e, 0, DEPTH);
if (s2 != absent_value) {
return wpi::util::unexpected(StatusToString(trailing_content));
return std::unexpected(StatusToString(trailing_content));
}
return j;
}

View File

@@ -259,12 +259,12 @@ static std::unique_ptr<WritableMemoryBuffer> GetMemoryBufferForStream(
return GetMemBufferCopyImpl(buffer, bufferName, ec);
}
wpi::util::expected<std::unique_ptr<MemoryBuffer>, std::error_code>
std::expected<std::unique_ptr<MemoryBuffer>, std::error_code>
MemoryBuffer::GetFile(std::string_view filename, int64_t fileSize) {
std::error_code ec;
auto ret = GetFileAux<MemoryBuffer>(filename, ec, fileSize, fileSize, 0);
if (ec) {
return wpi::util::unexpected{ec};
return std::unexpected{ec};
}
return ret;
}

View File

@@ -24,7 +24,7 @@
#include <span>
#include <string_view>
#include <system_error>
#include "wpi/util/expected"
#include <expected>
// Duplicated from fs.h to avoid a dependency
namespace fs {
@@ -78,7 +78,7 @@ class MemoryBuffer {
/// if successful, otherwise returning null. If FileSize is specified, this
/// means that the client knows that the file exists and that it has the
/// specified size.
static wpi::util::expected<std::unique_ptr<MemoryBuffer>, std::error_code>
static std::expected<std::unique_ptr<MemoryBuffer>, std::error_code>
GetFile(std::string_view filename, int64_t fileSize = -1);
/// Read all of the specified file into a MemoryBuffer as a stream

View File

@@ -1,60 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpi/util/expected"
#include <initializer_list>
#include <tuple>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
namespace {
struct TakesInitAndVariadic {
std::vector<int> v;
std::tuple<int, int> t;
template <class... Args>
TakesInitAndVariadic(std::initializer_list<int> l, // NOLINT
Args&&... args)
: v(l), t(std::forward<Args>(args)...) {}
};
} // namespace
TEST(ExpectedTest, Emplace) {
{
wpi::util::expected<std::unique_ptr<int>, int> e;
e.emplace(new int{42});
EXPECT_TRUE(e);
EXPECT_EQ(**e, 42);
}
{
wpi::util::expected<std::vector<int>, int> e;
e.emplace({0, 1});
EXPECT_TRUE(e);
EXPECT_EQ((*e)[0], 0);
EXPECT_EQ((*e)[1], 1);
}
{
wpi::util::expected<std::tuple<int, int>, int> e;
e.emplace(2, 3);
EXPECT_TRUE(e);
EXPECT_EQ(std::get<0>(*e), 2);
EXPECT_EQ(std::get<1>(*e), 3);
}
{
wpi::util::expected<TakesInitAndVariadic, int> e =
wpi::util::make_unexpected(0);
e.emplace({0, 1}, 2, 3);
EXPECT_TRUE(e);
EXPECT_EQ(e->v[0], 0);
EXPECT_EQ(e->v[1], 1);
EXPECT_EQ(std::get<0>(e->t), 2);
EXPECT_EQ(std::get<1>(e->t), 3);
}
}