5
5
# Summary
6
6
7
7
Describe the various kinds of type conversions available in Rust and suggest
8
- some tweeks .
8
+ some tweaks .
9
9
10
10
Provide a mechanism for smart pointers to be part of the DST coercion system.
11
11
@@ -177,7 +177,7 @@ And where coerce_inner is defined as
177
177
178
178
* coerce_inner(` (..., T) ` ) = ` (..., coerce_inner(T)) ` .
179
179
180
- Note that coercing from sub-trait to a super-trait it a new coercion and is non-
180
+ Note that coercing from sub-trait to a super-trait is a new coercion and is non-
181
181
trivial. One implementation strategy which avoids re-computation of vtables is
182
182
given in RFC PR #250 .
183
183
@@ -259,6 +259,8 @@ Here is an example implementation of `CoerceUnsized` for `Rc`:
259
259
260
260
```
261
261
impl<Sized? T, Sized? U> CoerceUnsized<Rc<T>> for Rc<U> {
262
+ where U: Unsize<T>
263
+
262
264
fn coerce(self) -> Rc<T> {
263
265
let new_ptr: *const RcBox<T> = fat_pointer_convert(self._ptr);
264
266
Rc { _ptr: new_ptr }
@@ -304,7 +306,7 @@ reference apply. (Note that the implementation of the coercion isn't so simple,
304
306
it is embedded in the search for candidate methods, but from the point of view
305
307
of type conversions, that is not relevant).
306
308
307
- Alternatively, a recevier coercion may be thought of as a two stage process.
309
+ Alternatively, a receiver coercion may be thought of as a two stage process.
308
310
First, we dereference and then take the address until the source type has the
309
311
same shape (i.e., has the same kind and number of indirection) as the target
310
312
type. Then we try to coerce the adjusted source type to the target type using
@@ -317,13 +319,20 @@ descriptions are equivalent.
317
319
Casting is indicated by the ` as ` keyword. A cast ` e as U ` is valid if one of the
318
320
following holds:
319
321
320
- * ` e ` has type ` T ` and ` T ` coerces to ` U `
322
+ * ` e ` has type ` T ` and ` T ` coerces to ` U ` ;
323
+
324
+ * ` e ` has type ` *T ` and ` U ` is ` *U_0 ` (i.e., between any raw pointers);
325
+
326
+ * ` e ` has type ` *T ` and ` U ` is ` uint ` , or vice versa;
327
+
328
+ * ` e ` has type ` T ` and ` T ` and ` U ` are any numeric types;
321
329
322
- * ` e ` is a C-like enum and ` U ` is any integer type, ` bool ` , or a raw pointer ;
330
+ * ` e ` is a C-like enum and ` U ` is any integer type, ` bool ` ;
323
331
324
- * ` T == u8 ` and ` U == char ` ;
332
+ * ` e ` has type ` T ` and ` T == u8 ` and ` U == char ` ;
325
333
326
- * ` T == &[V] ` or ` T == &[V, ..n] ` and ` U == *V ` .
334
+ * ` e ` has type ` T ` and ` T == &[V, ..n] ` or ` T == &V ` and ` U == *const V ` , and
335
+ similarly for the mutable variants to either ` *const V ` or ` *mut V ` .
327
336
328
337
Casting is not transitive, that is, even if ` e as U1 as U2 ` is a valid
329
338
expression, ` e as U2 ` is not necessarily so (in fact it will only be valid if
@@ -420,6 +429,9 @@ worse.
420
429
421
430
These rules could be tweaked in any number of ways.
422
431
432
+ Specifically for the DST custom coercions, the compiler could throw an error if
433
+ it finds a user-supplied implementation of the ` Unsize ` trait, rather than
434
+ silently ignoring them.
423
435
424
436
# Unresolved questions
425
437
0 commit comments