about summary refs log tree commit diff
path: root/src/libcore/unsafe.rs
blob: 22cce4956498260f83a5fd5771d0266c071c3c0c (plain)
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
/*
Module: unsafe

Unsafe operations
*/

#[abi = "rust-intrinsic"]
native mod rusti {
    fn cast<T, U>(src: T) -> U;
}

#[abi = "cdecl"]
native mod rustrt {
    fn leak<T>(-thing: T);
}

/*
Function: reinterpret_cast

Casts the value at `src` to U. The two types must have the same length.
*/
unsafe fn reinterpret_cast<T, U>(src: T) -> U {
    let t1 = sys::get_type_desc::<T>();
    let t2 = sys::get_type_desc::<U>();
    if (*t1).size != (*t2).size {
        fail "attempt to cast values of differing sizes";
    }
    ret rusti::cast(src);
}

/*
Function: leak

Move `thing` into the void.

The leak function will take ownership of the provided value but neglect
to run any required cleanup or memory-management operations on it. This
can be used for various acts of magick, particularly when using
reinterpret_cast on managed pointer types.
*/
unsafe fn leak<T>(-thing: T) { rustrt::leak(thing); }