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

use std::ops::{Deref, DerefMut};

pub struct BorrowMut<'a, T: 'a> {
    inner: &'a mut Vec<T>
}

impl<'a, T> Deref for BorrowMut<'a, T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Self::Target { self.inner }
}

impl<'a, T> DerefMut for BorrowMut<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target { self.inner }
}

impl<'a, T> Drop for BorrowMut<'a, T> {
    fn drop(&mut self){
        self.inner.clear();
    }
}

pub struct TmpVec<U> {
    inner: Vec<U>
}

impl<U> Default for TmpVec<U> {
    fn default() -> Self { Self::new() }
}

impl<U> TmpVec<U> {
    pub fn new() -> Self { TmpVec{ inner: Vec::new() } }
    pub fn with_capacity(cap: usize) -> Self { TmpVec{ inner: Vec::with_capacity(cap) } }
    
    pub fn borrow_mut<T>(&mut self) -> BorrowMut<T> {
        use std::mem;
        assert_eq!(mem::size_of::<T>(), mem::size_of::<U>());
        assert_eq!(mem::align_of::<T>(), mem::align_of::<U>());
        unsafe { self.inner.set_len(0); } // leak if BorrowMut was leaked
        BorrowMut{ inner: unsafe { mem::transmute(&mut self.inner) } }
    }
}

impl<U> Drop for TmpVec<U> {
    fn drop(&mut self){
        unsafe { self.inner.set_len(0); } // leak if BorrowMut was leaked
    }
}