Skip to content

Commit d65c19d

Browse files
committed
Add some casts I missed the first time around and a few misc. corrections.
1 parent a3025c8 commit d65c19d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

0000-coercions.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Summary
66

77
Describe the various kinds of type conversions available in Rust and suggest
8-
some tweeks.
8+
some tweaks.
99

1010
Provide a mechanism for smart pointers to be part of the DST coercion system.
1111

@@ -177,7 +177,7 @@ And where coerce_inner is defined as
177177

178178
* coerce_inner(`(..., T)`) = `(..., coerce_inner(T))`.
179179

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-
181181
trivial. One implementation strategy which avoids re-computation of vtables is
182182
given in RFC PR #250.
183183

@@ -259,6 +259,8 @@ Here is an example implementation of `CoerceUnsized` for `Rc`:
259259

260260
```
261261
impl<Sized? T, Sized? U> CoerceUnsized<Rc<T>> for Rc<U> {
262+
where U: Unsize<T>
263+
262264
fn coerce(self) -> Rc<T> {
263265
let new_ptr: *const RcBox<T> = fat_pointer_convert(self._ptr);
264266
Rc { _ptr: new_ptr }
@@ -304,7 +306,7 @@ reference apply. (Note that the implementation of the coercion isn't so simple,
304306
it is embedded in the search for candidate methods, but from the point of view
305307
of type conversions, that is not relevant).
306308

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.
308310
First, we dereference and then take the address until the source type has the
309311
same shape (i.e., has the same kind and number of indirection) as the target
310312
type. Then we try to coerce the adjusted source type to the target type using
@@ -317,13 +319,20 @@ descriptions are equivalent.
317319
Casting is indicated by the `as` keyword. A cast `e as U` is valid if one of the
318320
following holds:
319321

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;
321329

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`;
323331

324-
* `T == u8` and `U == char`;
332+
* `e` has type `T` and `T == u8` and `U == char`;
325333

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`.
327336

328337
Casting is not transitive, that is, even if `e as U1 as U2` is a valid
329338
expression, `e as U2` is not necessarily so (in fact it will only be valid if
@@ -420,6 +429,9 @@ worse.
420429

421430
These rules could be tweaked in any number of ways.
422431

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.
423435

424436
# Unresolved questions
425437

0 commit comments

Comments
 (0)