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
use std::ptr::drop_in_place;
use internal::gen_free;
pub trait Free {
fn free(ptr: *mut Self);
}
fn free_ptr_ref<T>(ptr: *mut T) {
unsafe {
drop_in_place(ptr);
gen_free(ptr);
}
}
impl<T> Free for T {
#[cfg(nightly_channel)]
default fn free(ptr_ref: *mut T) {
free_ptr_ref(ptr_ref);
}
#[cfg(stable_channel)]
fn free(ptr_ref: *mut T) {
free_ptr_ref(ptr_ref);
}
}
impl<T> Free for [T] {
fn free(fat_ptr: *mut [T]) {
unsafe {
let thin_ptr = (*fat_ptr).as_mut_ptr();
drop_in_place(fat_ptr);
gen_free(thin_ptr);
}
}
}
impl Free for str {
fn free(fat_ptr: *mut str) {
Free::free(fat_ptr as *mut [u8]);
}
}