Skip to content

Commit 192900e

Browse files
committed
Add CoerceSized impls throughout libstd
This will make receiver types like `Rc<Self>` and `Pin<&mut Self>` object-safe.
1 parent 9f59da2 commit 192900e

File tree

7 files changed

+36
-6
lines changed

7 files changed

+36
-6
lines changed

src/liballoc/boxed.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use core::iter::FusedIterator;
7777
use core::marker::{Unpin, Unsize};
7878
use core::mem;
7979
use core::pin::Pin;
80-
use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
80+
use core::ops::{CoerceUnsized, CoerceSized, Deref, DerefMut, Generator, GeneratorState};
8181
use core::ptr::{self, NonNull, Unique};
8282
use core::task::{LocalWaker, Poll};
8383

@@ -696,6 +696,9 @@ impl<'a, A, R> FnOnce<A> for Box<dyn FnBox<A, Output = R> + Send + 'a> {
696696
#[unstable(feature = "coerce_unsized", issue = "27732")]
697697
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
698698

699+
#[unstable(feature = "coerce_sized", issue = "0")]
700+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Box<T>> for Box<U> {}
701+
699702
#[stable(feature = "box_slice_clone", since = "1.3.0")]
700703
impl<T: Clone> Clone for Box<[T]> {
701704
fn clone(&self) -> Self {

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#![feature(box_syntax)]
8787
#![feature(cfg_target_has_atomic)]
8888
#![feature(coerce_unsized)]
89+
#![feature(coerce_sized)]
8990
#![feature(core_intrinsics)]
9091
#![feature(custom_attribute)]
9192
#![feature(dropck_eyepatch)]

src/liballoc/rc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ use core::marker;
255255
use core::marker::{Unpin, Unsize, PhantomData};
256256
use core::mem::{self, align_of_val, forget, size_of_val};
257257
use core::ops::Deref;
258-
use core::ops::CoerceUnsized;
258+
use core::ops::{CoerceUnsized, CoerceSized};
259259
use core::pin::Pin;
260260
use core::ptr::{self, NonNull};
261261
use core::convert::From;
@@ -297,6 +297,9 @@ impl<T: ?Sized> !marker::Sync for Rc<T> {}
297297
#[unstable(feature = "coerce_unsized", issue = "27732")]
298298
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
299299

300+
#[unstable(feature = "coerce_sized", issue = "0")]
301+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Rc<T>> for Rc<U> {}
302+
300303
impl<T> Rc<T> {
301304
/// Constructs a new `Rc<T>`.
302305
///
@@ -1176,6 +1179,9 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
11761179
#[unstable(feature = "coerce_unsized", issue = "27732")]
11771180
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
11781181

1182+
#[unstable(feature = "coerce_sized", issue = "0")]
1183+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Weak<T>> for Weak<U> {}
1184+
11791185
impl<T> Weak<T> {
11801186
/// Constructs a new `Weak<T>`, without allocating any memory.
11811187
/// Calling [`upgrade`][Weak::upgrade] on the return value always gives [`None`].

src/liballoc/sync.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::cmp::Ordering;
2525
use core::intrinsics::abort;
2626
use core::mem::{self, align_of_val, size_of_val};
2727
use core::ops::Deref;
28-
use core::ops::CoerceUnsized;
28+
use core::ops::{CoerceUnsized, CoerceSized};
2929
use core::pin::Pin;
3030
use core::ptr::{self, NonNull};
3131
use core::marker::{Unpin, Unsize, PhantomData};
@@ -214,6 +214,9 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
214214
#[unstable(feature = "coerce_unsized", issue = "27732")]
215215
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
216216

217+
#[unstable(feature = "coerce_sized", issue = "0")]
218+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Arc<T>> for Arc<U> {}
219+
217220
/// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
218221
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
219222
/// pointer, which returns an [`Option`]`<`[`Arc`]`<T>>`.
@@ -254,6 +257,8 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Weak<T> {}
254257

255258
#[unstable(feature = "coerce_unsized", issue = "27732")]
256259
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
260+
#[unstable(feature = "coerce_sized", issue = "0")]
261+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceSized<Weak<T>> for Weak<U> {}
257262

258263
#[stable(feature = "arc_weak", since = "1.4.0")]
259264
impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {

src/libcore/nonzero.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Exposes the NonZero lang item which provides optimization hints.
1212
13-
use ops::CoerceUnsized;
13+
use ops::{CoerceUnsized, CoerceSized};
1414

1515
/// A wrapper type for raw pointers and integers that will never be
1616
/// NULL or 0 that might allow certain optimizations.
@@ -20,3 +20,5 @@ use ops::CoerceUnsized;
2020
pub(crate) struct NonZero<T>(pub(crate) T);
2121

2222
impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {}
23+
24+
impl<T: CoerceUnsized<U>, U: CoerceSized<T>> CoerceSized<NonZero<T>> for NonZero<U> {}

src/libcore/pin.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
use fmt;
9393
use marker::Sized;
94-
use ops::{Deref, DerefMut, CoerceUnsized};
94+
use ops::{Deref, DerefMut, CoerceUnsized, CoerceSized};
9595

9696
#[doc(inline)]
9797
pub use marker::Unpin;
@@ -324,5 +324,12 @@ where
324324
P: CoerceUnsized<U>,
325325
{}
326326

327+
#[unstable(feature = "pin", issue = "49150")]
328+
impl<'a, P, U> CoerceSized<Pin<P>> for Pin<U>
329+
where
330+
P: CoerceUnsized<U>,
331+
U: CoerceSized<P>,
332+
{}
333+
327334
#[unstable(feature = "pin", issue = "49150")]
328335
impl<P> Unpin for Pin<P> {}

src/libcore/ptr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
use convert::From;
7777
use intrinsics;
78-
use ops::CoerceUnsized;
78+
use ops::{CoerceUnsized, CoerceSized};
7979
use fmt;
8080
use hash;
8181
use marker::{PhantomData, Unsize};
@@ -2795,6 +2795,9 @@ impl<T: ?Sized> Copy for Unique<T> { }
27952795
#[unstable(feature = "ptr_internals", issue = "0")]
27962796
impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> { }
27972797

2798+
#[unstable(feature = "ptr_internals", issue = "0")]
2799+
impl<T: ?Sized, U: ?Sized> CoerceSized<Unique<T>> for Unique<U> where T: Unsize<U> { }
2800+
27982801
#[unstable(feature = "ptr_internals", issue = "0")]
27992802
impl<T: ?Sized> fmt::Pointer for Unique<T> {
28002803
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -2951,6 +2954,9 @@ impl<T: ?Sized> Copy for NonNull<T> { }
29512954
#[unstable(feature = "coerce_unsized", issue = "27732")]
29522955
impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
29532956

2957+
#[unstable(feature = "coerce_sized", issue = "0")]
2958+
impl<T: ?Sized, U: ?Sized> CoerceSized<NonNull<T>> for NonNull<U> where T: Unsize<U> { }
2959+
29542960
#[stable(feature = "nonnull", since = "1.25.0")]
29552961
impl<T: ?Sized> fmt::Debug for NonNull<T> {
29562962
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)