/rust/registry/src/index.crates.io-6f17d22bba15001f/bytes-1.10.1/src/buf/reader.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::Buf; |
2 | | |
3 | | use std::{cmp, io}; |
4 | | |
5 | | /// A `Buf` adapter which implements `io::Read` for the inner value. |
6 | | /// |
7 | | /// This struct is generally created by calling `reader()` on `Buf`. See |
8 | | /// documentation of [`reader()`](Buf::reader) for more |
9 | | /// details. |
10 | | #[derive(Debug)] |
11 | | pub struct Reader<B> { |
12 | | buf: B, |
13 | | } |
14 | | |
15 | 0 | pub fn new<B>(buf: B) -> Reader<B> { |
16 | 0 | Reader { buf } |
17 | 0 | } Unexecuted instantiation: bytes::buf::reader::new::<opendal::types::buffer::Buffer> Unexecuted instantiation: bytes::buf::reader::new::<&[u8]> Unexecuted instantiation: bytes::buf::reader::new::<_> |
18 | | |
19 | | impl<B: Buf> Reader<B> { |
20 | | /// Gets a reference to the underlying `Buf`. |
21 | | /// |
22 | | /// It is inadvisable to directly read from the underlying `Buf`. |
23 | | /// |
24 | | /// # Examples |
25 | | /// |
26 | | /// ```rust |
27 | | /// use bytes::Buf; |
28 | | /// |
29 | | /// let buf = b"hello world".reader(); |
30 | | /// |
31 | | /// assert_eq!(b"hello world", buf.get_ref()); |
32 | | /// ``` |
33 | 0 | pub fn get_ref(&self) -> &B { |
34 | 0 | &self.buf |
35 | 0 | } |
36 | | |
37 | | /// Gets a mutable reference to the underlying `Buf`. |
38 | | /// |
39 | | /// It is inadvisable to directly read from the underlying `Buf`. |
40 | 0 | pub fn get_mut(&mut self) -> &mut B { |
41 | 0 | &mut self.buf |
42 | 0 | } |
43 | | |
44 | | /// Consumes this `Reader`, returning the underlying value. |
45 | | /// |
46 | | /// # Examples |
47 | | /// |
48 | | /// ```rust |
49 | | /// use bytes::Buf; |
50 | | /// use std::io; |
51 | | /// |
52 | | /// let mut buf = b"hello world".reader(); |
53 | | /// let mut dst = vec![]; |
54 | | /// |
55 | | /// io::copy(&mut buf, &mut dst).unwrap(); |
56 | | /// |
57 | | /// let buf = buf.into_inner(); |
58 | | /// assert_eq!(0, buf.remaining()); |
59 | | /// ``` |
60 | 0 | pub fn into_inner(self) -> B { |
61 | 0 | self.buf |
62 | 0 | } |
63 | | } |
64 | | |
65 | | impl<B: Buf + Sized> io::Read for Reader<B> { |
66 | 0 | fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> { |
67 | 0 | let len = cmp::min(self.buf.remaining(), dst.len()); |
68 | 0 |
|
69 | 0 | Buf::copy_to_slice(&mut self.buf, &mut dst[0..len]); |
70 | 0 | Ok(len) |
71 | 0 | } |
72 | | } |
73 | | |
74 | | impl<B: Buf + Sized> io::BufRead for Reader<B> { |
75 | 0 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
76 | 0 | Ok(self.buf.chunk()) |
77 | 0 | } Unexecuted instantiation: <bytes::buf::reader::Reader<opendal::types::buffer::Buffer> as std::io::BufRead>::fill_buf Unexecuted instantiation: <bytes::buf::reader::Reader<&[u8]> as std::io::BufRead>::fill_buf Unexecuted instantiation: <bytes::buf::reader::Reader<_> as std::io::BufRead>::fill_buf |
78 | 0 | fn consume(&mut self, amt: usize) { |
79 | 0 | self.buf.advance(amt) |
80 | 0 | } Unexecuted instantiation: <bytes::buf::reader::Reader<opendal::types::buffer::Buffer> as std::io::BufRead>::consume Unexecuted instantiation: <bytes::buf::reader::Reader<&[u8]> as std::io::BufRead>::consume Unexecuted instantiation: <bytes::buf::reader::Reader<_> as std::io::BufRead>::consume |
81 | | } |