MMK
January 25, 2023, 3:14pm
1
I tried:
pub fn get(&self) -> io::Result<String> {
let rd = &mut self.file.borrow_mut().read;
rd.rewind()?;
let mut data = Vec::new();
rd.read(&mut data)?;
dbg!(&data);
let data = String::from_utf8_lossy(&data).into_owned();
dbg!(&data);
Ok(data)
}
but Read::read() didn't add anything to the vector
xfix
January 25, 2023, 3:18pm
2
Read::read
accepts &mut [u8]
, not &mut Vec<u8>
. To read to end, use Read::read_to_end
instead which accepts &mut Vec<u8>
. Also, because you are reading to string, you may also consider using Read::read_to_string
.
Clippy will notice this problem: Clippy Lints .
2 Likes
MMK
January 25, 2023, 3:21pm
3
but doesen't the compiler do the conversion?
xfix
January 25, 2023, 3:23pm
4
Vec<u8>
has an implementation of DerefMut
which can be used to deref coerce &mut Vec<u8>
into &mut [u8]
.
In this case because the vector is empty you end up with essentially passing &mut []
to read
function which tells read
to not actually read anything.
1 Like
MMK
January 25, 2023, 3:26pm
5
I don't understand, shoulden't read() add bytes to the vector?
&mut [u8]
cannot be resized, so it cannot grow to hold the additional data.
read_to_end
and read_to_string
specifically take &mut Vec<u8>
, allowing them to resize (and potentially reallocate) when the buffer is filled.
2 Likes
ZiCog
January 25, 2023, 3:48pm
8
MMK:
rd.read
Beware that .read()
returns a Result that you should check. On success the result contains the number of bytes read. Which may be less than the size of the given buffer.
3 Likes
system
Closed
April 25, 2023, 3:49pm
9
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.