about summary refs log tree commit diff
path: root/src/test/compile-fail/removed-syntax-fixed-vec.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-02-01 21:53:53 +0000
committerbors <bors@rust-lang.org>2015-02-01 21:53:53 +0000
commitca4b9674c26c1de07a2042cb68e6a062d7184cef (patch)
tree759ff3ca5e735f22b9cc47a4bec21e87aa204262 /src/test/compile-fail/removed-syntax-fixed-vec.rs
parentc2bda2a5bb55e2ed54fecd2a03b133bb108e66e7 (diff)
parent5a722f8632eabfa5a776171ebdd6c1f6385098c7 (diff)
downloadrust-ca4b9674c26c1de07a2042cb68e6a062d7184cef.tar.gz
rust-ca4b9674c26c1de07a2042cb68e6a062d7184cef.zip
Auto merge of #21318 - stepancheg:box-fns, r=alexcrichton
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 = b.into_raw();
```

`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/test/compile-fail/removed-syntax-fixed-vec.rs')
0 files changed, 0 insertions, 0 deletions