Implement Copy/Clone for closures
This commit is contained in:
parent
01c65cb15a
commit
f7964aebe5
10 changed files with 212 additions and 18 deletions
21
src/test/compile-fail/feature-gate-clone-closures.rs
Normal file
21
src/test/compile-fail/feature-gate-clone-closures.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#[derive(Clone)]
|
||||
struct S(i32);
|
||||
|
||||
fn main() {
|
||||
let a = S(5);
|
||||
let hello = move || {
|
||||
println!("Hello {}", a.0);
|
||||
};
|
||||
|
||||
let hello = hello.clone(); //~ ERROR no method named `clone` found for type
|
||||
}
|
||||
19
src/test/compile-fail/feature-gate-copy-closures.rs
Normal file
19
src/test/compile-fail/feature-gate-copy-closures.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
fn main() {
|
||||
let a = 5;
|
||||
let hello = || {
|
||||
println!("Hello {}", a);
|
||||
};
|
||||
|
||||
let b = hello;
|
||||
let c = hello; //~ ERROR use of moved value: `hello` [E0382]
|
||||
}
|
||||
24
src/test/compile-fail/not-clone-closure.rs
Normal file
24
src/test/compile-fail/not-clone-closure.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Check that closures do not implement `Clone` if their environment is not `Clone`.
|
||||
|
||||
#![feature(clone_closures)]
|
||||
|
||||
struct S(i32);
|
||||
|
||||
fn main() {
|
||||
let a = S(5);
|
||||
let hello = move || {
|
||||
println!("Hello {}", a.0);
|
||||
};
|
||||
|
||||
let hello = hello.clone(); //~ ERROR the trait bound `S: std::clone::Clone` is not satisfied
|
||||
}
|
||||
24
src/test/compile-fail/not-copy-closure.rs
Normal file
24
src/test/compile-fail/not-copy-closure.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Check that closures do not implement `Copy` if their environment is not `Copy`.
|
||||
|
||||
#![feature(copy_closures)]
|
||||
#![feature(clone_closures)]
|
||||
|
||||
fn main() {
|
||||
let mut a = 5;
|
||||
let hello = || {
|
||||
a += 1;
|
||||
};
|
||||
|
||||
let b = hello;
|
||||
let c = hello; //~ ERROR use of moved value: `hello` [E0382]
|
||||
}
|
||||
29
src/test/run-pass/clone-closure.rs
Normal file
29
src/test/run-pass/clone-closure.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Check that closures implement `Clone`.
|
||||
|
||||
#![feature(clone_closures)]
|
||||
|
||||
#[derive(Clone)]
|
||||
struct S(i32);
|
||||
|
||||
fn main() {
|
||||
let mut a = S(5);
|
||||
let mut hello = move || {
|
||||
a.0 += 1;
|
||||
println!("Hello {}", a.0);
|
||||
a.0
|
||||
};
|
||||
|
||||
let mut hello2 = hello.clone();
|
||||
assert_eq!(6, hello2());
|
||||
assert_eq!(6, hello());
|
||||
}
|
||||
28
src/test/run-pass/copy-closure.rs
Normal file
28
src/test/run-pass/copy-closure.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Check that closures implement `Copy`.
|
||||
|
||||
#![feature(copy_closures)]
|
||||
#![feature(clone_closures)]
|
||||
|
||||
fn call<T, F: FnOnce() -> T>(f: F) -> T { f() }
|
||||
|
||||
fn main() {
|
||||
let a = 5;
|
||||
let hello = || {
|
||||
println!("Hello {}", a);
|
||||
a
|
||||
};
|
||||
|
||||
assert_eq!(5, call(hello.clone()));
|
||||
assert_eq!(5, call(hello));
|
||||
assert_eq!(5, call(hello));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue