Skip to content

Commit b12f368

Browse files
committedJun 29, 2021
Re-organize casting section
1 parent f2e228e commit b12f368

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed
 

‎src/casts.md

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
# Casts
22

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. That said, casts must be valid
13-
at the type level, or else they will be prevented statically. For instance,
14-
`7u8 as bool` will not compile.
15-
16-
That said, casts aren't `unsafe` because they generally can't violate memory
17-
safety *on their own*. For instance, converting an integer to a raw pointer can
18-
very easily lead to terrible things. However the act of creating the pointer
19-
itself is safe, because actually using a raw pointer is already marked as
20-
`unsafe`.
3+
Casts are a superset of coercions: every coercion can be explicitly invoked via a cast.
4+
However some conversions require a cast.
5+
While coercions are pervasive and largely harmless, these "true casts" are rare and potentially dangerous.
6+
As such, casts must be explicitly invoked using the `as` keyword: `expr as Type`.
7+
8+
You can find an exhaustive list of [all the true casts][cast list] and [casting semantics][semantics list] on the reference.
9+
10+
## Safety of casting
11+
12+
True casts generally revolve around raw pointers and the primitive numeric types.
13+
Even though they're dangerous, these casts are infallible at runtime.
14+
If a cast triggers some subtle corner case no indication will be given that this occurred.
15+
The cast will simply succeed.
16+
That said, casts must be valid at the type level, or else they will be prevented statically.
17+
For instance, `7u8 as bool` will not compile.
18+
19+
That said, casts aren't `unsafe` because they generally can't violate memory safety *on their own*.
20+
For instance, converting an integer to a raw pointer can very easily lead to terrible things.
21+
However the act of creating the pointer itself is safe, because actually using a raw pointer is already marked as `unsafe`.
22+
23+
## Some notes about casting
24+
25+
### Lengths when casting raw slices
2126

2227
Note that lengths are not adjusted when casting raw slices; `*const [u16] as *const [u8]` creates a slice that only includes half of the original memory.
2328

24-
Casting is not transitive, that is, even if `e as U1 as U2` is a valid expression, `e as U2` is not necessarily so.
29+
### Transitivity
2530

26-
You can find an exhaustive list of [all the true casts][cast list] and [casting semantics][semantics list] on the reference.
31+
Casting is not transitive, that is, even if `e as U1 as U2` is a valid expression, `e as U2` is not necessarily so.
2732

2833
[cast list]: ../reference/expressions/operator-expr.html#type-cast-expressions
2934
[semantics list]: ../reference/expressions/operator-expr.html#semantics

0 commit comments

Comments
 (0)
Please sign in to comment.