aboutsummaryrefslogtreecommitdiffstats
path: root/nihil/read_file.ccm
diff options
context:
space:
mode:
Diffstat (limited to 'nihil/read_file.ccm')
-rw-r--r--nihil/read_file.ccm33
1 files changed, 14 insertions, 19 deletions
diff --git a/nihil/read_file.ccm b/nihil/read_file.ccm
index 5f332fd..481bf70 100644
--- a/nihil/read_file.ccm
+++ b/nihil/read_file.ccm
@@ -18,33 +18,28 @@ export module nihil:read_file;
import :error;
import :fd;
+import :monad;
import :open_file;
namespace nihil {
/*
- * Load the contents of a file into an output iterator.
+ * Read the contents of a file into an output iterator.
*/
-export auto read_file(std::filesystem::path const &filename,
- std::output_iterator<char> auto &&iter)
+export [[nodiscard]] auto
+read_file(std::filesystem::path const &filename,
+ std::output_iterator<char> auto &&iter)
-> std::expected<void, error>
{
- auto do_write = [&](fd &&file) -> std::expected<void, error>
- {
- auto constexpr bufsize = std::size_t{1024};
- auto buffer = std::array<char, bufsize>{};
-
- for (;;) {
- auto err = read(file, buffer);
- if (!err)
- return std::unexpected(err.error());
-
- auto data = std::span(buffer).subspan(0, *err);
- std::ranges::copy(data, iter);
- }
- };
-
- return open_file(filename, O_RDONLY).and_then(do_write);
+ auto file = co_await open_file(filename, O_RDONLY);
+
+ auto constexpr bufsize = std::size_t{1024};
+ auto buffer = std::array<char, bufsize>{};
+
+ for (;;) {
+ auto read_buf = co_await(read(file, buffer));
+ std::ranges::copy(read_buf, iter);
+ }
}
} // namespace nihil