|
1 | 1 | mod lines;
|
2 | 2 | mod read_line;
|
3 | 3 | mod read_until;
|
| 4 | +mod split; |
4 | 5 |
|
5 | 6 | pub use lines::Lines;
|
| 7 | +pub use split::Split; |
| 8 | + |
6 | 9 | use read_line::ReadLineFuture;
|
7 | 10 | use read_until::ReadUntilFuture;
|
8 | 11 |
|
@@ -226,6 +229,57 @@ extension_trait! {
|
226 | 229 | read: 0,
|
227 | 230 | }
|
228 | 231 | }
|
| 232 | + |
| 233 | + #[doc = r#" |
| 234 | + Returns a stream over the contents of this reader split on the byte `byte`. |
| 235 | +
|
| 236 | + The stream returned from this function will return instances of |
| 237 | + [`io::Result`]`<`[`Vec<u8>`]`>`. Each vector returned will *not* have |
| 238 | + the delimiter byte at the end. |
| 239 | +
|
| 240 | + This function will yield errors whenever [`read_until`] would have |
| 241 | + also yielded an error. |
| 242 | +
|
| 243 | + [`io::Result`]: type.Result.html |
| 244 | + [`Vec<u8>`]: ../vec/struct.Vec.html |
| 245 | + [`read_until`]: #method.read_until |
| 246 | +
|
| 247 | + # Examples |
| 248 | +
|
| 249 | + [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In |
| 250 | + this example, we use [`Cursor`] to iterate over all hyphen delimited |
| 251 | + segments in a byte slice |
| 252 | +
|
| 253 | + [`Cursor`]: struct.Cursor.html |
| 254 | +
|
| 255 | + ``` |
| 256 | + # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 257 | + # |
| 258 | + use async_std::prelude::*; |
| 259 | + use async_std::io; |
| 260 | +
|
| 261 | + let cursor = io::Cursor::new(b"lorem-ipsum-dolor"); |
| 262 | +
|
| 263 | + let mut split_iter = cursor.split(b'-').map(|l| l.unwrap()); |
| 264 | + assert_eq!(split_iter.next().await, Some(b"lorem".to_vec())); |
| 265 | + assert_eq!(split_iter.next().await, Some(b"ipsum".to_vec())); |
| 266 | + assert_eq!(split_iter.next().await, Some(b"dolor".to_vec())); |
| 267 | + assert_eq!(split_iter.next().await, None); |
| 268 | + # |
| 269 | + # Ok(()) }) } |
| 270 | + ``` |
| 271 | + "#] |
| 272 | + fn split(self, byte: u8) -> Split<Self> |
| 273 | + where |
| 274 | + Self: Sized, |
| 275 | + { |
| 276 | + Split { |
| 277 | + reader: self, |
| 278 | + buf: Vec::new(), |
| 279 | + delim: byte, |
| 280 | + read: 0, |
| 281 | + } |
| 282 | + } |
229 | 283 | }
|
230 | 284 |
|
231 | 285 | impl<T: BufRead + Unpin + ?Sized> BufRead for Box<T> {
|
|
0 commit comments