Skip to content

Commit b099e0e

Browse files
author
Clar Charr
committed
Move CoerceUnsized to module.
1 parent 6693b4d commit b099e0e

File tree

2 files changed

+82
-69
lines changed

2 files changed

+82
-69
lines changed

src/libcore/ops/mod.rs

+3-69
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ mod index;
155155
mod place;
156156
mod range;
157157
mod try;
158+
mod unsize;
158159

159160
#[stable(feature = "rust1", since = "1.0.0")]
160161
pub use self::arith::{Add, Sub, Mul, Div, Rem, Neg};
@@ -190,7 +191,8 @@ pub use self::try::Try;
190191
#[unstable(feature = "placement_new_protocol", issue = "27779")]
191192
pub use self::place::{Place, Placer, InPlace, Boxed, BoxPlace};
192193

193-
use marker::Unsize;
194+
#[unstable(feature = "coerce_unsized", issue = "27732")]
195+
pub use self::unsize::CoerceUnsized;
194196

195197
/// The `Drop` trait is used to run some code when a value goes out of scope.
196198
/// This is sometimes called a 'destructor'.
@@ -281,71 +283,3 @@ pub trait Drop {
281283
#[stable(feature = "rust1", since = "1.0.0")]
282284
fn drop(&mut self);
283285
}
284-
285-
/// Trait that indicates that this is a pointer or a wrapper for one,
286-
/// where unsizing can be performed on the pointee.
287-
///
288-
/// See the [DST coercion RfC][dst-coerce] and [the nomicon entry on coercion][nomicon-coerce]
289-
/// for more details.
290-
///
291-
/// For builtin pointer types, pointers to `T` will coerce to pointers to `U` if `T: Unsize<U>`
292-
/// by converting from a thin pointer to a fat pointer.
293-
///
294-
/// For custom types, the coercion here works by coercing `Foo<T>` to `Foo<U>`
295-
/// provided an impl of `CoerceUnsized<Foo<U>> for Foo<T>` exists.
296-
/// Such an impl can only be written if `Foo<T>` has only a single non-phantomdata
297-
/// field involving `T`. If the type of that field is `Bar<T>`, an implementation
298-
/// of `CoerceUnsized<Bar<U>> for Bar<T>` must exist. The coercion will work by
299-
/// by coercing the `Bar<T>` field into `Bar<U>` and filling in the rest of the fields
300-
/// from `Foo<T>` to create a `Foo<U>`. This will effectively drill down to a pointer
301-
/// field and coerce that.
302-
///
303-
/// Generally, for smart pointers you will implement
304-
/// `CoerceUnsized<Ptr<U>> for Ptr<T> where T: Unsize<U>, U: ?Sized`, with an
305-
/// optional `?Sized` bound on `T` itself. For wrapper types that directly embed `T`
306-
/// like `Cell<T>` and `RefCell<T>`, you
307-
/// can directly implement `CoerceUnsized<Wrap<U>> for Wrap<T> where T: CoerceUnsized<U>`.
308-
/// This will let coercions of types like `Cell<Box<T>>` work.
309-
///
310-
/// [`Unsize`][unsize] is used to mark types which can be coerced to DSTs if behind
311-
/// pointers. It is implemented automatically by the compiler.
312-
///
313-
/// [dst-coerce]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
314-
/// [unsize]: ../marker/trait.Unsize.html
315-
/// [nomicon-coerce]: ../../nomicon/coercions.html
316-
#[unstable(feature = "coerce_unsized", issue = "27732")]
317-
#[lang="coerce_unsized"]
318-
pub trait CoerceUnsized<T> {
319-
// Empty.
320-
}
321-
322-
// &mut T -> &mut U
323-
#[unstable(feature = "coerce_unsized", issue = "27732")]
324-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
325-
// &mut T -> &U
326-
#[unstable(feature = "coerce_unsized", issue = "27732")]
327-
impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
328-
// &mut T -> *mut U
329-
#[unstable(feature = "coerce_unsized", issue = "27732")]
330-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
331-
// &mut T -> *const U
332-
#[unstable(feature = "coerce_unsized", issue = "27732")]
333-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
334-
335-
// &T -> &U
336-
#[unstable(feature = "coerce_unsized", issue = "27732")]
337-
impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
338-
// &T -> *const U
339-
#[unstable(feature = "coerce_unsized", issue = "27732")]
340-
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
341-
342-
// *mut T -> *mut U
343-
#[unstable(feature = "coerce_unsized", issue = "27732")]
344-
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
345-
// *mut T -> *const U
346-
#[unstable(feature = "coerce_unsized", issue = "27732")]
347-
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
348-
349-
// *const T -> *const U
350-
#[unstable(feature = "coerce_unsized", issue = "27732")]
351-
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}

