Hello,
I've had a strange experience: when I try to read part of a buffer using these two methods, one works and the other doesn't!
I have a main buffer which is a Cursor :
buf_reader
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter, ReadHalf, WriteHalf};
use std::io::{Cursor, Error};
and this is the two ways to read from this buffer and put it in another temporary buffer :
Method 1 :
...
println!("> payload_len = {}", payload_len);
// ----------------
let mut pl_buf : Vec<u8> = vec![0; payload_len as usize];
// ----------------
println!("> buffer capacity = {}",pl_buf.capacity());
buf_reader.read_exact(&mut pl_buf).await?;
println!("> buffer len = {}", pl_buf.len());
...
> payload_len = 8073
> buffer capacity = 8073
> buffer len = 8073
buffer len = 8073 : GOOD
Method 2 :
...
println!("> payload_len = {}", payload_len);
// ----------------
let mut pl_buf: Vec<u8> = Vec::with_capacity(payload_len as usize);
// ----------------
println!("> buffer capacity = {}",pl_buf.capacity());
buf_reader.read_exact(&mut pl_buf).await?;
println!("> buffer len = {}", pl_buf.len());
...
> payload_len = 8073
> buffer capacity = 8073
> buffer len = 0
buffer len = 0 !!!! NOT GOOD
But if I set the payload_len to a value < 8064, it works
what's strange is that when I use this method in another project, it works correctly.
let mut pl_buf : Vec<u8> = Vec::with_capacity(payload_len as usize)
do you have an explanation for this behavior ?
why it works with :
let mut pl_buf : Vec<u8> = vec![0; payload_len as usize];
and not with :
let mut pl_buf : Vec<u8> = Vec::with_capacity(payload_len as usize);
thank you in advance for your advice.