serial-rs icon indicating copy to clipboard operation
serial-rs copied to clipboard

std::io::Read::read_to_end - Read Vec<> based on Vec<>.capacity() and not Vec<>.len()

Open chkolbe opened this issue 2 years ago • 0 comments

Lib read Implementation receives Bytes based on the Length and not on Capacity. Receiving Bytes must be smaller or equal to Bytes send.

Added read_to_end, to use same Buffer for Writing and Receiving based on Capacity.

Implementation testes on Mac OS. Windows Implementation is untested.

let port = SerialPort::new();
let mut buf: Vec<u8> = Vec::with_capacity(255);
let bytes_count_to_receive: usize = 100;

// We prepare to send one Byte
buf.push(0xff);
port.write_all(buf.as_mut_slice()).unwrap();

// We clear the Vec so Len = 0 and Capacity = 255
buf.clear();

// We expect to receive more as 0 Byte.
if port.read_to_end(&mut buf).unwrap() != bytes_count_to_receive {
    panic!("Can not read all Bytes!");
}

chkolbe avatar Sep 25 '22 18:09 chkolbe