@@ -54,15 +54,27 @@ actually think about it is probably the general *Deref Coercion*: `&T` coerces t
54
54
55
55
Casts are a superset of coercions: every coercion can be explicitly invoked via a cast,
56
56
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:
60
59
61
60
* rawptr -> rawptr (e.g. ` *mut T as *const T ` or ` *mut T as *mut U ` )
62
61
* 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 ` )
65
64
* ` 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 ` .
66
78
67
79
68
80
## Conversion Traits
0 commit comments