tmp_vec/
lib.rs

1//! Problem:
2//!
3//! ```
4//! pub struct Foo {
5//!     ...
6//! }
7//!
8//! impl Foo {
9//!     fn do_something(&mut self, ...){
10//!         //FIXME: avoid allocation. can't fix this because T is lifetime-bound.
11//!         let mut guards: Vec<Guard<'x>> = Vec::with_capacity(xxx.len());
12//!         ...
13//!     }
14//! }
15//! ```
16//!
17//! Solution:
18//!
19//! ```
20//! use tmp_vec::TmpVec;
21//!
22//! pub struct Foo {
23//!     tmp_guards: TmpVec<Guard<'static>>,
24//!     ...
25//! }
26//!
27//! impl Foo {
28//!     fn do_something(&mut self, ...){
29//!          let mut guards = self.tmp_guards.borrow_mut();
30//!          ...
31//!     }
32//! }
33//!         
34//! ```
35
36use std::ops::{Deref, DerefMut};
37
38pub struct BorrowMut<'a, T: 'a> {
39    inner: &'a mut Vec<T>
40}
41
42impl<'a, T> Deref for BorrowMut<'a, T> {
43    type Target = Vec<T>;
44    fn deref(&self) -> &Self::Target { self.inner }
45}
46
47impl<'a, T> DerefMut for BorrowMut<'a, T> {
48    fn deref_mut(&mut self) -> &mut Self::Target { self.inner }
49}
50
51impl<'a, T> Drop for BorrowMut<'a, T> {
52    fn drop(&mut self){
53        self.inner.clear();
54    }
55}
56
57pub struct TmpVec<U> {
58    inner: Vec<U>
59}
60
61impl<U> Default for TmpVec<U> {
62    fn default() -> Self { Self::new() }
63}
64
65impl<U> TmpVec<U> {
66    pub fn new() -> Self { TmpVec{ inner: Vec::new() } }
67    pub fn with_capacity(cap: usize) -> Self { TmpVec{ inner: Vec::with_capacity(cap) } }
68    
69    pub fn borrow_mut<T>(&mut self) -> BorrowMut<T> {
70        use std::mem;
71        assert_eq!(mem::size_of::<T>(), mem::size_of::<U>());
72        assert_eq!(mem::align_of::<T>(), mem::align_of::<U>());
73        unsafe { self.inner.set_len(0); } // leak if BorrowMut was leaked
74        BorrowMut{ inner: unsafe { mem::transmute(&mut self.inner) } }
75    }
76}
77
78impl<U> Drop for TmpVec<U> {
79    fn drop(&mut self){
80        unsafe { self.inner.set_len(0); } // leak if BorrowMut was leaked
81    }
82}
83