#[repr(C)]pub struct Coercion<T, U: ?Sized, F: FnOnce(*const T) -> *const U = fn(*const T) -> *const U> { /* private fields */ }Expand description
Enables the unsizing of a sized pointer.
Implementations§
Source§impl<'lt, T: Display + 'lt> Coercion<T, dyn Display + 'lt>
impl<'lt, T: Display + 'lt> Coercion<T, dyn Display + 'lt>
Sourcepub fn to_display() -> Self
pub fn to_display() -> Self
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())
}Source§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>
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>
Source§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>
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>
Source§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>
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>
Sourcepub fn to_fn_once() -> Self
pub fn to_fn_once() -> Self
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)
Source§impl<'lt, T: Iterator<Item = I> + 'lt, I> Coercion<T, dyn Iterator<Item = I> + 'lt>
impl<'lt, T: Iterator<Item = I> + 'lt, I> Coercion<T, dyn Iterator<Item = I> + 'lt>
Sourcepub fn to_iterator() -> Self
pub fn to_iterator() -> Self
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())
}
}Source§impl<'lt, T: Future<Output = I> + 'lt, I> Coercion<T, dyn Future<Output = I> + 'lt>
impl<'lt, T: Future<Output = I> + 'lt, I> Coercion<T, dyn Future<Output = I> + 'lt>
Sourcepub fn to_future() -> Self
pub fn to_future() -> Self
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())
}
}