Iros
 
Loading...
Searching...
No Matches
read_all.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace di::io {
9namespace detail {
10 struct ReadAll {
11 constexpr auto operator()(Impl<Reader> auto& reader) const -> Result<Vector<Byte>> {
12 constexpr auto block_size = 16384ZU;
13
14 auto buffer = Vector<Byte> {};
15
16 auto try_reserve_more_capacity = [&] {
17 return invoke_as_fallible([&] {
18 buffer.reserve(buffer.capacity() + block_size);
19 return;
20 });
21 };
22
23 for (;;) {
24 auto nread = DI_TRY(try_reserve_more_capacity() >> [&] {
25 return read_some(reader, { buffer.end(), buffer.begin() + buffer.capacity() });
26 } | if_success([&](auto nread) {
27 buffer.assume_size(buffer.size() + nread);
28 }));
29 if (nread == 0) {
30 break;
31 }
32 }
33
34 return buffer;
35 }
36 };
37}
38
39constexpr inline auto read_all = detail::ReadAll {};
40}
41
42namespace di {
43using io::read_all;
44}
Definition vector_forward_declaration.h:8
#define DI_TRY(...)
Definition monad_try.h:13
Definition reader.h:8
Definition reader.h:7
constexpr auto read_all
Definition read_all.h:39
constexpr auto read_some
Definition reader.h:32
Expected< T, Error > Result
Definition result.h:8
Definition zstring_parser.h:9
constexpr auto invoke_as_fallible
Definition invoke_as_fallible.h:37
constexpr auto if_success
Definition if_success.h:31
Definition read_all.h:10
constexpr auto operator()(Impl< Reader > auto &reader) const -> Result< Vector< Byte > >
Definition read_all.h:11