Struct unsize::Coercion[][src]

#[repr(C)]pub struct Coercion<T, U: ?Sized, F: FnOnce(*const T) -> *const U = fn(_: *const T) -> *const U> { /* fields omitted */ }

Enables the unsizing of a sized pointer.

Implementations

impl<F, T, U: ?Sized> Coercion<T, U, F> where
    F: FnOnce(*const T) -> *const U, 
[src]

pub unsafe fn new(coerce: F) -> Self[src]

Construct a new coercer.

Safety

The method must not perform any action other than unsizing the pointer.

Usage

use unsize::Coercion;
use core::fmt::Debug;

let c: Coercion<u32, dyn Debug> = unsafe {
    Coercion::new(|x| x)
};

impl<'lt, T: Any + 'lt> Coercion<T, dyn Any + 'lt>[src]

pub fn to_any() -> Self[src]

Create a coercer that unsizes and keeps dynamic type information.

Usage

use unsize::{Coercion, CoerceUnsize};
use core::any::Any;

fn generic<T: Any>(ptr: &T) -> &dyn Any {
    ptr.unsize(Coercion::to_any())
}

impl<'lt, T: Debug + 'lt> Coercion<T, dyn Debug + 'lt>[src]

pub fn to_debug() -> Self[src]

Create a coercer that unsizes a parameter to dynamically debug its fields.

Usage

use unsize::{Coercion, CoerceUnsize};
use core::fmt::Debug;

fn generic<T: Debug>(ptr: &T) -> &dyn Debug {
    ptr.unsize(Coercion::to_debug())
}

impl<'lt, T: Display + 'lt> Coercion<T, dyn Display + 'lt>[src]

pub fn to_display() -> Self[src]

Create a coercer that unsizes a parameter to display it.

Usage

use unsize::{Coercion, CoerceUnsize};
use core::fmt::Display;

fn generic<T: Display>(ptr: &T) -> &dyn Display {
    ptr.unsize(Coercion::to_display())
}

impl<T, const N: usize> Coercion<[T; N], [T]>[src]

pub fn to_slice() -> Self[src]

Create a coercer that unsizes an array to a slice.

Usage

use unsize::{Coercion, CoerceUnsize};
use core::fmt::Display;

fn generic<T>(ptr: &[T; 2]) -> &[T] {
    ptr.unsize(Coercion::to_slice())
}

impl<'lt, T: Fn(A, B, C, D, E, G) -> Ret + 'lt, Ret, A, B, C, D, E, G> Coercion<T, dyn Fn(A, B, C, D, E, G) -> Ret + 'lt>[src]

pub fn to_fn() -> Self[src]

Create a coercer that unsizes to a dynamically dispatched function.

This is implemented for function arities up to the shown one (other methods / impls are hidden in the docs for readability)

impl<'lt, T: FnMut(A, B, C, D, E, G) -> Ret + 'lt, Ret, A, B, C, D, E, G> Coercion<T, dyn FnMut(A, B, C, D, E, G) -> Ret + 'lt>[src]

pub fn to_fn_mut() -> Self[src]

Create a coercer that unsizes to a dynamically dispatched mutable function.

This is implemented for function arities up to the shown one (other methods / impls are hidden in the docs for readability)

impl<'lt, T: FnOnce(A, B, C, D, E, G) -> Ret + 'lt, Ret, A, B, C, D, E, G> Coercion<T, dyn FnOnce(A, B, C, D, E, G) -> Ret + 'lt>[src]

pub fn to_fn_once() -> Self[src]

Create a coercer that unsizes to a dynamically dispatched once function.

This is implemented for function arities up to the shown one (other methods / impls are hidden in the docs for readability)

impl<'lt, T: Iterator<Item = I> + 'lt, I> Coercion<T, dyn Iterator<Item = I> + 'lt>[src]

pub fn to_iterator() -> Self[src]

Create a coercer that unsizes to a dynamically dispatched iterator.

This is implemented for all iterator types. It can type-erase the concrete type to wrap an otherwise unnameable adapter in a custom smart pointer, to store it within a struct.

Usage

// A non-coercible box, for demonstration purposes
struct MyBox<T: ?Sized>(Box<T>);

unsafe impl<'lt, T, U: ?Sized + 'lt> CoerciblePtr<U> for MyBox<T> {
    // …
}

use unsize::{Coercion, CoerceUnsize};
use core::fmt::Display;

fn maybe_empty<T: Clone>(item: &T) -> MyBox<dyn Iterator<Item=T> + '_> {
    if core::mem::size_of::<T>() % 2 == 0 {
        MyBox::new(core::iter::empty())
            .unsize(Coercion::to_iterator())
    } else {
        MyBox::new(core::iter::repeat(item.clone()))
            .unsize(Coercion::to_iterator())
    }
}

impl<'lt, T: Future<Output = I> + 'lt, I> Coercion<T, dyn Future<Output = I> + 'lt>[src]

pub fn to_future() -> Self[src]

Create a coercer that unsizes to a dynamically dispatched future.

This is implemented for all iterator types. It can type-erase the concrete type to wrap an otherwise unnameable adapter in a custom smart pointer, to store it within a struct.

Usage

// A non-coercible box, for demonstration purposes
struct MyBox<T: ?Sized>(Box<T>);

unsafe impl<'lt, T, U: ?Sized + 'lt> CoerciblePtr<U> for MyBox<T> {
    // …
}

use unsize::{Coercion, CoerceUnsize};
use core::fmt::Display;

fn maybe_empty<T: 'static>(val: T) -> MyBox<dyn Future<Output=T>> {
    if core::mem::size_of::<T>() % 2 == 0 {
        MyBox::new(core::future::pending())
            .unsize(Coercion::to_future())
    } else {
        MyBox::new(core::future::ready(val))
            .unsize(Coercion::to_future())
    }
}

Trait Implementations

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> Clone for Coercion<T, U, F> where
    F: Clone
[src]

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> Copy for Coercion<T, U, F> where
    F: Copy
[src]

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> Debug for Coercion<T, U, F> where
    F: Debug
[src]

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> Eq for Coercion<T, U, F> where
    F: Eq
[src]

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> Hash for Coercion<T, U, F> where
    F: Hash
[src]

impl<T, U: ?Sized, F: FnOnce(*const T) -> *const U> PartialEq<Coercion<T, U, F>> for Coercion<T, U, F> where
    F: PartialEq
[src]

Auto Trait Implementations

impl<T, U: ?Sized, F> Send for Coercion<T, U, F> where
    F: Send

impl<T, U: ?Sized, F> Sync for Coercion<T, U, F> where
    F: Sync

impl<T, U: ?Sized, F> Unpin for Coercion<T, U, F> where
    F: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.