[wpiutil] Return wpi::expected from MemoryBuffer::GetFile (#7069)

This commit is contained in:
Joseph Eng
2024-09-12 23:45:35 -07:00
committed by GitHub
parent d44b651558
commit 1f3ef019ce
15 changed files with 87 additions and 103 deletions

View File

@@ -259,10 +259,14 @@ static std::unique_ptr<WritableMemoryBuffer> GetMemoryBufferForStream(
return GetMemBufferCopyImpl(buffer, bufferName, ec);
}
std::unique_ptr<MemoryBuffer> MemoryBuffer::GetFile(std::string_view filename,
std::error_code& ec,
int64_t fileSize) {
return GetFileAux<MemoryBuffer>(filename, ec, fileSize, fileSize, 0);
wpi::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::unexpected{ec};
}
return ret;
}
template <typename MB>

View File

@@ -24,6 +24,7 @@
#include <span>
#include <string_view>
#include <system_error>
#include <wpi/expected>
// Duplicated from fs.h to avoid a dependency
namespace fs {
@@ -77,9 +78,8 @@ 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 std::unique_ptr<MemoryBuffer> GetFile(std::string_view filename,
std::error_code& ec,
int64_t fileSize = -1);
static wpi::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
/// (i.e. until EOF reached). This is useful for special files that