Skip to content

Commit 3318e3d

Browse files
committed
progress
1 parent 39c0473 commit 3318e3d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

conversions.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,27 @@ actually think about it is probably the general *Deref Coercion*: `&T` coerces t
5454

5555
Casts are a superset of coercions: every coercion can be explicitly invoked via a cast,
5656
but some changes require a cast. These "true casts" are generally regarded as dangerous or
57-
problematic actions. The set of true casts is actually quite small, and once again revolves
58-
largely around pointers. However it also introduces the primary mechanism to convert between
59-
numeric types.
57+
problematic actions. True casts revolves around raw pointers and the primitive numeric
58+
types. Here's an exhaustive list of all the true casts:
6059

6160
* rawptr -> rawptr (e.g. `*mut T as *const T` or `*mut T as *mut U`)
6261
* rawptr <-> usize (e.g. `*mut T as usize` or `usize as *mut T`)
63-
* primitive -> primitive (e.g. `u32 as u8` or `u8 as u32`)
64-
* c-like enum -> integer/bool (e.g. `DaysOfWeek as u8`)
62+
* number -> number (e.g. `u32 as i8` or `i16 as f64`)
63+
* c-like enum -> integer/bool (e.g. `DaysOfWeek as u32`)
6564
* `u8` -> `char`
65+
* something about arrays?
66+
67+
For number -> number casts, there are quite a few cases to consider:
68+
69+
* unsigned to bigger unsigned will zero-extend losslessly
70+
* unsigned to smaller unsigned will truncate via wrapping
71+
* signed to unsigned will ... TODO rest of this list
72+
73+
The casts involving rawptrs also allow us to completely bypass type-safety
74+
by re-interpretting a pointer of T to a pointer of U for arbitrary types, as
75+
well as interpret integers as addresses. However it is impossible to actually
76+
*capitalize* on this violation in Safe Rust, because derefencing a raw ptr is
77+
`unsafe`.
6678

6779

6880
## Conversion Traits

0 commit comments

Comments
 (0)