src/libcore/ops/unsize.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use marker::Unsize;
12+
13+
/// Trait that indicates that this is a pointer or a wrapper for one,
14+
/// where unsizing can be performed on the pointee.
15+
///
16+
/// See the [DST coercion RfC][dst-coerce] and [the nomicon entry on coercion][nomicon-coerce]
17+
/// for more details.
18+
///
19+
/// For builtin pointer types, pointers to `T` will coerce to pointers to `U` if `T: Unsize<U>`
20+
/// by converting from a thin pointer to a fat pointer.
21+
///
22+
/// For custom types, the coercion here works by coercing `Foo<T>` to `Foo<U>`
23+
/// provided an impl of `CoerceUnsized<Foo<U>> for Foo<T>` exists.
24+
/// Such an impl can only be written if `Foo<T>` has only a single non-phantomdata
25+
/// field involving `T`. If the type of that field is `Bar<T>`, an implementation
26+
/// of `CoerceUnsized<Bar<U>> for Bar<T>` must exist. The coercion will work by
27+
/// by coercing the `Bar<T>` field into `Bar<U>` and filling in the rest of the fields
28+
/// from `Foo<T>` to create a `Foo<U>`. This will effectively drill down to a pointer
29+
/// field and coerce that.
30+
///
31+
/// Generally, for smart pointers you will implement
32+
/// `CoerceUnsized<Ptr<U>> for Ptr<T> where T: Unsize<U>, U: ?Sized`, with an
33+
/// optional `?Sized` bound on `T` itself. For wrapper types that directly embed `T`
34+
/// like `Cell<T>` and `RefCell<T>`, you
35+
/// can directly implement `CoerceUnsized<Wrap<U>> for Wrap<T> where T: CoerceUnsized<U>`.
36+
/// This will let coercions of types like `Cell<Box<T>>` work.
37+
///
38+
/// [`Unsize`][unsize] is used to mark types which can be coerced to DSTs if behind
39+
/// pointers. It is implemented automatically by the compiler.
40+
///
41+
/// [dst-coerce]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
42+
/// [unsize]: ../marker/trait.Unsize.html
43+
/// [nomicon-coerce]: ../../nomicon/coercions.html
44+
#[unstable(feature = "coerce_unsized", issue = "27732")]
45+
#[lang="coerce_unsized"]
46+
pub trait CoerceUnsized<T> {
47+
// Empty.
48+
}
49+
50+
// &mut T -> &mut U
51+
#[unstable(feature = "coerce_unsized", issue = "27732")]
52+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
53+
// &mut T -> &U
54+
#[unstable(feature = "coerce_unsized", issue = "27732")]
55+
impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
56+
// &mut T -> *mut U
57+
#[unstable(feature = "coerce_unsized", issue = "27732")]
58+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
59+
// &mut T -> *const U
60+
#[unstable(feature = "coerce_unsized", issue = "27732")]
61+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
62+
63+
// &T -> &U
64+
#[unstable(feature = "coerce_unsized", issue = "27732")]
65+
impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
66+
// &T -> *const U
67+
#[unstable(feature = "coerce_unsized", issue = "27732")]
68+
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
69+
70+
// *mut T -> *mut U
71+
#[unstable(feature = "coerce_unsized", issue = "27732")]
72+
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
73+
// *mut T -> *const U
74+
#[unstable(feature = "coerce_unsized", issue = "27732")]
75+
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
76+
77+
// *const T -> *const U
78+
#[unstable(feature = "coerce_unsized", issue = "27732")]
79+
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}

0 commit comments

Comments
 (0)