mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
fix rpass tests
This commit is contained in:
parent
8d0d7521d6
commit
ca17d08126
@ -11,12 +11,12 @@
|
||||
#![crate_name="cci_impl_lib"]
|
||||
|
||||
pub trait uint_helpers {
|
||||
fn to(&self, v: uint, f: |uint|);
|
||||
fn to<F>(&self, v: uint, f: F) where F: FnMut(uint);
|
||||
}
|
||||
|
||||
impl uint_helpers for uint {
|
||||
#[inline]
|
||||
fn to(&self, v: uint, f: |uint|) {
|
||||
fn to<F>(&self, v: uint, mut f: F) where F: FnMut(uint) {
|
||||
let mut i = *self;
|
||||
while i < v {
|
||||
f(i);
|
||||
|
@ -11,7 +11,7 @@
|
||||
#![crate_name="cci_iter_lib"]
|
||||
|
||||
#[inline]
|
||||
pub fn iter<T>(v: &[T], f: |&T|) {
|
||||
pub fn iter<T, F>(v: &[T], mut f: F) where F: FnMut(&T) {
|
||||
let mut i = 0u;
|
||||
let n = v.len();
|
||||
while i < n {
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
|
||||
// same as cci_iter_lib, more-or-less, but not marked inline
|
||||
pub fn iter(v: Vec<uint> , f: |uint|) {
|
||||
pub fn iter<F>(v: Vec<uint> , mut f: F) where F: FnMut(uint) {
|
||||
let mut i = 0u;
|
||||
let n = v.len();
|
||||
while i < n {
|
||||
|
@ -12,12 +12,12 @@
|
||||
|
||||
// part of issue-6919.rs
|
||||
|
||||
pub struct C<'a> {
|
||||
pub k: ||: 'a,
|
||||
pub struct C<K> where K: FnOnce() {
|
||||
pub k: K,
|
||||
}
|
||||
|
||||
fn no_op() { }
|
||||
pub const D : C<'static> = C {
|
||||
k: no_op
|
||||
pub const D : C<fn()> = C {
|
||||
k: no_op as fn()
|
||||
};
|
||||
|
||||
|
@ -21,7 +21,6 @@ pub mod testtypes {
|
||||
ids.push(TypeId::of::<FooEnum>());
|
||||
ids.push(TypeId::of::<FooUniq>());
|
||||
ids.push(TypeId::of::<FooPtr>());
|
||||
ids.push(TypeId::of::<FooClosure>());
|
||||
ids.push(TypeId::of::<&'static FooTrait>());
|
||||
ids.push(TypeId::of::<FooStruct>());
|
||||
ids.push(TypeId::of::<FooTuple>());
|
||||
@ -68,9 +67,6 @@ pub mod testtypes {
|
||||
|
||||
// Skipping ty_bare_fn (how do you get a bare function type, rather than proc or closure?)
|
||||
|
||||
// Tests ty_closure (does not test all types of closures)
|
||||
pub type FooClosure = |arg: u8|: 'static -> u8;
|
||||
|
||||
// Tests ty_trait
|
||||
pub trait FooTrait {
|
||||
fn foo_method(&self) -> uint;
|
||||
|
@ -13,5 +13,5 @@
|
||||
|
||||
pub fn foo<T>() {
|
||||
fn death() -> int { panic!() }
|
||||
debug!("{}", (||{ death() })());
|
||||
debug!("{}", (|&:|{ death() })());
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ fn f1(a: &mut X, b: &mut int, c: int) -> int {
|
||||
return r;
|
||||
}
|
||||
|
||||
fn f2(a: int, f: |int|) -> int { f(1); return a; }
|
||||
fn f2<F>(a: int, f: F) -> int where F: FnOnce(int) { f(1); return a; }
|
||||
|
||||
pub fn main() {
|
||||
let mut a = X {x: 1};
|
||||
|
@ -11,11 +11,11 @@
|
||||
|
||||
fn f<T>(x: Vec<T>) -> T { return x.into_iter().next().unwrap(); }
|
||||
|
||||
fn g(act: |Vec<int> | -> int) -> int { return act(vec!(1, 2, 3)); }
|
||||
fn g<F>(act: F) -> int where F: FnOnce(Vec<int>) -> int { return act(vec!(1, 2, 3)); }
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(g(f), 1);
|
||||
let f1: |Vec<String>| -> String = f;
|
||||
let f1 = f;
|
||||
assert_eq!(f1(vec!["x".to_string(), "y".to_string(), "z".to_string()]),
|
||||
"x".to_string());
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn asBlock(f: || -> uint) -> uint {
|
||||
fn asBlock<F>(f: F) -> uint where F: FnOnce() -> uint {
|
||||
return f();
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,6 @@
|
||||
// except according to those terms.
|
||||
|
||||
pub fn main() {
|
||||
fn as_buf<T>(s: String, f: |String| -> T) -> T { f(s) }
|
||||
fn as_buf<T, F>(s: String, f: F) -> T where F: FnOnce(String) -> T { f(s) }
|
||||
as_buf("foo".to_string(), |foo: String| -> () println!("{}", foo) );
|
||||
}
|
||||
|
@ -8,10 +8,11 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn force(f: || -> int) -> int { return f(); }
|
||||
fn force<F>(f: F) -> int where F: FnOnce() -> int { return f(); }
|
||||
|
||||
pub fn main() {
|
||||
fn f() -> int { return 7; }
|
||||
assert_eq!(force(f), 7);
|
||||
let g = {||force(f)};
|
||||
let g = {|&:|force(f)};
|
||||
assert_eq!(g(), 7);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn iter_vec<T>(v: Vec<T> , f: |&T|) { for x in v.iter() { f(x); } }
|
||||
fn iter_vec<T, F>(v: Vec<T> , mut f: F) where F: FnMut(&T) { for x in v.iter() { f(x); } }
|
||||
|
||||
pub fn main() {
|
||||
let v = vec!(1i, 2, 3, 4, 5, 6, 7);
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn iter_vec<T>(v: Vec<T> , f: |&T|) { for x in v.iter() { f(x); } }
|
||||
fn iter_vec<T, F>(v: Vec<T>, mut f: F) where F: FnMut(&T) { for x in v.iter() { f(x); } }
|
||||
|
||||
pub fn main() {
|
||||
let v = vec!(1i, 2, 3, 4, 5);
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
fn borrow(x: &int, f: |x: &int|) {
|
||||
fn borrow<F>(x: &int, f: F) where F: FnOnce(&int) {
|
||||
f(x)
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,10 @@
|
||||
// the closures are in scope. Issue #6801.
|
||||
|
||||
fn a() -> int {
|
||||
let mut x = 3;
|
||||
let mut x = 3i;
|
||||
x += 1;
|
||||
let c1 = || x * 4;
|
||||
let c2 = || x * 5;
|
||||
let c1 = |&:| x * 4;
|
||||
let c2 = |&:| x * 5;
|
||||
c1() * c2() * x
|
||||
}
|
||||
|
||||
@ -29,16 +29,16 @@ fn get(x: &int) -> int {
|
||||
fn b() -> int {
|
||||
let mut x = 3;
|
||||
x += 1;
|
||||
let c1 = || get(&x);
|
||||
let c2 = || get(&x);
|
||||
let c1 = |&:| get(&x);
|
||||
let c2 = |&:| get(&x);
|
||||
c1() * c2() * x
|
||||
}
|
||||
|
||||
fn c() -> int {
|
||||
let mut x = 3;
|
||||
x += 1;
|
||||
let c1 = || x * 5;
|
||||
let c2 = || get(&x);
|
||||
let c1 = |&:| x * 5;
|
||||
let c2 = |&:| get(&x);
|
||||
c1() * c2() * x
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ fn add_int(x: &mut Ints, v: int) {
|
||||
swap(&mut values, &mut x.values);
|
||||
}
|
||||
|
||||
fn iter_ints(x: &Ints, f: |x: &int| -> bool) -> bool {
|
||||
fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&int) -> bool {
|
||||
let l = x.values.len();
|
||||
range(0u, l).all(|i| f(&x.values[i]))
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// ignore-test FIXME(japaric) this ICEs
|
||||
|
||||
fn foo() -> int { 22 }
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn each<T>(x: &[T], f: |&T|) {
|
||||
fn each<T, F>(x: &[T], mut f: F) where F: FnMut(&T) {
|
||||
for val in x.iter() {
|
||||
f(val)
|
||||
}
|
||||
|
@ -11,9 +11,9 @@
|
||||
|
||||
fn foo(i: int) -> int { i + 1 }
|
||||
|
||||
fn apply<A>(f: |A| -> A, v: A) -> A { f(v) }
|
||||
fn apply<A, F>(f: F, v: A) -> A where F: FnOnce(A) -> A { f(v) }
|
||||
|
||||
pub fn main() {
|
||||
let f = {|i| foo(i)};
|
||||
let f = {|: i| foo(i)};
|
||||
assert_eq!(apply(f, 2), 3);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
// Test a rather underspecified example:
|
||||
|
||||
pub fn main() {
|
||||
let f = {|i| i};
|
||||
let f = {|&: i| i};
|
||||
assert_eq!(f(2i), 2i);
|
||||
assert_eq!(f(5i), 5i);
|
||||
}
|
||||
|
@ -22,25 +22,14 @@ fn call_it<F>(f: F)
|
||||
println!("{}", f("Fred".to_string()))
|
||||
}
|
||||
|
||||
fn call_a_thunk(f: ||) {
|
||||
fn call_a_thunk<F>(f: F) where F: FnOnce() {
|
||||
f();
|
||||
}
|
||||
|
||||
fn call_this(f: |&str|:Send) {
|
||||
fn call_this<F>(f: F) where F: FnOnce(&str) + Send {
|
||||
f("Hello!");
|
||||
}
|
||||
|
||||
fn call_that(f: <'a>|&'a int, &'a int| -> int) {
|
||||
let (ten, forty_two) = (10, 42);
|
||||
println!("Your lucky number is {}", f(&ten, &forty_two));
|
||||
}
|
||||
|
||||
fn call_cramped(f:||->uint,g:<'a>||->&'a uint) {
|
||||
let number = f();
|
||||
let other_number = *g();
|
||||
println!("Ticket {} wins an all-expenses-paid trip to Mountain View", number + other_number);
|
||||
}
|
||||
|
||||
fn call_bare(f: fn(&str)) {
|
||||
f("Hello world!")
|
||||
}
|
||||
@ -71,16 +60,6 @@ pub fn main() {
|
||||
|
||||
call_this(|s| println!("{}", s));
|
||||
|
||||
call_that(|x, y| *x + *y);
|
||||
|
||||
let z = 100;
|
||||
call_that(|x, y| *x + *y - z);
|
||||
|
||||
call_cramped(|| 1, || unsafe {
|
||||
static a: uint = 100;
|
||||
mem::transmute(&a)
|
||||
});
|
||||
|
||||
// External functions
|
||||
|
||||
call_bare(println);
|
||||
|
@ -1,20 +0,0 @@
|
||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
fn f(x: || -> !) -> ! {
|
||||
x();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x: || -> ! = || panic!();
|
||||
let _y: || -> ! = || x();
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
// compile-flags:-g
|
||||
|
||||
fn foo<T>() {}
|
||||
|
||||
trait Bar3 {}
|
||||
impl<'b> Bar3 for <'a>|&'a int|: 'b + Send -> &'a int {}
|
||||
|
||||
struct Foo<'a> {
|
||||
a: ||: 'a,
|
||||
b: ||: 'static,
|
||||
c: <'b>||: 'a,
|
||||
d: ||: 'a + Sync,
|
||||
e: <'b>|int|: 'a + Sync -> &'b f32,
|
||||
}
|
||||
|
||||
fn f<'a>(a: &'a int, f: <'b>|&'b int| -> &'b int) -> &'a int {
|
||||
f(a)
|
||||
}
|
||||
|
||||
fn g<'a>(a: &'a int) -> &'a int {
|
||||
a
|
||||
}
|
||||
|
||||
struct A;
|
||||
|
||||
impl A {
|
||||
fn foo<T>(&self) {}
|
||||
}
|
||||
|
||||
fn bar<'b>() {
|
||||
foo::<||>();
|
||||
foo::<|| -> ()>();
|
||||
foo::<||:>();
|
||||
foo::<||:'b>();
|
||||
foo::<||:'b + Sync>();
|
||||
foo::<||:Sync>();
|
||||
foo::< <'a>|int, f32, &'a int|:'b + Sync -> &'a int>();
|
||||
|
||||
foo::<<'a>||>();
|
||||
|
||||
// issue #11209
|
||||
let _: ||: 'b; // for comparison
|
||||
let _: <'a> ||;
|
||||
|
||||
let _: Option<||:'b>;
|
||||
let _: Option<<'a>||>;
|
||||
let _: Option< <'a>||>;
|
||||
|
||||
// issue #11210
|
||||
let _: ||: 'static;
|
||||
|
||||
let a = A;
|
||||
a.foo::<<'a>||>();
|
||||
|
||||
// issue #13490
|
||||
let _ = || -> ! loop {};
|
||||
|
||||
// issue #17021
|
||||
let c = box |&:| {};
|
||||
}
|
||||
|
||||
struct B<T>;
|
||||
impl<'b> B<<'a>||: 'b> {}
|
||||
|
||||
pub fn main() {
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
fn id<T>(x: T) -> T {
|
||||
x
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Show)]
|
||||
struct Foo<T>(T);
|
||||
|
||||
#[derive(PartialEq, Show)]
|
||||
enum Bar<T> {
|
||||
Baz(T)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let f: |int| -> int = id;
|
||||
assert_eq!(f(5), 5);
|
||||
|
||||
let f: |int| -> Foo<int> = Foo;
|
||||
assert_eq!(f(5), Foo(5));
|
||||
|
||||
let f: |int| -> Bar<int> = Bar::Baz;
|
||||
assert_eq!(f(5), Bar::Baz(5));
|
||||
|
||||
let f: |int| -> Option<int> = Some;
|
||||
assert_eq!(f(5), Some(5));
|
||||
}
|
@ -12,9 +12,9 @@ fn foo() -> int {
|
||||
return 0xca7f000d;
|
||||
}
|
||||
|
||||
struct Bar<'a> { f: ||: 'a -> int }
|
||||
struct Bar<F> where F: FnMut() -> int { f: F }
|
||||
|
||||
static mut b : Bar<'static> = Bar { f: foo };
|
||||
static mut b : Bar<fn() -> int> = Bar { f: foo as fn() -> int};
|
||||
|
||||
pub fn main() {
|
||||
unsafe { assert_eq!((b.f)(), 0xca7f000d); }
|
||||
|
@ -17,8 +17,8 @@
|
||||
|
||||
fn f() { }
|
||||
static bare_fns: &'static [fn()] = &[f, f];
|
||||
struct S<'a>(||:'a);
|
||||
static mut closures: &'static mut [S<'static>] = &mut [S(f), S(f)];
|
||||
struct S<F: FnOnce()>(F);
|
||||
static mut closures: &'static mut [S<fn()>] = &mut [S(f as fn()), S(f as fn())];
|
||||
|
||||
pub fn main() {
|
||||
unsafe {
|
||||
|
@ -25,6 +25,6 @@ fn wrapper3(i: chan) {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let wrapped = {||wrapper3(chan::chan_t)};
|
||||
let wrapped = {|&:|wrapper3(chan::chan_t)};
|
||||
wrapped();
|
||||
}
|
||||
|
@ -19,9 +19,6 @@ use std::sync::Arc;
|
||||
trait Trait {}
|
||||
|
||||
fn main() {
|
||||
// Closures - ||
|
||||
assert_eq!(size_of::<||>(), size_of::<Option<||>>());
|
||||
|
||||
// Functions
|
||||
assert_eq!(size_of::<fn(int)>(), size_of::<Option<fn(int)>>());
|
||||
assert_eq!(size_of::<extern "C" fn(int)>(), size_of::<Option<extern "C" fn(int)>>());
|
||||
|
@ -11,9 +11,8 @@
|
||||
|
||||
|
||||
fn test_fn() {
|
||||
type t = ||: 'static -> int;
|
||||
fn ten() -> int { return 10; }
|
||||
let rs: t = ten;
|
||||
let rs = ten;
|
||||
assert!((rs() == 10));
|
||||
}
|
||||
|
||||
|
@ -8,12 +8,9 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
type compare<'a, T> = |Box<T>, Box<T>|: 'a -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: Box<T>, eq: compare<T>) {
|
||||
fn test_generic<T, F>(expected: Box<T>, eq: F) where T: Clone, F: FnOnce(Box<T>, Box<T>) -> bool {
|
||||
let actual: Box<T> = { expected.clone() };
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_box() {
|
||||
@ -22,7 +19,7 @@ fn test_box() {
|
||||
println!("{}", *b2);
|
||||
return *b1 == *b2;
|
||||
}
|
||||
test_generic::<bool>(box true, compare_box);
|
||||
test_generic::<bool, _>(box true, compare_box);
|
||||
}
|
||||
|
||||
pub fn main() { test_box(); }
|
||||
|
@ -8,17 +8,14 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
type compare<'a, T> = |T, T|: 'a -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: T, eq: compare<T>) {
|
||||
fn test_generic<T, F>(expected: T, eq: F) where T: Clone, F: FnOnce(T, T) -> bool {
|
||||
let actual: T = { expected.clone() };
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_vec(v1: Box<int>, v2: Box<int>) -> bool { return v1 == v2; }
|
||||
test_generic::<Box<int>>(box 1, compare_vec);
|
||||
test_generic::<Box<int>, _>(box 1, compare_vec);
|
||||
}
|
||||
|
||||
pub fn main() { test_vec(); }
|
||||
|
@ -8,19 +8,14 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
|
||||
// Tests for standalone blocks as expressions with dynamic type sizes
|
||||
type compare<'a, T> = |T, T|: 'a -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: T, eq: compare<T>) {
|
||||
fn test_generic<T: Clone, F>(expected: T, eq: F) where F: FnOnce(T, T) -> bool {
|
||||
let actual: T = { expected.clone() };
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_bool() {
|
||||
fn compare_bool(b1: bool, b2: bool) -> bool { return b1 == b2; }
|
||||
test_generic::<bool>(true, compare_bool);
|
||||
test_generic::<bool, _>(true, compare_bool);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -33,7 +28,7 @@ fn test_rec() {
|
||||
fn compare_rec(t1: Pair, t2: Pair) -> bool {
|
||||
t1.a == t2.a && t1.b == t2.b
|
||||
}
|
||||
test_generic::<Pair>(Pair {a: 1, b: 2}, compare_rec);
|
||||
test_generic::<Pair, _>(Pair {a: 1, b: 2}, compare_rec);
|
||||
}
|
||||
|
||||
pub fn main() { test_bool(); test_rec(); }
|
||||
|
@ -8,18 +8,17 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
// Tests for if as expressions with dynamic type sizes
|
||||
type compare<T> = |T, T|: 'static -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: T, not_expected: T, eq: compare<T>) {
|
||||
fn test_generic<T, F>(expected: T, not_expected: T, eq: F) where
|
||||
T: Clone,
|
||||
F: FnOnce(T, T) -> bool,
|
||||
{
|
||||
let actual: T = if true { expected.clone() } else { not_expected };
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_bool() {
|
||||
fn compare_bool(b1: bool, b2: bool) -> bool { return b1 == b2; }
|
||||
test_generic::<bool>(true, false, compare_bool);
|
||||
test_generic::<bool, _>(true, false, compare_bool);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -32,7 +31,7 @@ fn test_rec() {
|
||||
fn compare_rec(t1: Pair, t2: Pair) -> bool {
|
||||
t1.a == t2.a && t1.b == t2.b
|
||||
}
|
||||
test_generic::<Pair>(Pair{a: 1, b: 2}, Pair{a: 2, b: 3}, compare_rec);
|
||||
test_generic::<Pair, _>(Pair{a: 1, b: 2}, Pair{a: 2, b: 3}, compare_rec);
|
||||
}
|
||||
|
||||
pub fn main() { test_bool(); test_rec(); }
|
||||
|
@ -8,22 +8,19 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
type compare<T> = |Box<T>, Box<T>|: 'static -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: Box<T>, eq: compare<T>) {
|
||||
fn test_generic<T: Clone, F>(expected: Box<T>, eq: F) where F: FnOnce(Box<T>, Box<T>) -> bool {
|
||||
let actual: Box<T> = match true {
|
||||
true => { expected.clone() },
|
||||
_ => panic!("wat")
|
||||
};
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_box() {
|
||||
fn compare_box(b1: Box<bool>, b2: Box<bool>) -> bool {
|
||||
return *b1 == *b2;
|
||||
}
|
||||
test_generic::<bool>(box true, compare_box);
|
||||
test_generic::<bool, _>(box true, compare_box);
|
||||
}
|
||||
|
||||
pub fn main() { test_box(); }
|
||||
|
@ -8,20 +8,17 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
type compare<'a, T> = |T, T|: 'a -> bool;
|
||||
|
||||
fn test_generic<T:Clone>(expected: T, eq: compare<T>) {
|
||||
fn test_generic<T: Clone, F>(expected: T, eq: F) where F: FnOnce(T, T) -> bool {
|
||||
let actual: T = match true {
|
||||
true => expected.clone(),
|
||||
_ => panic!("wat")
|
||||
};
|
||||
assert!((eq(expected, actual)));
|
||||
assert!(eq(expected, actual));
|
||||
}
|
||||
|
||||
fn test_vec() {
|
||||
fn compare_box(v1: Box<int>, v2: Box<int>) -> bool { return v1 == v2; }
|
||||
test_generic::<Box<int>>(box 1, compare_box);
|
||||
test_generic::<Box<int>, _>(box 1, compare_box);
|
||||
}
|
||||
|
||||
pub fn main() { test_vec(); }
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
fn bare() {}
|
||||
|
||||
fn likes_block(f: ||) { f() }
|
||||
fn likes_block<F>(f: F) where F: FnOnce() { f() }
|
||||
|
||||
pub fn main() {
|
||||
likes_block(bare);
|
||||
|
@ -8,11 +8,11 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
struct r<'a> {
|
||||
field: ||: 'a,
|
||||
struct r<F> where F: FnOnce() {
|
||||
field: F,
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
fn f() {}
|
||||
let _i: r = r {field: f};
|
||||
let _i: r<fn()> = r {field: f as fn()};
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
pub fn main() {
|
||||
let f: |(int,int)| = |(x, y)| {
|
||||
let f = |&: (x, y): (int, int)| {
|
||||
assert_eq!(x, 1);
|
||||
assert_eq!(y, 2);
|
||||
};
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
pub fn main() {
|
||||
// We should be able to type infer inside of ||s.
|
||||
let _f = || {
|
||||
let _f = |&:| {
|
||||
let i = 10i;
|
||||
};
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
fn two(it: |int|) { it(0); it(1); }
|
||||
fn two<F>(mut it: F) where F: FnMut(int) { it(0); it(1); }
|
||||
|
||||
pub fn main() {
|
||||
let mut a: Vec<int> = vec!(-1, -1, -1, -1);
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
|
||||
fn pairs(it: |(int, int)|) {
|
||||
fn pairs<F>(mut it: F) where F: FnMut((int, int)) {
|
||||
let mut i: int = 0;
|
||||
let mut j: int = 0;
|
||||
while i < 10 { it((i, j)); i += 1; j += i; }
|
||||
|
@ -19,7 +19,7 @@ pub fn main() {
|
||||
assert_eq!(sum, 45);
|
||||
}
|
||||
|
||||
fn first_ten(it: |int|) {
|
||||
fn first_ten<F>(mut it: F) where F: FnMut(int) {
|
||||
let mut i: int = 0;
|
||||
while i < 10 { println!("first_ten"); it(i); i = i + 1; }
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn ho(f: |int| -> int) -> int { let n: int = f(3); return n; }
|
||||
fn ho<F>(f: F) -> int where F: FnOnce(int) -> int { let n: int = f(3); return n; }
|
||||
|
||||
fn direct(x: int) -> int { return x + 1; }
|
||||
|
||||
|
@ -10,11 +10,11 @@
|
||||
|
||||
|
||||
trait vec_utils<T> {
|
||||
fn map_<U>(x: &Self, f: |&T| -> U) -> Vec<U> ;
|
||||
fn map_<U, F>(x: &Self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
||||
}
|
||||
|
||||
impl<T> vec_utils<T> for Vec<T> {
|
||||
fn map_<U>(x: &Vec<T> , f: |&T| -> U) -> Vec<U> {
|
||||
fn map_<U, F>(x: &Vec<T> , mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
||||
let mut r = Vec::new();
|
||||
for elt in x.iter() {
|
||||
r.push(f(elt));
|
||||
|
@ -9,14 +9,16 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
/**
|
||||
A somewhat reduced test case to expose some Valgrind issues.
|
||||
|
||||
This originally came from the word-count benchmark.
|
||||
*/
|
||||
|
||||
pub fn map(filename: String, emit: map_reduce::putter) {
|
||||
emit(filename, "1".to_string());
|
||||
pub fn map(filename: String, mut emit: map_reduce::putter) {
|
||||
emit.call_mut((filename, "1".to_string(),));
|
||||
}
|
||||
|
||||
mod map_reduce {
|
||||
@ -25,7 +27,7 @@ mod map_reduce {
|
||||
use std::str;
|
||||
use std::thread::Thread;
|
||||
|
||||
pub type putter<'a> = |String, String|: 'a;
|
||||
pub type putter<'a> = Box<FnMut(String, String) + 'a>;
|
||||
|
||||
pub type mapper = extern fn(String, putter);
|
||||
|
||||
@ -58,7 +60,7 @@ mod map_reduce {
|
||||
}
|
||||
|
||||
let ctrl_clone = ctrl.clone();
|
||||
::map(input, |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
|
||||
::map(input, box |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
|
||||
ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
|
||||
}
|
||||
|
||||
|
@ -13,8 +13,7 @@ trait Typer<'tcx> {
|
||||
fn dummy(&self) { }
|
||||
}
|
||||
|
||||
fn g(_: |&Typer|) {
|
||||
}
|
||||
fn g<F>(_: F) where F: FnOnce(&Typer) {}
|
||||
|
||||
fn h() {
|
||||
g(|typer| typer.dummy())
|
||||
|
@ -40,8 +40,5 @@ fn foo21(t: for<'a> unsafe fn(int) -> int) { }
|
||||
fn foo22(t: for<'a> extern "C" fn(int) -> int) { }
|
||||
fn foo23(t: for<'a> unsafe extern "C" fn(int) -> int) { }
|
||||
|
||||
fn foo30(t: for<'a> |int| -> int) { }
|
||||
fn foo31(t: for<'a> unsafe |int| -> int) { }
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ struct NoAnn<'ast> {
|
||||
impl<'ast> PrinterSupport<'ast> for NoAnn<'ast> {
|
||||
}
|
||||
|
||||
fn foo<'ast> (f: Option<&'ast uint>, g: |&PrinterSupport|) {
|
||||
fn foo<'ast, G>(f: Option<&'ast uint>, g: G) where G: FnOnce(&PrinterSupport) {
|
||||
let annotation = NoAnn { f: f };
|
||||
g(&annotation)
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ trait Repro {
|
||||
fn repro(self, s: MyStruct) -> String;
|
||||
}
|
||||
|
||||
impl Repro for |MyStruct|:'static -> String {
|
||||
impl<F> Repro for F where F: FnOnce(MyStruct) -> String {
|
||||
fn repro(self, s: MyStruct) -> String {
|
||||
self(s)
|
||||
}
|
||||
@ -26,5 +26,5 @@ fn do_stuff<R: Repro>(r: R) -> String {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!("MyStruct".to_string(), do_stuff(|s: MyStruct| format!("{}", s)));
|
||||
assert_eq!("MyStruct".to_string(), do_stuff(|: s: MyStruct| format!("{}", s)));
|
||||
}
|
||||
|
@ -24,7 +24,6 @@ pub fn type_ids() -> Vec<TypeId> {
|
||||
ids.push(TypeId::of::<testtypes::FooEnum>());
|
||||
ids.push(TypeId::of::<testtypes::FooUniq>());
|
||||
ids.push(TypeId::of::<testtypes::FooPtr>());
|
||||
ids.push(TypeId::of::<testtypes::FooClosure>());
|
||||
ids.push(TypeId::of::<&'static testtypes::FooTrait>());
|
||||
ids.push(TypeId::of::<testtypes::FooStruct>());
|
||||
ids.push(TypeId::of::<testtypes::FooTuple>());
|
||||
|
@ -9,12 +9,12 @@
|
||||
// except according to those terms.
|
||||
|
||||
struct Foo<'a> {
|
||||
listener: ||: 'a
|
||||
listener: Box<FnMut() + 'a>,
|
||||
}
|
||||
|
||||
impl<'a> Foo<'a> {
|
||||
fn new(listener: ||: 'a) -> Foo<'a> {
|
||||
Foo { listener: listener }
|
||||
fn new<F>(listener: F) -> Foo<'a> where F: FnMut() + 'a {
|
||||
Foo { listener: box listener }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,5 +10,5 @@
|
||||
|
||||
|
||||
pub fn main() {
|
||||
{|i| if 1i == i { }};
|
||||
{|&: i| if 1i == i { }};
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ trait Matcher {
|
||||
|
||||
struct CharPredMatcher<'a, 'b> {
|
||||
str: &'a str,
|
||||
pred: |char|:'b -> bool
|
||||
pred: Box<FnMut(char) -> bool + 'b>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Matcher for CharPredMatcher<'a, 'b> {
|
||||
@ -29,11 +29,11 @@ trait IntoMatcher<'a, T> {
|
||||
fn into_matcher(self, &'a str) -> T;
|
||||
}
|
||||
|
||||
impl<'a, 'b> IntoMatcher<'a, CharPredMatcher<'a, 'b>> for |char|:'b -> bool {
|
||||
impl<'a, 'b, F> IntoMatcher<'a, CharPredMatcher<'a, 'b>> for F where F: FnMut(char) -> bool + 'b {
|
||||
fn into_matcher(self, s: &'a str) -> CharPredMatcher<'a, 'b> {
|
||||
CharPredMatcher {
|
||||
str: s,
|
||||
pred: self
|
||||
pred: box self,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -57,6 +57,6 @@ fn match_indices<'a, M, T: IntoMatcher<'a, M>>(s: &'a str, from: T) -> MatchIndi
|
||||
|
||||
fn main() {
|
||||
let s = "abcbdef";
|
||||
match_indices(s, |c: char| c == 'b')
|
||||
match_indices(s, |&mut: c: char| c == 'b')
|
||||
.collect::<Vec<(uint, uint)>>();
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub fn main() {
|
||||
let early_error: |&str|: 'static -> ! = |_msg| { panic!() };
|
||||
}
|
@ -10,5 +10,5 @@
|
||||
|
||||
fn main() {
|
||||
let mut buf = Vec::new();
|
||||
|c: u8| buf.push(c);
|
||||
|&mut: c: u8| buf.push(c);
|
||||
}
|
||||
|
@ -11,11 +11,11 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
pub fn main() {
|
||||
let one: || -> uint = || {
|
||||
let one = |&:| {
|
||||
enum r { a };
|
||||
r::a as uint
|
||||
};
|
||||
let two: || -> uint = || {
|
||||
let two = |&:| {
|
||||
enum r { a };
|
||||
r::a as uint
|
||||
};
|
||||
|
@ -29,7 +29,7 @@ fn socket() -> socket {
|
||||
}
|
||||
}
|
||||
|
||||
fn closure(f: ||) { f() }
|
||||
fn closure<F>(f: F) where F: FnOnce() { f() }
|
||||
|
||||
fn setsockopt_bytes(_sock: int) { }
|
||||
|
||||
|
@ -9,10 +9,10 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
type Connection = |Vec<u8>|: 'static;
|
||||
type Connection = Box<FnMut(Vec<u8>) + 'static>;
|
||||
|
||||
fn f() -> Option<Connection> {
|
||||
let mock_connection: Connection = |_| {};
|
||||
let mock_connection: Connection = box |&mut: _| {};
|
||||
Some(mock_connection)
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
pub fn main() {
|
||||
let x = 1;
|
||||
let y: || -> int = || x;
|
||||
let x = 1u;
|
||||
let y = |&:| x;
|
||||
let _z = y();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
enum PureCounter { PureCounterVariant(uint) }
|
||||
|
||||
fn each(thing: PureCounter, blk: |v: &uint|) {
|
||||
fn each<F>(thing: PureCounter, blk: F) where F: FnOnce(&uint) {
|
||||
let PureCounter::PureCounterVariant(ref x) = thing;
|
||||
blk(x);
|
||||
}
|
||||
|
@ -8,21 +8,19 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
type ErrPrinter<'a> = |&str, &str|: 'a;
|
||||
|
||||
fn example_err(prog: &str, arg: &str) {
|
||||
println!("{}: {}", prog, arg)
|
||||
}
|
||||
|
||||
fn exit(print: ErrPrinter, prog: &str, arg: &str) {
|
||||
fn exit<F>(print: F, prog: &str, arg: &str) where F: FnOnce(&str, &str) {
|
||||
print(prog, arg);
|
||||
}
|
||||
|
||||
struct X<'a> {
|
||||
err: ErrPrinter<'a>
|
||||
struct X<F> where F: FnOnce(&str, &str) {
|
||||
err: F,
|
||||
}
|
||||
|
||||
impl<'a> X<'a> {
|
||||
impl<F> X<F> where F: FnOnce(&str, &str) {
|
||||
pub fn boom(self) {
|
||||
exit(self.err, "prog", "arg");
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
// Regression test for issue #5239
|
||||
|
||||
pub fn main() {
|
||||
let _f: |int| -> int = |ref x: int| { *x };
|
||||
let _f = |&: ref x: int| { *x };
|
||||
let foo = 10;
|
||||
assert!(_f(foo) == 10);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
fn swap(f: |Vec<int> | -> Vec<int> ) -> Vec<int> {
|
||||
fn swap<F>(f: F) -> Vec<int> where F: FnOnce(Vec<int>) -> Vec<int> {
|
||||
let x = vec!(1, 2, 3);
|
||||
f(x)
|
||||
}
|
||||
|
@ -8,9 +8,9 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub trait OpInt<'a> { fn call(&mut self, int, int) -> int; }
|
||||
pub trait OpInt { fn call(&mut self, int, int) -> int; }
|
||||
|
||||
impl<'a> OpInt<'a> for |int, int|: 'a -> int {
|
||||
impl<F> OpInt for F where F: FnMut(int, int) -> int {
|
||||
fn call(&mut self, a:int, b:int) -> int {
|
||||
(*self)(a, b)
|
||||
}
|
||||
@ -21,7 +21,7 @@ fn squarei<'a>(x: int, op: &'a mut OpInt) -> int { op.call(x, x) }
|
||||
fn muli(x:int, y:int) -> int { x * y }
|
||||
|
||||
pub fn main() {
|
||||
let mut f = |x,y| muli(x,y);
|
||||
let mut f = |&mut: x, y| muli(x, y);
|
||||
{
|
||||
let g = &mut f;
|
||||
let h = g as &mut OpInt;
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn f<T>(g: || -> T) -> T { g() }
|
||||
fn f<T, F>(g: F) -> T where F: FnOnce() -> T { g() }
|
||||
|
||||
pub fn main() {
|
||||
let _x = f( | | { 10i });
|
||||
@ -20,5 +20,5 @@ pub fn main() {
|
||||
let _: () = f(| | { });
|
||||
// empty block with no type info should compile too
|
||||
let _ = f(||{});
|
||||
let _ = (||{});
|
||||
let _ = (|&:|{});
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ fn Ident_new() -> Ident {
|
||||
|
||||
pub fn light_fuse(fld: Box<bomb>) {
|
||||
int3!();
|
||||
let f = || {
|
||||
let f = |&:| {
|
||||
int3!();
|
||||
fld.boom(Ident_new()); // *** 1
|
||||
};
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
|
||||
|
||||
fn range_(a: int, b: int, it: |int|) {
|
||||
fn range_<F>(a: int, b: int, mut it: F) where F: FnMut(int) {
|
||||
assert!((a < b));
|
||||
let mut i: int = a;
|
||||
while i < b { it(i); i += 1; }
|
||||
|
@ -16,7 +16,7 @@ struct Refs { refs: Vec<int> , n: int }
|
||||
|
||||
pub fn main() {
|
||||
let mut e = Refs{refs: vec!(), n: 0};
|
||||
let _f: || = || println!("{}", e.n);
|
||||
let _f = |&:| println!("{}", e.n);
|
||||
let x: &[int] = e.refs.as_slice();
|
||||
assert_eq!(x.len(), 0);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
// Issue #1818
|
||||
|
||||
fn lp<T>(s: String, f: |String| -> T) -> T {
|
||||
fn lp<T, F>(s: String, mut f: F) -> T where F: FnMut(String) -> T {
|
||||
while false {
|
||||
let r = f(s);
|
||||
return (r);
|
||||
@ -18,8 +18,8 @@ fn lp<T>(s: String, f: |String| -> T) -> T {
|
||||
panic!();
|
||||
}
|
||||
|
||||
fn apply<T>(s: String, f: |String| -> T) -> T {
|
||||
fn g<T>(s: String, f: |String| -> T) -> T {f(s)}
|
||||
fn apply<T, F>(s: String, mut f: F) -> T where F: FnMut(String) -> T {
|
||||
fn g<T, F>(s: String, mut f: F) -> T where F: FnMut(String) -> T {f(s)}
|
||||
g(s, |v| { let r = f(v); r })
|
||||
}
|
||||
|
||||
|
@ -10,16 +10,17 @@
|
||||
|
||||
// Make sure #1399 stays fixed
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
struct A { a: Box<int> }
|
||||
|
||||
fn foo() -> ||: 'static -> int {
|
||||
fn foo() -> Box<FnMut() -> int + 'static> {
|
||||
let k = box 22i;
|
||||
let _u = A {a: k.clone()};
|
||||
let result: ||: 'static -> int = || 22;
|
||||
result
|
||||
let result = |&mut:| 22;
|
||||
box result
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(foo()(), 22);
|
||||
assert_eq!(foo().call_mut(()), 22);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
struct A { a: Box<int> }
|
||||
|
||||
pub fn main() {
|
||||
fn invoke(f: ||) { f(); }
|
||||
fn invoke<F>(f: F) where F: FnOnce() { f(); }
|
||||
let k = box 22i;
|
||||
let _u = A {a: k.clone()};
|
||||
invoke(|| println!("{}", k.clone()) )
|
||||
|
@ -13,7 +13,7 @@
|
||||
|
||||
enum thing { a, b, c, }
|
||||
|
||||
fn foo(it: |int|) { it(10); }
|
||||
fn foo<F>(it: F) where F: FnOnce(int) { it(10); }
|
||||
|
||||
pub fn main() {
|
||||
let mut x = true;
|
||||
|
@ -11,11 +11,11 @@
|
||||
|
||||
|
||||
trait vec_monad<A> {
|
||||
fn bind<B>(&self, f: |&A| -> Vec<B> ) -> Vec<B> ;
|
||||
fn bind<B, F>(&self, f: F ) -> Vec<B> where F: FnMut(&A) -> Vec<B> ;
|
||||
}
|
||||
|
||||
impl<A> vec_monad<A> for Vec<A> {
|
||||
fn bind<B>(&self, f: |&A| -> Vec<B> ) -> Vec<B> {
|
||||
fn bind<B, F>(&self, mut f: F) -> Vec<B> where F: FnMut(&A) -> Vec<B> {
|
||||
let mut r = Vec::new();
|
||||
for elt in self.iter() {
|
||||
r.extend(f(elt).into_iter());
|
||||
@ -25,11 +25,11 @@ impl<A> vec_monad<A> for Vec<A> {
|
||||
}
|
||||
|
||||
trait option_monad<A> {
|
||||
fn bind<B>(&self, f: |&A| -> Option<B>) -> Option<B>;
|
||||
fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B>;
|
||||
}
|
||||
|
||||
impl<A> option_monad<A> for Option<A> {
|
||||
fn bind<B>(&self, f: |&A| -> Option<B>) -> Option<B> {
|
||||
fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B> {
|
||||
match *self {
|
||||
Some(ref a) => { f(a) }
|
||||
None => { None }
|
||||
|
@ -9,9 +9,9 @@
|
||||
// except according to those terms.
|
||||
|
||||
// Issue #922
|
||||
fn f2(_thing: ||) { }
|
||||
fn f2<F>(_thing: F) where F: FnOnce() { }
|
||||
|
||||
fn f(thing: ||) {
|
||||
fn f<F>(thing: F) where F: FnOnce() {
|
||||
f2(thing);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ fn f(mut y: Box<int>) {
|
||||
}
|
||||
|
||||
fn g() {
|
||||
let frob: |Box<int>| = |mut q| { *q = 2; assert!(*q == 2); };
|
||||
let frob = |&: mut q: Box<int>| { *q = 2; assert!(*q == 2); };
|
||||
let w = box 37;
|
||||
frob(w);
|
||||
|
||||
|
@ -75,6 +75,6 @@ pub fn main() {
|
||||
x = 30;
|
||||
assert_eq!(x, 30);
|
||||
|
||||
(|A { x: mut t }: A| { t = t+1; t })(A { x: 34 });
|
||||
(|&: A { x: mut t }: A| { t = t+1; t })(A { x: 34 });
|
||||
|
||||
}
|
||||
|
@ -10,9 +10,9 @@
|
||||
|
||||
// Tests for the new |args| expr lambda syntax
|
||||
|
||||
fn f(i: int, f: |int| -> int) -> int { f(i) }
|
||||
fn f<F>(i: int, f: F) -> int where F: FnOnce(int) -> int { f(i) }
|
||||
|
||||
fn g(_g: ||) { }
|
||||
fn g<G>(_g: G) where G: FnOnce() { }
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(f(10, |a| a), 10);
|
||||
|
@ -12,7 +12,7 @@ struct X {
|
||||
repr: int
|
||||
}
|
||||
|
||||
fn apply<T>(x: T, f: |T|) {
|
||||
fn apply<T, F>(x: T, f: F) where F: FnOnce(T) {
|
||||
f(x);
|
||||
}
|
||||
|
||||
|
@ -14,8 +14,8 @@ struct Foo {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let f = |(x, _): (int, int)| println!("{}", x + 1);
|
||||
let g = |Foo { x: x, y: _y }: Foo| println!("{}", x + 1);
|
||||
let f = |&: (x, _): (int, int)| println!("{}", x + 1);
|
||||
let g = |&: Foo { x: x, y: _y }: Foo| println!("{}", x + 1);
|
||||
f((2, 3));
|
||||
g(Foo { x: 1, y: 2 });
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
fn something(f: ||) { f(); }
|
||||
fn something<F>(f: F) where F: FnOnce() { f(); }
|
||||
pub fn main() {
|
||||
something(|| println!("hi!") );
|
||||
}
|
||||
|
@ -8,11 +8,13 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
struct closure_box<'a> {
|
||||
cl: ||: 'a,
|
||||
cl: Box<FnMut() + 'a>,
|
||||
}
|
||||
|
||||
fn box_it(x: ||) -> closure_box {
|
||||
fn box_it<'a>(x: Box<FnMut() + 'a>) -> closure_box<'a> {
|
||||
closure_box {cl: x}
|
||||
}
|
||||
|
||||
@ -20,9 +22,9 @@ pub fn main() {
|
||||
let mut i = 3i;
|
||||
assert_eq!(i, 3);
|
||||
{
|
||||
let cl = || i += 1;
|
||||
let cl_box = box_it(cl);
|
||||
(cl_box.cl)();
|
||||
let cl = |&mut:| i += 1;
|
||||
let mut cl_box = box_it(box cl);
|
||||
cl_box.cl.call_mut(());
|
||||
}
|
||||
assert_eq!(i, 4);
|
||||
}
|
||||
|
@ -11,9 +11,9 @@
|
||||
// Test lifetimes are linked properly when we autoslice a vector.
|
||||
// Issue #3148.
|
||||
|
||||
fn subslice(v: ||) -> || { v }
|
||||
fn subslice<F>(v: F) -> F where F: FnOnce() { v }
|
||||
|
||||
fn both(v: ||) -> || {
|
||||
fn both<F>(v: F) -> F where F: FnOnce() {
|
||||
subslice(subslice(v))
|
||||
}
|
||||
|
||||
|
@ -13,13 +13,13 @@
|
||||
// Here, `f` is a function that takes a pointer `x` and a function
|
||||
// `g`, where `g` requires its argument `y` to be in the same region
|
||||
// that `x` is in.
|
||||
fn has_same_region(f: <'a>|x: &'a int, g: |y: &'a int||) {
|
||||
fn has_same_region(f: Box<for<'a> FnMut(&'a int, Box<FnMut(&'a int)>)>) {
|
||||
// `f` should be the type that `wants_same_region` wants, but
|
||||
// right now the compiler complains that it isn't.
|
||||
wants_same_region(f);
|
||||
}
|
||||
|
||||
fn wants_same_region(_f: <'b>|x: &'b int, g: |y: &'b int||) {
|
||||
fn wants_same_region(_f: Box<for<'b> FnMut(&'b int, Box<FnMut(&'b int)>)>) {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
@ -14,21 +14,21 @@
|
||||
#![allow(unused_variable)]
|
||||
|
||||
// Should pass region checking.
|
||||
fn ok(f: |x: &uint|) {
|
||||
fn ok(f: Box<FnMut(&uint)>) {
|
||||
// Here, g is a function that can accept a uint pointer with
|
||||
// lifetime r, and f is a function that can accept a uint pointer
|
||||
// with any lifetime. The assignment g = f should be OK (i.e.,
|
||||
// f's type should be a subtype of g's type), because f can be
|
||||
// used in any context that expects g's type. But this currently
|
||||
// fails.
|
||||
let mut g: <'r>|y: &'r uint| = |x| { };
|
||||
let mut g: Box<for<'r> FnMut(&'r uint)> = box |x| { };
|
||||
g = f;
|
||||
}
|
||||
|
||||
// This version is the same as above, except that here, g's type is
|
||||
// inferred.
|
||||
fn ok_inferred(f: |x: &uint|) {
|
||||
let mut g: <'r>|x: &'r uint| = |_| {};
|
||||
fn ok_inferred(f: Box<FnMut(&uint)>) {
|
||||
let mut g: Box<for<'r> FnMut(&'r uint)> = box |_| {};
|
||||
g = f;
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
fn takes_two(x: &int, y: &int) -> int { *x + *y }
|
||||
|
||||
fn with<T>(f: |x: &int| -> T) -> T {
|
||||
fn with<T, F>(f: F) -> T where F: FnOnce(&int) -> T {
|
||||
f(&20)
|
||||
}
|
||||
|
||||
|
@ -24,15 +24,15 @@
|
||||
|
||||
pub fn main() {
|
||||
fn explicit() {
|
||||
fn test(_x: Option<|f: <'a> |g: &'a int||>) {}
|
||||
test(Some(|_f: <'a> |g: &'a int|| {}));
|
||||
fn test<F>(_x: Option<Box<F>>) where F: FnMut(Box<for<'a> FnMut(&'a int)>) {}
|
||||
test(Some(box |&mut: _f: Box<for<'a> FnMut(&'a int)>| {}));
|
||||
}
|
||||
|
||||
// The code below is shorthand for the code above (and more likely
|
||||
// to represent what one encounters in practice).
|
||||
fn implicit() {
|
||||
fn test(_x: Option<|f: |g: & int||>) {}
|
||||
test(Some(|_f: |g: & int|| {}));
|
||||
fn test<F>(_x: Option<Box<F>>) where F: FnMut(Box< FnMut(& int)>) {}
|
||||
test(Some(box |&mut: _f: Box< FnMut(& int)>| {}));
|
||||
}
|
||||
|
||||
explicit();
|
||||
|
@ -13,7 +13,7 @@
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
fn with<'a>(_: |&'a Vec<int>| -> &'a Vec<int>) { }
|
||||
fn with<'a, F>(_: F) where F: FnOnce(&'a Vec<int>) -> &'a Vec<int> { }
|
||||
|
||||
fn foo() {
|
||||
with(|&ref ints| ints);
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
fn region_identity(x: &uint) -> &uint { x }
|
||||
|
||||
fn apply<T>(t: T, f: |T| -> T) -> T { f(t) }
|
||||
fn apply<T, F>(t: T, f: F) -> T where F: FnOnce(T) -> T { f(t) }
|
||||
|
||||
fn parameterized(x: &uint) -> uint {
|
||||
let z = apply(x, ({|y|
|
||||
|
@ -8,19 +8,21 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
struct closure_box<'a> {
|
||||
cl: ||: 'a,
|
||||
cl: Box<FnMut() + 'a>,
|
||||
}
|
||||
|
||||
fn box_it(x: ||) -> closure_box {
|
||||
fn box_it<'a>(x: Box<FnMut() + 'a>) -> closure_box<'a> {
|
||||
closure_box {cl: x}
|
||||
}
|
||||
|
||||
fn call_static_closure(cl: closure_box<'static>) {
|
||||
(cl.cl)();
|
||||
fn call_static_closure(mut cl: closure_box<'static>) {
|
||||
cl.cl.call_mut(())
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let cl_box = box_it(|| println!("Hello, world!"));
|
||||
let cl_box = box_it(box |&mut:| println!("Hello, world!"));
|
||||
call_static_closure(cl_box);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
static mut calls: uint = 0;
|
||||
|
||||
fn surrounding() {
|
||||
let return_works = |n: int| {
|
||||
let return_works = |&: n: int| {
|
||||
unsafe { calls += 1 }
|
||||
|
||||
if n >= 0 { return; }
|
||||
|
@ -9,7 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
fn test(f: |uint| -> uint) -> uint {
|
||||
fn test<F>(f: F) -> uint where F: FnOnce(uint) -> uint {
|
||||
return f(22u);
|
||||
}
|
||||
|
||||
|
@ -26,14 +26,14 @@ mod b {
|
||||
|
||||
trait uint_utils {
|
||||
fn str(&self) -> String;
|
||||
fn multi(&self, f: |uint|);
|
||||
fn multi<F>(&self, f: F) where F: FnMut(uint);
|
||||
}
|
||||
|
||||
impl uint_utils for uint {
|
||||
fn str(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
fn multi(&self, f: |uint|) {
|
||||
fn multi<F>(&self, mut f: F) where F: FnMut(uint) {
|
||||
let mut c = 0u;
|
||||
while c < *self { f(c); c += 1u; }
|
||||
}
|
||||
@ -41,14 +41,14 @@ impl uint_utils for uint {
|
||||
|
||||
trait vec_utils<T> {
|
||||
fn length_(&self, ) -> uint;
|
||||
fn iter_(&self, f: |&T|);
|
||||
fn map_<U>(&self, f: |&T| -> U) -> Vec<U> ;
|
||||
fn iter_<F>(&self, f: F) where F: FnMut(&T);
|
||||
fn map_<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
||||
}
|
||||
|
||||
impl<T> vec_utils<T> for Vec<T> {
|
||||
fn length_(&self) -> uint { self.len() }
|
||||
fn iter_(&self, f: |&T|) { for x in self.iter() { f(x); } }
|
||||
fn map_<U>(&self, f: |&T| -> U) -> Vec<U> {
|
||||
fn iter_<F>(&self, mut f: F) where F: FnMut(&T) { for x in self.iter() { f(x); } }
|
||||
fn map_<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
||||
let mut r = Vec::new();
|
||||
for elt in self.iter() {
|
||||
r.push(f(elt));
|
||||
@ -64,7 +64,7 @@ pub fn main() {
|
||||
assert_eq!((vec!(1i)).length_().str(), "1".to_string());
|
||||
let vect = vec!(3i, 4).map_(|a| *a + 4);
|
||||
assert_eq!(vect[0], 7);
|
||||
let vect = (vec!(3i, 4)).map_::<uint>(|a| *a as uint + 4u);
|
||||
let vect = (vec!(3i, 4)).map_::<uint, _>(|a| *a as uint + 4u);
|
||||
assert_eq!(vect[0], 7u);
|
||||
let mut x = 0u;
|
||||
10u.multi(|_n| x += 2u );
|
||||
|
@ -16,7 +16,7 @@ struct S { val: int }
|
||||
impl S { fn new(v: int) -> S { S { val: v } } }
|
||||
impl Drop for S { fn drop(&mut self) { } }
|
||||
|
||||
pub fn f<T>((b1, b2): (T, T), f: |T| -> T) -> Partial<T> {
|
||||
pub fn f<T, F>((b1, b2): (T, T), mut f: F) -> Partial<T> where F: FnMut(T) -> T {
|
||||
let p = Partial { x: b1, y: b2 };
|
||||
|
||||
// Move of `p` is legal even though we are also moving `p.y`; the
|
||||
|
@ -18,7 +18,7 @@ impl Drop for S { fn drop(&mut self) { } }
|
||||
|
||||
pub type Two<T> = (Partial<T>, Partial<T>);
|
||||
|
||||
pub fn f<T>((b1, b2): (T, T), (b3, b4): (T, T), f: |T| -> T) -> Two<T> {
|
||||
pub fn f<T, F>((b1, b2): (T, T), (b3, b4): (T, T), mut f: F) -> Two<T> where F: FnMut(T) -> T {
|
||||
let p = Partial { x: b1, y: b2 };
|
||||
let q = Partial { x: b3, y: b4 };
|
||||
|
||||
|
@ -189,7 +189,7 @@ pub fn dont_double_panic() {
|
||||
assert!(r.is_err());
|
||||
}
|
||||
|
||||
fn in_tmpdir(f: ||) {
|
||||
fn in_tmpdir<F>(f: F) where F: FnOnce() {
|
||||
let tmpdir = TempDir::new("test").ok().expect("can't make tmpdir");
|
||||
assert!(os::change_dir(tmpdir.path()).is_ok());
|
||||
|
||||
|
@ -11,12 +11,16 @@
|
||||
// Tests that a heterogeneous list of existential types can be put inside an Arc
|
||||
// and shared between tasks as long as all types fulfill Send.
|
||||
|
||||
// ignore-pretty
|
||||
|
||||
#![feature(unboxed_closures)]
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::thread::Thread;
|
||||
|
||||
trait Pet {
|
||||
fn name(&self, blk: |&str|);
|
||||
fn name(&self, blk: Box<FnMut(&str)>);
|
||||
fn num_legs(&self) -> uint;
|
||||
fn of_good_pedigree(&self) -> bool;
|
||||
}
|
||||
@ -38,19 +42,19 @@ struct Goldfyshe {
|
||||
}
|
||||
|
||||
impl Pet for Catte {
|
||||
fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
|
||||
fn name(&self, mut blk: Box<FnMut(&str)>) { blk.call_mut((self.name.as_slice(),)) }
|
||||
fn num_legs(&self) -> uint { 4 }
|
||||
fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
|
||||
}
|
||||
impl Pet for Dogge {
|
||||
fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
|
||||
fn name(&self, mut blk: Box<FnMut(&str)>) { blk.call_mut((self.name.as_slice(),)) }
|
||||
fn num_legs(&self) -> uint { 4 }
|
||||
fn of_good_pedigree(&self) -> bool {
|
||||
self.bark_decibels < 70 || self.tricks_known > 20
|
||||
}
|
||||
}
|
||||
impl Pet for Goldfyshe {
|
||||
fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
|
||||
fn name(&self, mut blk: Box<FnMut(&str)>) { blk.call_mut((self.name.as_slice(),)) }
|
||||
fn num_legs(&self) -> uint { 0 }
|
||||
fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
|
||||
}
|
||||
@ -98,7 +102,7 @@ fn check_legs(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
|
||||
}
|
||||
fn check_names(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
|
||||
for pet in arc.iter() {
|
||||
pet.name(|name| {
|
||||
pet.name(box |name| {
|
||||
assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
|
||||
})
|
||||
}
|
||||
|
@ -24,10 +24,10 @@ impl to_str for () {
|
||||
}
|
||||
|
||||
trait map<T> {
|
||||
fn map<U>(&self, f: |&T| -> U) -> Vec<U> ;
|
||||
fn map<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
||||
}
|
||||
impl<T> map<T> for Vec<T> {
|
||||
fn map<U>(&self, f: |&T| -> U) -> Vec<U> {
|
||||
fn map<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
||||
let mut r = Vec::new();
|
||||
for i in self.iter() {
|
||||
r.push(f(i));
|
||||
|
@ -34,24 +34,6 @@ fn main() {
|
||||
let f = TypeId::of::<fn(for<'a> fn(&'a int) -> &'a int)>();
|
||||
assert!(e != f);
|
||||
}
|
||||
// Stack closures
|
||||
{
|
||||
let a = TypeId::of::<|&'static int, &'static int|>();
|
||||
let b = TypeId::of::<for<'a> |&'static int, &'a int|>();
|
||||
let c = TypeId::of::<for<'a, 'b> |&'a int, &'b int|>();
|
||||
let d = TypeId::of::<for<'a, 'b> |&'b int, &'a int|>();
|
||||
assert!(a != b);
|
||||
assert!(a != c);
|
||||
assert!(a != d);
|
||||
assert!(b != c);
|
||||
assert!(b != d);
|
||||
assert_eq!(c, d);
|
||||
|
||||
// Make sure De Bruijn indices are handled correctly
|
||||
let e = TypeId::of::<for<'a> |(|&'a int| -> &'a int)|>();
|
||||
let f = TypeId::of::<|for<'a> |&'a int| -> &'a int|>();
|
||||
assert!(e != f);
|
||||
}
|
||||
// Boxed unboxed closures
|
||||
{
|
||||
let a = TypeId::of::<Box<Fn(&'static int, &'static int)>>();
|
||||
|
@ -14,7 +14,7 @@ struct S<T> {
|
||||
b: uint,
|
||||
}
|
||||
|
||||
fn range_(lo: uint, hi: uint, it: |uint|) {
|
||||
fn range_<F>(lo: uint, hi: uint, mut it: F) where F: FnMut(uint) {
|
||||
let mut lo_ = lo;
|
||||
while lo_ < hi { it(lo_); lo_ += 1u; }
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ fn good(_a: &int) {
|
||||
|
||||
// unnamed argument &int is now parse x: &int
|
||||
|
||||
fn called(_f: |&int|) {
|
||||
fn called<F>(_f: F) where F: FnOnce(&int) {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user