1 use std::mem; 2 use std::slice; 3 4 /// Slice from `vec[vec.len()..vec.capacity()]` remaining_capacity_as_slice_mut<A>(vec: &mut Vec<A>) -> &mut [A]5pub unsafe fn remaining_capacity_as_slice_mut<A>(vec: &mut Vec<A>) -> &mut [A] { 6 slice::from_raw_parts_mut( 7 vec.as_mut_slice().as_mut_ptr().offset(vec.len() as isize), 8 vec.capacity() - vec.len(), 9 ) 10 } 11 remove_lifetime_mut<A: ?Sized>(a: &mut A) -> &'static mut A12pub unsafe fn remove_lifetime_mut<A: ?Sized>(a: &mut A) -> &'static mut A { 13 mem::transmute(a) 14 } 15 16 #[cfg(test)] 17 mod test { 18 use super::*; 19 20 #[test] test_remaining_capacity_as_slice_mut()21 fn test_remaining_capacity_as_slice_mut() { 22 let mut v = Vec::with_capacity(5); 23 v.push(10); 24 v.push(11); 25 v.push(12); 26 unsafe { 27 { 28 let s = remaining_capacity_as_slice_mut(&mut v); 29 assert_eq!(2, s.len()); 30 s[0] = 13; 31 s[1] = 14; 32 } 33 v.set_len(5); 34 } 35 assert_eq!(vec![10, 11, 12, 13, 14], v); 36 } 37 } 38