| 1 |
use std::mem; |
| 2 |
|
| 3 |
/** |
| 4 |
* Leak a piece of data by never calling its destructor |
| 5 |
* |
| 6 |
* Useful for things that are going to be used for the life of the program, but aren't technically |
| 7 |
* static (because they are created in response to arguments, environment, or other |
| 8 |
* configuration/data read at program start). |
| 9 |
* |
| 10 |
* This is a modified version of the proposed rfc: https://github.com/rust-lang/rfcs/pull/1233 |
| 11 |
* |
| 12 |
* |
| 13 |
* Notable changes: |
| 14 |
* - for user convenience, leak() is a non-static method |
| 15 |
* - Return `&T` instead of `&mut T` |
| 16 |
* |
| 17 |
* While it would be ideal to return a `&'a mut T`, we apparently can't do that due to limitations |
| 18 |
* in rust's borrow checker causing soundness issues. Details are in the RFC liked above. |
| 19 |
*/ |
| 20 |
pub trait Leak<T : ?Sized> { |
| 21 |
fn leak<'a>(self) -> &'a T where T: 'a; |
| 22 |
} |
| 23 |
|
| 24 |
impl<T : ?Sized> Leak<T> for Box<T> { |
| 25 |
fn leak<'a>(self) -> &'a T where T: 'a { |
| 26 |
let r = Self::into_raw(self); |
| 27 |
unsafe { &mut *r } |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/* |
| 32 |
* while String and Vec<T> could have impls in terms of Box, we specialize them because their |
| 33 |
* conversions to Box (into_boxed_slice and into_boxed_str) result in resizing underlying storage |
| 34 |
*/ |
| 35 |
|
| 36 |
impl Leak<str> for String { |
| 37 |
fn leak<'a>(mut self) -> &'a str where Self: 'a { |
| 38 |
let r: *mut str = &mut self[..]; |
| 39 |
mem::forget(self); |
| 40 |
unsafe { &mut *r } |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl<T> Leak<[T]> for Vec<T> { |
| 45 |
fn leak<'a>(mut self) -> &'a [T] where [T]: 'a { |
| 46 |
let r: *mut [T] = &mut self[..]; |
| 47 |
mem::forget(self); |
| 48 |
unsafe { &mut *r } |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(test)] |
| 53 |
mod test { |
| 54 |
#[test] |
| 55 |
fn leak_str() { |
| 56 |
use super::Leak; |
| 57 |
use std::borrow::ToOwned; |
| 58 |
|
| 59 |
let v = "hi"; |
| 60 |
let leaked : &str = { |
| 61 |
let o = v.to_owned(); |
| 62 |
o.leak() |
| 63 |
}; |
| 64 |
assert_eq!(leaked, v); |
| 65 |
|
| 66 |
let leaked : &'static str = { |
| 67 |
let o = v.to_owned(); |
| 68 |
o.leak() |
| 69 |
}; |
| 70 |
assert_eq!(leaked, v); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn leak_empty_str() { |
| 75 |
use super::Leak; |
| 76 |
use std::borrow::ToOwned; |
| 77 |
|
| 78 |
let v = ""; |
| 79 |
let leaked : &'static str = { |
| 80 |
let o = v.to_owned(); |
| 81 |
o.leak() |
| 82 |
}; |
| 83 |
assert_eq!(leaked, v); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn leak_vec() { |
| 88 |
use super::Leak; |
| 89 |
|
| 90 |
let v = vec![3, 5]; |
| 91 |
let leaked : &'static [u8] = { |
| 92 |
let o = v.clone(); |
| 93 |
o.leak() |
| 94 |
}; |
| 95 |
assert_eq!(leaked, &*v); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn leak_empty_vec() { |
| 100 |
use super::Leak; |
| 101 |
|
| 102 |
let v = vec![]; |
| 103 |
let leaked : &'static [u8] = { |
| 104 |
let o = v.clone(); |
| 105 |
o.leak() |
| 106 |
}; |
| 107 |
assert_eq!(leaked, &*v); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn leak_box() { |
| 112 |
use super::Leak; |
| 113 |
|
| 114 |
let v : Box<[&str]> = vec!["hi", "there"].into_boxed_slice(); |
| 115 |
let leaked : &'static [&str] = { |
| 116 |
let o = v.clone(); |
| 117 |
o.leak() |
| 118 |
}; |
| 119 |
assert_eq!(leaked, &*v); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn leak_nested() { |
| 124 |
use super::Leak; |
| 125 |
|
| 126 |
let v : Box<Vec<&str>> = Box::new(vec!["hi", "there"]); |
| 127 |
let leaked : &'static [&str] = { |
| 128 |
let o = v.clone(); |
| 129 |
o.leak() |
| 130 |
}; |
| 131 |
assert_eq!(leaked, &**v); |
| 132 |
} |
| 133 |
} |