core::ops::CoerceUnsized - Rust
Description: API documentation for the Rust `CoerceUnsized` trait in crate `core`.
Keywords: rust, rustlang, rust-lang, CoerceUnsized

# [−][src]Trait core::ops::CoerceUnsized

    #[lang = "coerce_unsized"]pub trait CoerceUnsized<T: ?Sized> { }

🔬 This is a nightly-only experimental API. (`coerce_unsized` #27732)

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 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.

## Implementors

### `impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T`[src]

### `impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T`[src]

### `impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T`[src]

### `impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T`[src]

### `impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T`[src]

### `impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T`[src]

### `impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Ref<'b, U>> for Ref<'b, T>`[src]

### `impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RefMut<'b, U>> for RefMut<'b, T>`[src]

### `impl<P, U> CoerceUnsized<Pin<U>> for Pin<P> where
P: CoerceUnsized<U>,` [src]

### `impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T>`[src]

### `impl<T: CoerceUnsized<U>, U> CoerceUnsized<RefCell<U>> for RefCell<T>`[src]

### `impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T>`[src]

### `impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T`[src]

### `impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T`[src]

### `impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T`[src]

### `impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where
T: Unsize<U>,` [src]

Loading content...