Skip to content

Commit 9d9af08

Browse files
committed
---
yaml --- r: 229029 b: refs/heads/try c: 94a89e5 h: refs/heads/master i: 229027: 343c967 v: v3
1 parent ad7dd58 commit 9d9af08

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: 13b2605ed985a37481dc497357dc6dcdf37ba6ff
4+
refs/heads/try: 94a89e561a62d54d25dd64329ef8af396d8ec032
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/src/doc/tarpl/casts.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
% Casts
22

3-
Casts are a superset of coercions: every coercion can be explicitly invoked via
4-
a cast, but some conversions *require* a cast. These "true casts" are generally
5-
regarded as dangerous or problematic actions. True casts revolve around raw
6-
pointers and the primitive numeric types. True casts aren't checked.
3+
Casts are a superset of coercions: every coercion can be explicitly
4+
invoked via a cast. However some conversions *require* a cast.
5+
While coercions are pervasive and largely harmless, these "true casts"
6+
are rare and potentially dangerous. As such, casts must be explicitly invoked
7+
using the `as` keyword: `expr as Type`.
8+
9+
True casts generally revolve around raw pointers and the primitive numeric
10+
types. Even though they're dangerous, these casts are *infallible* at runtime.
11+
If a cast triggers some subtle corner case no indication will be given that
12+
this occurred. The cast will simply succeed.
13+
14+
That said, casts aren't `unsafe` because they generally can't violate memory
15+
safety *on their own*. For instance, converting an integer to a raw pointer can
16+
very easily lead to terrible things. However the act of creating the pointer
17+
itself is safe, because actually using a raw pointer is already marked as
18+
`unsafe`.
719

820
Here's an exhaustive list of all the true casts. For brevity, we will use `*`
921
to denote either a `*const` or `*mut`, and `integer` to denote any integral
@@ -22,13 +34,8 @@ primitive:
2234
* `fn as *T` where `T: Sized`
2335
* `fn as integer`
2436

25-
where `&.T` and `*T` are references of either mutability,
26-
and where unsize_kind(`T`) is the kind of the unsize info
27-
in `T` - the vtable for a trait definition (e.g. `fmt::Display` or
28-
`Iterator`, not `Iterator<Item=u8>`) or a length (or `()` if `T: Sized`).
29-
3037
Note that lengths are not adjusted when casting raw slices -
31-
`T: *const [u16] as *const [u8]` creates a slice that only includes
38+
`*const [u16] as *const [u8]` creates a slice that only includes
3239
half of the original memory.
3340

3441
Casting is not transitive, that is, even if `e as U1 as U2` is a valid

branches/try/src/doc/tarpl/coercions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ Here's all the kinds of coercion:
99

1010
Coercion is allowed between the following types:
1111

12-
* Subtyping: `T` to `U` if `T` is a [subtype](lifetimes.html#subtyping-and-variance)
13-
of `U`
12+
* Subtyping: `T` to `U` if `T` is a [subtype][] of `U`
1413
* Transitivity: `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to `T_3`
1514
* Pointer Weakening:
1615
* `&mut T` to `&T`
@@ -25,7 +24,6 @@ only implemented automatically, and enables the following transformations:
2524

2625
* `[T, ..n]` => `[T]`
2726
* `T` => `Trait` where `T: Trait`
28-
* `SubTrait` => `Trait` where `SubTrait: Trait` (TODO: is this now implied by the previous?)
2927
* `Foo<..., T, ...>` => `Foo<..., U, ...>` where:
3028
* `T: Unsize<U>`
3129
* `Foo` is a struct
@@ -70,3 +68,5 @@ fn main() {
7068
<anon>:10 foo(t);
7169
^~~
7270
```
71+
72+
[subtype]: subtyping.html

branches/try/src/doc/tarpl/conversions.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
% Type Conversions
22

33
At the end of the day, everything is just a pile of bits somewhere, and type
4-
systems are just there to help us use those bits right. Needing to reinterpret
5-
those piles of bits as different types is a common problem and Rust consequently
6-
gives you several ways to do that.
4+
systems are just there to help us use those bits right. There are two common
5+
problems with typing bits: needing to reinterpret those exact bits as a
6+
different type, and needing to change the bits to have equivalent meaning for
7+
a different type. Because Rust encourages encoding important properties in the
8+
type system, these problems are incredibly pervasive. As such, Rust
9+
consequently gives you several ways to solve them.
710

811
First we'll look at the ways that *Safe Rust* gives you to reinterpret values.
912
The most trivial way to do this is to just destructure a value into its
@@ -26,6 +29,6 @@ fn reinterpret(foo: Foo) -> Bar {
2629
}
2730
```
2831

29-
But this is, at best, annoying to do. For common conversions, Rust provides
32+
But this is, at best, annoying. For common conversions, Rust provides
3033
more ergonomic alternatives.
3134

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
% Transmutes
22

33
Get out of our way type system! We're going to reinterpret these bits or die
4-
trying! Even though this book is all about doing things that are unsafe, I really
5-
can't emphasize that you should deeply think about finding Another Way than the
6-
operations covered in this section. This is really, truly, the most horribly
7-
unsafe thing you can do in Rust. The railguards here are dental floss.
4+
trying! Even though this book is all about doing things that are unsafe, I
5+
really can't emphasize that you should deeply think about finding Another Way
6+
than the operations covered in this section. This is really, truly, the most
7+
horribly unsafe thing you can do in Rust. The railguards here are dental floss.
88

99
`mem::transmute<T, U>` takes a value of type `T` and reinterprets it to have
1010
type `U`. The only restriction is that the `T` and `U` are verified to have the
@@ -17,13 +17,19 @@ same size. The ways to cause Undefined Behaviour with this are mind boggling.
1717
* Making a primitive with an invalid value is UB
1818
* Transmuting between non-repr(C) types is UB
1919
* Transmuting an & to &mut is UB
20+
* Transmuting an & to &mut is *always* UB
21+
* No you can't do it
22+
* No you're not special
2023
* Transmuting to a reference without an explicitly provided lifetime
21-
produces an [unbound lifetime](lifetimes.html#unbounded-lifetimes)
24+
produces an [unbounded lifetime][]
2225

2326
`mem::transmute_copy<T, U>` somehow manages to be *even more* wildly unsafe than
2427
this. It copies `size_of<U>` bytes out of an `&T` and interprets them as a `U`.
2528
The size check that `mem::transmute` has is gone (as it may be valid to copy
2629
out a prefix), though it is Undefined Behaviour for `U` to be larger than `T`.
2730

2831
Also of course you can get most of the functionality of these functions using
29-
pointer casts.
32+
pointer casts.
33+
34+
35+
[unbounded lifetime]: unbounded-lifetimes.html

0 commit comments

Comments
 (0)