1 /// Can create buffers of arbitrary lifetime. 2 /// Frees created buffers when dropped. 3 /// 4 /// This struct is of course unsafe and the fact that 5 /// it must outlive the created slices has to be ensured by 6 /// the programmer. 7 /// 8 /// Used at certain test scenarios as a safer version of 9 /// Vec::leak, to satisfy the address sanitizer. 10 pub struct LeakedBuffers { 11 leaked_vecs: Vec<Box<[u8]>>, 12 } 13 14 impl LeakedBuffers { new() -> Self15 pub fn new() -> Self { 16 Self { 17 leaked_vecs: vec![], 18 } 19 } create<'a>(&mut self, size: usize) -> &'a mut [u8]20 pub unsafe fn create<'a>(&mut self, size: usize) -> &'a mut [u8] { 21 let new_mem = vec![0u8; size].into_boxed_slice(); 22 self.leaked_vecs.push(new_mem); 23 let new_mem = self.leaked_vecs.last_mut().unwrap(); 24 std::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), new_mem.len()) 25 } 26 } 27