From: Michael Hewson Date: Thu, 20 Sep 2018 07:18:00 +0000 (-0400) Subject: Add CoerceSized impls throughout libstd X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=192900e7c2792b2afafb055ddba530252ce5c0a3;p=rust.git Add CoerceSized impls throughout libstd This will make receiver types like `Rc` and `Pin<&mut Self>` object-safe. --- diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f989e701913..8a4e646f278 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -77,7 +77,7 @@ use core::marker::{Unpin, Unsize}; use core::mem; use core::pin::Pin; -use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState}; +use core::ops::{CoerceUnsized, CoerceSized, Deref, DerefMut, Generator, GeneratorState}; use core::ptr::{self, NonNull, Unique}; use core::task::{LocalWaker, Poll}; @@ -696,6 +696,9 @@ extern "rust-call" fn call_once(self, args: A) -> R { #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Box {} +#[unstable(feature = "coerce_sized", issue = "0")] +impl, U: ?Sized> CoerceSized> for Box {} + #[stable(feature = "box_slice_clone", since = "1.3.0")] impl Clone for Box<[T]> { fn clone(&self) -> Self { diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 7db6261a01c..93c340a6dbd 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -86,6 +86,7 @@ #![feature(box_syntax)] #![feature(cfg_target_has_atomic)] #![feature(coerce_unsized)] +#![feature(coerce_sized)] #![feature(core_intrinsics)] #![feature(custom_attribute)] #![feature(dropck_eyepatch)] diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 3e8dfd105de..c1cc7ac976d 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -255,7 +255,7 @@ use core::marker::{Unpin, Unsize, PhantomData}; use core::mem::{self, align_of_val, forget, size_of_val}; use core::ops::Deref; -use core::ops::CoerceUnsized; +use core::ops::{CoerceUnsized, CoerceSized}; use core::pin::Pin; use core::ptr::{self, NonNull}; use core::convert::From; @@ -297,6 +297,9 @@ impl !marker::Sync for Rc {} #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Rc {} +#[unstable(feature = "coerce_sized", issue = "0")] +impl, U: ?Sized> CoerceSized> for Rc {} + impl Rc { /// Constructs a new `Rc`. /// @@ -1176,6 +1179,9 @@ impl !marker::Sync for Weak {} #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Weak {} +#[unstable(feature = "coerce_sized", issue = "0")] +impl, U: ?Sized> CoerceSized> for Weak {} + impl Weak { /// Constructs a new `Weak`, without allocating any memory. /// Calling [`upgrade`][Weak::upgrade] on the return value always gives [`None`]. diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index bcf5212f1ff..358e18bbe1b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -25,7 +25,7 @@ use core::intrinsics::abort; use core::mem::{self, align_of_val, size_of_val}; use core::ops::Deref; -use core::ops::CoerceUnsized; +use core::ops::{CoerceUnsized, CoerceSized}; use core::pin::Pin; use core::ptr::{self, NonNull}; use core::marker::{Unpin, Unsize, PhantomData}; @@ -214,6 +214,9 @@ unsafe impl Sync for Arc {} #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Arc {} +#[unstable(feature = "coerce_sized", issue = "0")] +impl, U: ?Sized> CoerceSized> for Arc {} + /// `Weak` is a version of [`Arc`] that holds a non-owning reference to the /// managed value. The value is accessed by calling [`upgrade`] on the `Weak` /// pointer, which returns an [`Option`]`<`[`Arc`]`>`. @@ -254,6 +257,8 @@ unsafe impl Sync for Weak {} #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Weak {} +#[unstable(feature = "coerce_sized", issue = "0")] +impl, U: ?Sized> CoerceSized> for Weak {} #[stable(feature = "arc_weak", since = "1.4.0")] impl fmt::Debug for Weak { diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 118e75e1ee7..2bc6c36171c 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -10,7 +10,7 @@ //! Exposes the NonZero lang item which provides optimization hints. -use ops::CoerceUnsized; +use ops::{CoerceUnsized, CoerceSized}; /// A wrapper type for raw pointers and integers that will never be /// NULL or 0 that might allow certain optimizations. @@ -20,3 +20,5 @@ pub(crate) struct NonZero(pub(crate) T); impl, U> CoerceUnsized> for NonZero {} + +impl, U: CoerceSized> CoerceSized> for NonZero {} diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index a03c080fb3f..87ca193aeee 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -91,7 +91,7 @@ use fmt; use marker::Sized; -use ops::{Deref, DerefMut, CoerceUnsized}; +use ops::{Deref, DerefMut, CoerceUnsized, CoerceSized}; #[doc(inline)] pub use marker::Unpin; @@ -324,5 +324,12 @@ impl CoerceUnsized> for Pin

P: CoerceUnsized, {} +#[unstable(feature = "pin", issue = "49150")] +impl<'a, P, U> CoerceSized> for Pin +where + P: CoerceUnsized, + U: CoerceSized

, +{} + #[unstable(feature = "pin", issue = "49150")] impl

Unpin for Pin

{} diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 36852b10fac..24ef028b49d 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -75,7 +75,7 @@ use convert::From; use intrinsics; -use ops::CoerceUnsized; +use ops::{CoerceUnsized, CoerceSized}; use fmt; use hash; use marker::{PhantomData, Unsize}; @@ -2795,6 +2795,9 @@ impl Copy for Unique { } #[unstable(feature = "ptr_internals", issue = "0")] impl CoerceUnsized> for Unique where T: Unsize { } +#[unstable(feature = "ptr_internals", issue = "0")] +impl CoerceSized> for Unique where T: Unsize { } + #[unstable(feature = "ptr_internals", issue = "0")] impl fmt::Pointer for Unique { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -2951,6 +2954,9 @@ impl Copy for NonNull { } #[unstable(feature = "coerce_unsized", issue = "27732")] impl CoerceUnsized> for NonNull where T: Unsize { } +#[unstable(feature = "coerce_sized", issue = "0")] +impl CoerceSized> for NonNull where T: Unsize { } + #[stable(feature = "nonnull", since = "1.25.0")] impl fmt::Debug for NonNull { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {