macro_rules! map_phantomdata {
    ($expr:expr, $closure:expr) => { ... };
}
Available on crate feature phantom only.
Expand description

Maps a PhantomData<T> to a PhantomData<U> by using a FnOnce(T) -> U closure.

Example

use core_extensions::{as_phantom, map_phantomdata};

use std::{
    borrow::Borrow,
    fmt::Debug,
    marker::PhantomData,
};

fn assert_impls<T>(_: PhantomData<T>) 
where
    T: AsRef<str> + Borrow<str> + Debug
{}

let tuple = (100, ["hello"]);

// ghost is a `PhantomData<&'static str>`
let ghost = map_phantomdata!(as_phantom(&tuple), |x| x.1[0] );

assert_impls(ghost);

Const callable

This macro works in constants, but not in const fns (as of Rust 1.51.0).

use core_extensions::{as_phantom, map_phantomdata};

use std::marker::PhantomData;
 
const fn size_of_phantom<T>(_: PhantomData<T>) -> usize {
    std::mem::size_of::<T>()
}

const SIZE: usize = {
    let tup = (0u8, 116, [3u128, 4]);

    size_of_phantom(map_phantomdata!(as_phantom(&tup), |x| x.2[0] ))
};

assert_eq!(SIZE, 16);