Add CoerceSized trait and lang item
This trait is more-or-less the reverse of CoerceUnsized, and will be used for object-safety checks. Receiver types like `Rc` will have to implement `CoerceSized` so that methods that use `Rc<Self>` as the receiver will be considered object-safe.
This commit is contained in:
parent
a920036f4f
commit
be80a79a1e
3 changed files with 36 additions and 1 deletions
|
|
@ -201,3 +201,6 @@ pub use self::generator::{Generator, GeneratorState};
|
|||
|
||||
#[unstable(feature = "coerce_unsized", issue = "27732")]
|
||||
pub use self::unsize::CoerceUnsized;
|
||||
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
pub use self::unsize::CoerceSized;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ use marker::Unsize;
|
|||
/// [nomicon-coerce]: ../../nomicon/coercions.html
|
||||
#[unstable(feature = "coerce_unsized", issue = "27732")]
|
||||
#[lang = "coerce_unsized"]
|
||||
pub trait CoerceUnsized<T> {
|
||||
pub trait CoerceUnsized<T: ?Sized> {
|
||||
// Empty.
|
||||
}
|
||||
|
||||
|
|
@ -77,3 +77,34 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
|
|||
// *const T -> *const U
|
||||
#[unstable(feature = "coerce_unsized", issue = "27732")]
|
||||
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
|
||||
|
||||
|
||||
/// Pointers to unsized types that can be coerced to a pointer to a sized type,
|
||||
/// as long as pointee is actually a value of that sized type. This is used for
|
||||
/// object safety, to check that a method's receiver type can be coerced from the version
|
||||
/// where `Self = dyn Trait` to the version where `Self = T`, the erased, sized type
|
||||
/// of the underlying object.
|
||||
///
|
||||
/// CoerceSized is implemented for:
|
||||
/// - &[T] is CoerceSized<&[T; N]> for any N
|
||||
/// - &Trait is CoerceSized<&T> for any T: Trait
|
||||
/// - and similarly for &mut T, *const T, *mut T, Box<T>, Rc<T>, Arc<T>
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
#[cfg_attr(not(stage0), lang = "coerce_sized")]
|
||||
pub trait CoerceSized<T> where T: CoerceUnsized<Self> {
|
||||
// Empty.
|
||||
}
|
||||
|
||||
// &U -> &T
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<&'a T> for &'a U {}
|
||||
// &mut U -> &mut T
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<&'a mut T> for &'a mut U {}
|
||||
// *const U -> *const T
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<*const T> for *const U {}
|
||||
// *mut U -> *mut T
|
||||
#[unstable(feature = "coerce_sized", issue = "0")]
|
||||
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceSized<*mut T> for *mut U {}
|
||||
|
||||
|
|
|
|||
|
|
@ -271,6 +271,7 @@ language_item_table! {
|
|||
DropTraitLangItem, "drop", drop_trait, Target::Trait;
|
||||
|
||||
CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait;
|
||||
CoerceSizedTraitLangItem, "coerce_sized", coerce_sized_trait, Target::Trait;
|
||||
|
||||
AddTraitLangItem, "add", add_trait, Target::Trait;
|
||||
SubTraitLangItem, "sub", sub_trait, Target::Trait;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue