Skip to content

Commit

Permalink
update real_read
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed May 13, 2024
1 parent da6f3dd commit 73eaa60
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions zedio/io/impl/impl_buf_read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ImplBufRead {
if (!ret) [[unlikely]] {
co_return std::unexpected{ret.error()};
}
static_cast<B *>(this)->r_stream_.w_increase(ret.value());
co_return Result<void>{};
}

Expand Down Expand Up @@ -100,24 +101,18 @@ class ImplBufRead {
template <bool eof_is_error, class Pred>
requires std::is_invocable_v<Pred>
auto real_read(std::span<char> buf, Pred pred) -> zedio::async::Task<Result<std::size_t>> {
if (static_cast<B *>(this)->r_stream_.capacity() < buf.size_bytes()) {
auto len = static_cast<B *>(this)->r_stream_.write_to(buf);
buf = buf.subspan(len, buf.size_bytes() - len);
co_return co_await static_cast<B *>(this)->io_.read(buf);
}

if (pred()) {
co_return static_cast<B *>(this)->r_stream_.write_to(buf);
}

if (static_cast<B *>(this)->r_stream_.w_remaining() < buf.size_bytes()) {
static_cast<B *>(this)->r_stream_.reset_data();
}

assert(static_cast<B *>(this)->r_stream_.w_remaining() >= buf.size_bytes());

Result<std::size_t> ret;
while (true) {
if (static_cast<B *>(this)->r_stream_.r_remaining()
+ static_cast<B *>(this)->r_stream_.w_remaining()
< buf.size_bytes()) {
static_cast<B *>(this)->r_stream_.reset_data();
}

ret = co_await static_cast<B *>(this)->io_.read(
static_cast<B *>(this)->r_stream_.w_slice());
if (!ret) [[unlikely]] {
Expand Down

0 comments on commit 73eaa60

Please sign in to comment.