diff options
| author | Stepan Koltsov <stepan.koltsov@gmail.com> | 2015-02-01 20:15:44 +0300 |
|---|---|---|
| committer | Stepan Koltsov <stepan.koltsov@gmail.com> | 2015-02-01 20:15:44 +0300 |
| commit | 5a722f8632eabfa5a776171ebdd6c1f6385098c7 (patch) | |
| tree | 26d25ba59daf1b74e534f79e08d5b45720c7a7e8 /src/libstd/sys/unix/stack_overflow.rs | |
| parent | 0ab8d5dadd72b8d44cc79a1756c0a00fec619efa (diff) | |
| download | rust-5a722f8632eabfa5a776171ebdd6c1f6385098c7.tar.gz rust-5a722f8632eabfa5a776171ebdd6c1f6385098c7.zip | |
box: into_raw, from_raw functions
Functions are needed for safety and convenience. It is a common pattern to use `mem::transmute` to convert between `Box` and raw pointer, like this: ``` let b = Box::new(3); let p = mem::transmute(b); // pass `p` to some C library ``` After this commit, conversion can be written as: ``` let p = boxed::into_raw(b); ``` `into_raw` and `from_raw` functions are still unsafe, but they are much safer than `mem::transmute`, because *raw functions do not convert between incompatible pointers. For example, this likely incorrect code can be successfully compiled: ``` let p: *mut u64 = ... let b: Box<u32> = mem::transmute(p); ``` Using `from_raw` results in compile-time error: ``` let p: *mut u64 = ... let b: Box<u32> = Box::from_raw(p); // compile-time error ``` `into_raw` and `from_raw` functions are similar to C++ `std::unique_ptr` `release` function [1] and constructor from pointer [2]. [1] http://en.cppreference.com/w/cpp/memory/unique_ptr/release [2] http://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr
Diffstat (limited to 'src/libstd/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions
