// run-pass use std::borrow::Cow; use std::iter::FromIterator; use std::ops::Add; use std::option::IntoIter as OptionIter; pub struct XorShiftRng; use XorShiftRng as DummyRng; impl Rng for XorShiftRng {} pub trait Rng {} pub trait Rand: Default + Sized { fn rand(_rng: &mut R) -> Self { Default::default() } } impl Rand for i32 { } pub trait IntoCow<'a, B: ?Sized> where B: ToOwned { fn into_cow(self) -> Cow<'a, B>; } impl<'a> IntoCow<'a, str> for String { fn into_cow(self) -> Cow<'a, str> { Cow::Owned(self) } } #[derive(PartialEq, Eq)] struct Newt(T); fn id(x: T) -> T { x } fn eq(a: T, b: T) -> bool { a == b } fn u8_as_i8(x: u8) -> i8 { x as i8 } fn odd(x: usize) -> bool { x % 2 == 1 } fn dummy_rng() -> DummyRng { XorShiftRng } trait Size: Sized { fn size() -> usize { std::mem::size_of::() } } impl Size for T {} #[derive(PartialEq, Eq)] struct BitVec; impl BitVec { fn from_fn(_: usize, _: F) -> BitVec where F: FnMut(usize) -> bool { BitVec } } #[derive(PartialEq, Eq)] struct Foo(T); impl Foo { fn map_in_place(self, mut f: F) -> Foo where F: FnMut(T) -> U { Foo(f(self.0)) } } macro_rules! tests { ($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({ const C: $ty = $expr; static S: $ty = $expr; assert!(eq(C($($test),*), $expr($($test),*))); assert!(eq(S($($test),*), $expr($($test),*))); assert!(eq(C($($test),*), S($($test),*))); })+}) } tests! { // Free function. id, fn(i32) -> i32, (5); id::, fn(i32) -> i32, (5); // Enum variant constructor. Some, fn(i32) -> Option, (5); Some::, fn(i32) -> Option, (5); // Tuple struct constructor. Newt, fn(i32) -> Newt, (5); Newt::, fn(i32) -> Newt, (5); // Inherent static methods. Vec::new, fn() -> Vec<()>, (); Vec::<()>::new, fn() -> Vec<()>, (); >::new, fn() -> Vec<()>, (); Vec::with_capacity, fn(usize) -> Vec<()>, (5); Vec::<()>::with_capacity, fn(usize) -> Vec<()>, (5); >::with_capacity, fn(usize) -> Vec<()>, (5); BitVec::from_fn, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd); BitVec::from_fn:: bool>, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd); // Inherent non-static method. Foo::map_in_place, fn(Foo, fn(u8) -> i8) -> Foo, (Foo(b'f'), u8_as_i8); Foo::map_in_place:: i8>, fn(Foo, fn(u8) -> i8) -> Foo, (Foo(b'f'), u8_as_i8); Foo::::map_in_place, fn(Foo, fn(u8) -> i8) -> Foo , (Foo(b'f'), u8_as_i8); Foo::::map_in_place:: i8>, fn(Foo, fn(u8) -> i8) -> Foo , (Foo(b'f'), u8_as_i8); // Trait static methods. bool::size, fn() -> usize, (); ::size, fn() -> usize, (); ::size, fn() -> usize, (); Default::default, fn() -> i32, (); i32::default, fn() -> i32, (); ::default, fn() -> i32, (); ::default, fn() -> i32, (); Rand::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); i32::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); ::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); ::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); Rand::rand::, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); i32::rand::, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); ::rand::, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); ::rand::, fn(&mut DummyRng) -> i32, (&mut dummy_rng()); // Trait non-static methods. Clone::clone, fn(&i32) -> i32, (&5); i32::clone, fn(&i32) -> i32, (&5); ::clone, fn(&i32) -> i32, (&5); ::clone, fn(&i32) -> i32, (&5); FromIterator::from_iter, fn(OptionIter) -> Vec, (Some(5).into_iter()); Vec::from_iter, fn(OptionIter) -> Vec, (Some(5).into_iter()); >::from_iter, fn(OptionIter) -> Vec, (Some(5).into_iter()); as FromIterator<_>>::from_iter, fn(OptionIter) -> Vec, (Some(5).into_iter()); as FromIterator<_>>::from_iter, fn(OptionIter) -> Vec, (Some(5).into_iter()); FromIterator::from_iter::>, fn(OptionIter) -> Vec, (Some(5).into_iter()); as FromIterator<_>>::from_iter::>, fn(OptionIter) -> Vec, (Some(5).into_iter()); Add::add, fn(i32, i32) -> i32, (5, 6); i32::add, fn(i32, i32) -> i32, (5, 6); ::add, fn(i32, i32) -> i32, (5, 6); >::add, fn(i32, i32) -> i32, (5, 6); >::add, fn(i32, i32) -> i32, (5, 6); String::into_cow, fn(String) -> Cow<'static, str>, ("foo".to_string()); ::into_cow, fn(String) -> Cow<'static, str>, ("foo".to_string()); >::into_cow, fn(String) -> Cow<'static, str>, ("foo".to_string()); >::into_cow, fn(String) -> Cow<'static, str>, ("foo".to_string()); }