1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! `qread` adds shortcut functionality to anything that implements the [`std::io::Read`]
//! or [`std::io::BufRead`] traits to make certain read operations more ergonomic by handling
//! allocation of a buffer for you. Specifically, `Read` implementors get two new methods:
//!
//! * [`read_all_to_bytes`](QRead::read_all_to_bytes), which calls `.read_to_end()` on the object and returns the vector of
//! bytes gotten from reading the object.
//! * [`read_all_to_string`](QRead::read_all_to_string), which calls `.read_to_string()` on the object and returns the string
//! created.
//!
//! Likewise, `ReadBuf` implementors also get two new methods:
//!
//! * [`read_until_to_bytes`](QBufRead::read_until_to_bytes`), which calls `.read_until()` on the object and returns the vector of
//! bytes gotten from the object.
//! * [`read_line_to_string`](QBufRead::read_line_to_string) which calls `.read_line()` on the object and returns the line as a
//! string (note that it will include the newline).
//!
//!
//! # Examples
//!
//! Using [`read_all_to_bytes`](QRead::read_all_to_bytes) and [`read_all_to_string`](QRead::read_all_to_string) to read the full
//! contents of a file as a vector of bytes or a string, respectively, is very straightforward. Note that these are both members
//! of the [`QRead`] trait, so that trait must be brought into scope to use these methods.
//!
//! ```no_run
//! use std::fs::File;
//! use qread::QRead;
//!
//! // This assumes that we have a file `demo.bin` in the current directory
//! let mut f1 = File::open("demo.bin").unwrap();
//! let data = f1.read_all_to_bytes().unwrap();
//!
//! // This assumes that we have a file `demo.txt` in the current directory
//! let mut f2 = File::open("demo.txt").unwrap();
//! let text = f2.read_all_to_string().unwrap();
//! ```
//!
//!
//! Likewise, the [`read_until_to_bytes`](QBufRead::read_until_to_bytes`) and [`read_line_to_string`](QBufRead::read_line_to_string)
//! methods are also straightforward, provided that the [`QBufRead`] trait is in scope.
//!
//! ```no_run
//! use std::fs::File;
//! use std::io::BufReader;
//! use qread::QBufRead;
//!
//! // This assumes that we have a file `demo.bin` in the current directory
//! let mut f1 = BufReader::new(File::open("demo.bin").unwrap());
//! // Read up to the first instance of byte = 97 (a lowercase ASCII "a")
//! let data = f1.read_until_to_bytes(97).unwrap();
//!
//! // This assumes that we have a file `demo.txt` in the current directory
//! let mut f2 = BufReader::new(File::open("demo.txt").unwrap());
//! let text = f2.read_line_to_string().unwrap();
//! ```
//!
//! # Implementing `QRead` and `QBufRead`
//!
//! You should not need to implement these traits on your own custom objects. Instead, implement [`Read`](std::io::Read)
//! and [`BufRead`](std::io::BufRead), respectively. Because [`QRead`] and [`QBufRead`] have blanket implementations for
//! any types implementing `Read` and `BufRead`, respectively, implementing the latter traits automatically grants the former.
use ;
/// Trait for implementing ergonomic reads of full file content
///
/// This trait implements wrappers around two of the methods from [`std::io::Read`]
/// to handle allocating the necessary buffer for you.
/// Trait for implementing ergonomic reads of buffered file content
///
/// This trait implements wrappers around two of the methods from [`std::io::BufRead`]
/// to handle allocating the necessary buffer for you.