[web] confirm safety of advance

After further testing and checks I'm fairy confident that the vectored
write code is safe.

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-11-03 20:30:30 -04:00
parent 63cfa014d5
commit 3a4afbcaea

View file

@ -77,7 +77,9 @@ fn advance<'a>(buf: &mut IoSlice<'a>, n: usize) {
if buf.len() < n {
panic!("advancing IoSlice beyond its length");
}
// SAFTEY: hopefully
// This is just a hacky way of advancing the pointer inside the IoSlice
// SAFTEY: The newly constructed IoSlice has the same lifetime as the old and
// this is guaranteed not to overflow the buffer due to the previous check
unsafe {
let mut ptr = buf.as_ptr() as *mut u8;
ptr = ptr.add(n);
@ -87,3 +89,17 @@ fn advance<'a>(buf: &mut IoSlice<'a>, n: usize) {
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_advance() {
let expected: Vec<_> = (10..100).collect();
let buf: Vec<_> = (0..100).collect();
let mut io_slice = IoSlice::new(&buf);
advance(&mut io_slice, 10);
assert_eq!(io_slice.len(), 90);
assert_eq!(&*io_slice, &expected);
}
}