[go: up one dir, main page]

Coercion

Struct Coercion 

Source
#[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<F, T, U: ?Sized> Coercion<T, U, F>
where F: FnOnce(*const T) -> *const U,

Source

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

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)
};
Source§

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

Source

pub fn to_any() -> Self

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())
}
Source§

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

Source

pub fn to_debug() -> Self

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())
}
Source§

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

Source

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<T, const N: usize> Coercion<[T; N], [T]>

Source

pub fn to_slice() -> Self

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())
}
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>

Source

pub fn to_fn() -> Self

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)

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>

Source

pub fn to_fn_mut() -> Self

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)

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>

Source

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>

Source

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>

Source

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())
    }
}

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.