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

Gets the return type of a parameterless closure as a PhantomData

Example

use core_extensions::{IteratorExt, return_type_phantom};

use std::{
   collections::HashSet,
   iter::FromIterator,
   marker::PhantomData,
};

fn collect<I, F>(_: PhantomData<F>, iter: I) -> F
where
   I: IntoIterator,
   F: FromIterator<I::Item>
{
   iter.into_iter().collect()
}

let ty = return_type_phantom!(||{
   let mut set = HashSet::new();
   set.insert(100);
   set
});

// `set` is a `HashSet<i32>`
let set = collect(ty, 1..=10);

assert_eq!(set.into_iter().sum_same(), 55);

Const callable

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

use core_extensions::return_type_phantom;

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(return_type_phantom!(|| tup.2[0] ))
};

assert_eq!(SIZE, 16);