Skip to content

Commit

Permalink
Replace fmt::error_code to std::error_code
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus authored and vitaut committed May 9, 2021
1 parent 2165bef commit 2a9b314
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 28 deletions.
13 changes: 1 addition & 12 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,6 @@ template <typename Char> class basic_cstring_view {
using cstring_view = basic_cstring_view<char>;
using wcstring_view = basic_cstring_view<wchar_t>;

// An error code.
class error_code {
private:
int value_;

public:
explicit error_code(int value = 0) FMT_NOEXCEPT : value_(value) {}

int get() const FMT_NOEXCEPT { return value_; }
};

template <typename Char> struct formatter<std::error_code, Char> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
Expand Down Expand Up @@ -351,7 +340,7 @@ class file {

// Makes fd be the copy of this file descriptor, closing fd first if
// necessary.
FMT_API void dup2(int fd, error_code& ec) FMT_NOEXCEPT;
FMT_API void dup2(int fd, std::error_code& ec) FMT_NOEXCEPT;

// Creates a pipe setting up read_end and write_end file objects for reading
// and writing respectively.
Expand Down
4 changes: 2 additions & 2 deletions src/os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ void file::dup2(int fd) {
}
}

void file::dup2(int fd, error_code& ec) FMT_NOEXCEPT {
void file::dup2(int fd, std::error_code& ec) FMT_NOEXCEPT {
int result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd)));
if (result == -1) ec = error_code(errno);
if (result == -1) ec = std::error_code(errno, std::generic_category());
}

void file::pipe(file& read_end, file& write_end) {
Expand Down
1 change: 0 additions & 1 deletion test/gtest-extra-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ TEST(gtest_extra_test, expect_system_error_streaming) {
#if FMT_USE_FCNTL

using fmt::buffered_file;
using fmt::error_code;
using fmt::file;

TEST(output_redirect_test, scoped_redirect) {
Expand Down
14 changes: 4 additions & 10 deletions test/os-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#endif

using fmt::buffered_file;
using fmt::error_code;
using testing::HasSubstr;

#ifdef _WIN32
Expand Down Expand Up @@ -69,11 +68,6 @@ TEST(util_test, utf16_to_utf8_convert) {
u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u)));
}

TEST(os_test, error_code) {
EXPECT_EQ(error_code().get(), 0);
EXPECT_EQ(error_code(42).get(), 42);
}

TEST(os_test, format_std_error_code) {
EXPECT_EQ("generic:42",
fmt::format(FMT_STRING("{0}"),
Expand Down Expand Up @@ -527,18 +521,18 @@ TEST(file_test, dup2_error) {
TEST(file_test, dup2_noexcept) {
file f = open_file();
file copy = open_file();
error_code ec;
std::error_code ec;
f.dup2(copy.descriptor(), ec);
EXPECT_EQ(ec.get(), 0);
EXPECT_EQ(ec.value(), 0);
EXPECT_NE(f.descriptor(), copy.descriptor());
EXPECT_READ(copy, file_content);
}

TEST(file_test, dup2_noexcept_error) {
file f = open_file();
error_code ec;
std::error_code ec;
SUPPRESS_ASSERT(f.dup2(-1, ec));
EXPECT_EQ(EBADF, ec.get());
EXPECT_EQ(EBADF, ec.value());
}

TEST(file_test, pipe) {
Expand Down
5 changes: 2 additions & 3 deletions test/posix-mock-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "util.h"

using fmt::buffered_file;
using fmt::error_code;

using testing::_;
using testing::Return;
Expand Down Expand Up @@ -367,13 +366,13 @@ TEST(file_test, dup2_retry) {
TEST(file_test, dup2_no_except_retry) {
int stdout_fd = FMT_POSIX(fileno(stdout));
file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
error_code ec;
std::error_code ec;
dup2_count = 1;
f1.dup2(f2.descriptor(), ec);
# ifndef _WIN32
EXPECT_EQ(4, dup2_count);
# else
EXPECT_EQ(EINTR, ec.get());
EXPECT_EQ(EINTR, ec.value());
# endif
dup2_count = 0;
}
Expand Down

0 comments on commit 2a9b314

Please sign in to comment.