Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async std{in/out/err} #15

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/stdio_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "zedio/core.hpp"
#include "zedio/io/stdio.hpp"
#include "zedio/log.hpp"

#include <string_view>
#include <vector>

using namespace zedio::async;
using namespace zedio::log;
using namespace zedio;

auto test() -> Task<void> {
for (auto i = 0; i < 3; i++) {
std::vector<char> buf(64);

auto res = co_await io::input(buf);
if (!res) {
console.info("EOF");
co_return;
}
console.info("read from stdin: {}", std::string_view{buf.data(), buf.size()});
co_await io::print("████████████████████\n");
co_await io::eprint("▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n");
}
co_return;
}

auto main() -> int {
SET_LOG_LEVEL(LogLevel::Debug);
auto runtime = Runtime::options().scheduler().set_num_workers(1).build();
runtime.block_on(test());
}
70 changes: 70 additions & 0 deletions zedio/io/stdio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include "zedio/common/debug.hpp"
#include "zedio/common/error.hpp"
#include "zedio/io/io.hpp"

// Linux
#include <unistd.h>

namespace zedio::io {

namespace detail {

template <int FD>
[[REMEMBER_CO_AWAIT]]
auto async_read(std::span<char> buf) noexcept -> async::Task<Result<void>> {
auto ret = co_await io::read(FD,
buf.data(),
static_cast<unsigned int>(buf.size_bytes()),
static_cast<std::size_t>(-1));
if (!ret) [[unlikely]] {
co_return std::unexpected{ret.error()};
}
if (ret.value() == 0) [[unlikely]] {
co_return std::unexpected{make_zedio_error(Error::UnexpectedEOF)};
}
co_return Result<void>{};
}

template <int FD>
[[REMEMBER_CO_AWAIT]]
auto async_write(std::span<const char> buf) noexcept -> async::Task<Result<void>> {
Result<std::size_t> ret{0uz};
while (!buf.empty()) {
ret = co_await io::write(FD,
buf.data(),
static_cast<unsigned int>(buf.size_bytes()),
static_cast<std::size_t>(-1));
if (!ret) [[unlikely]] {
co_return std::unexpected{ret.error()};
}
if (ret.value() == 0) [[unlikely]] {
co_return std::unexpected{make_zedio_error(Error::WriteZero)};
}
buf = buf.subspan(ret.value(), buf.size_bytes() - ret.value());
}
co_return Result<void>{};
}
} // namespace detail

[[REMEMBER_CO_AWAIT]]
static inline auto input(std::span<char> buf) -> async::Task<Result<void>> {
co_return co_await detail::async_read<STDIN_FILENO>(buf);
}

template <typename... Args>
[[REMEMBER_CO_AWAIT]]
static inline auto print(std::string_view fmt, Args &&...args) -> async::Task<Result<void>> {
co_return co_await detail::async_write<STDOUT_FILENO>(
std::vformat(fmt, std::make_format_args(std::forward<Args>(args)...)));
}

template <typename... Args>
[[REMEMBER_CO_AWAIT]]
static inline auto eprint(std::string_view fmt, Args &&...args) -> async::Task<Result<void>> {
co_return co_await detail::async_write<STDERR_FILENO>(
std::vformat(fmt, std::make_format_args(std::forward<Args>(args)...)));
}

} // namespace zedio::io