From 2a9b3146277f4ad7c481a4eee990d651daeb7888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=A9=D0=B0=D0=BF=D0=BE=D0=B2?= Date: Sun, 9 May 2021 18:49:45 +0500 Subject: [PATCH] Replace fmt::error_code to std::error_code --- include/fmt/os.h | 13 +------------ src/os.cc | 4 ++-- test/gtest-extra-test.cc | 1 - test/os-test.cc | 14 ++++---------- test/posix-mock-test.cc | 5 ++--- 5 files changed, 9 insertions(+), 28 deletions(-) diff --git a/include/fmt/os.h b/include/fmt/os.h index cd40c031f822..a679de09a8f9 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -118,17 +118,6 @@ template class basic_cstring_view { using cstring_view = basic_cstring_view; using wcstring_view = basic_cstring_view; -// 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 struct formatter { template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { @@ -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. diff --git a/src/os.cc b/src/os.cc index 68d6c6c7f3ef..3fd043c7c0f1 100644 --- a/src/os.cc +++ b/src/os.cc @@ -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) { diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 5a3695d0fcd2..0d86206c9337 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -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) { diff --git a/test/os-test.cc b/test/os-test.cc index c7e450211c26..8cf3c5e86a04 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -19,7 +19,6 @@ #endif using fmt::buffered_file; -using fmt::error_code; using testing::HasSubstr; #ifdef _WIN32 @@ -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}"), @@ -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) { diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index ec75887c5840..f669f13a5549 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -30,7 +30,6 @@ #include "util.h" using fmt::buffered_file; -using fmt::error_code; using testing::_; using testing::Return; @@ -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; }