pub trait CoerceUnsized<T> { }
Trait that indicates that this is a pointer or a wrapper for one, where unsizing can be performed on the pointee.
See the DST coercion RfC and the nomicon entry on coercion for more details.
For builtin pointer types, pointers to T
will coerce to pointers to U
if T: Unsize<U>
by converting from a thin pointer to a fat pointer.
For custom types, the coercion here works by coercing Foo<T>
to Foo<U>
provided an impl of CoerceUnsized<Foo<U>> for Foo<T>
exists. Such an impl can only be written if Foo<T>
has only a single non-phantomdata field involving T
. If the type of that field is Bar<T>
, an implementation of CoerceUnsized<Bar<U>> for Bar<T>
must exist. The coercion will work by by coercing the Bar<T>
field into Bar<U>
and filling in the rest of the fields from Foo<T>
to create a Foo<U>
. This will effectively drill down to a pointer field and coerce that.
Generally, for smart pointers you will implement CoerceUnsized<Ptr<U>> for Ptr<T> where T: Unsize<U>, U: ?Sized
, with an optional ?Sized
bound on T
itself. For wrapper types that directly embed T
like Cell<T>
and RefCell<T>
, you can directly implement CoerceUnsized<Wrap<U>> for Wrap<T> where T: CoerceUnsized<U>
. This will let coercions of types like Cell<Box<T>>
work.
Unsize
is used to mark types which can be coerced to DSTs if behind pointers. It is implemented automatically by the compiler.
impl<T, U> CoerceUnsized<Box<U>> for Box<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Arc<U>> for Arc<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Weak<U>> for std::sync::Weak<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Rc<U>> for Rc<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Weak<U>> for std::rc::Weak<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<NonZero<U>> for NonZero<T> where T: Zeroable + CoerceUnsized<U>,
        U: Zeroable
impl<T, U> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Shared<U>> for Shared<T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, T, U> CoerceUnsized<&'a mut U> for &'a mut T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b mut T where 'b: 'a,
        T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, T, U> CoerceUnsized<*mut U> for &'a mut T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, T, U> CoerceUnsized<*const U> for &'a mut T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b T where 'b: 'a,
        T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'a, T, U> CoerceUnsized<*const U> for &'a T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<*mut U> for *mut T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<*const U> for *mut T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<*const U> for *const T where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<Cell<U>> for Cell<T> where T: CoerceUnsized<U>
impl<T, U> CoerceUnsized<RefCell<U>> for RefCell<T> where T: CoerceUnsized<U>
impl<'b, T, U> CoerceUnsized<Ref<'b, U>> for Ref<'b, T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<'b, T, U> CoerceUnsized<RefMut<'b, U>> for RefMut<'b, T> where T: Unsize<U> + ?Sized,
        U: ?Sized
impl<T, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> where T: CoerceUnsized<U>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html