Pre-RFC: Read::read_into_uninitialized

It would be better to have an ‘uninitialized buffer’ trait rather than forcing every Read impl to take care of it (and use unsafe code). Something like

unsafe trait UninitializedBuffer<T> {
    fn get(&mut self) -> *mut [T];
    unsafe fn did_fill(&mut self, size: usize);
    // + safe helper methods to append, etc.
}
// example
unsafe impl<T> UninitializedBuffer<T> for Vec<T> {
    fn get(&mut self) -> *mut [T] {
        unsafe {
            slice::from_raw_parts_mut(self.as_mut_ptr().offset(self.len() as isize),
                                      self.capacity() - self.len()) as *mut [T]
        }
    }
    unsafe fn did_fill(&mut self, size: usize) {
        self.set_len(self.len() + size);
    }
}
2 Likes