2016-11-10 17:08:21 +00:00
|
|
|
#![feature(fn_traits, unboxed_closures)]
|
2014-07-01 21:30:33 +00:00
|
|
|
|
|
|
|
use std::ops::{Fn, FnMut, FnOnce};
|
|
|
|
|
|
|
|
struct SFn {
|
2015-01-08 10:54:35 +00:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2014-07-01 21:30:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl Fn<(isize,)> for SFn {
|
2015-01-08 10:54:35 +00:00
|
|
|
extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
|
2014-07-01 21:30:33 +00:00
|
|
|
self.x * self.y * z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnMut<(isize,)> for SFn {
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FnOnce<(isize,)> for SFn {
|
|
|
|
type Output = isize;
|
|
|
|
extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) }
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:30:33 +00:00
|
|
|
struct SFnMut {
|
2015-01-08 10:54:35 +00:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2014-07-01 21:30:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl FnMut<(isize,)> for SFnMut {
|
2015-01-08 10:54:35 +00:00
|
|
|
extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
|
2014-07-01 21:30:33 +00:00
|
|
|
self.x * self.y * z
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnOnce<(isize,)> for SFnMut {
|
|
|
|
type Output = isize;
|
|
|
|
extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) }
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:30:33 +00:00
|
|
|
struct SFnOnce {
|
|
|
|
x: String,
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl FnOnce<(String,)> for SFnOnce {
|
|
|
|
type Output = usize;
|
|
|
|
|
2015-01-08 11:02:42 +00:00
|
|
|
extern "rust-call" fn call_once(self, (z,): (String,)) -> usize {
|
2014-07-01 21:30:33 +00:00
|
|
|
self.x.len() + z.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let mut s = SFn {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
};
|
|
|
|
let sp = &mut s;
|
|
|
|
s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
|
2018-11-05 15:29:41 +00:00
|
|
|
use_mut(sp);
|
2014-07-01 21:30:33 +00:00
|
|
|
}
|
|
|
|
fn g() {
|
|
|
|
let s = SFnMut {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
};
|
2019-04-22 07:40:08 +00:00
|
|
|
s(3); //~ ERROR cannot borrow `s` as mutable, as it is not declared as mutable
|
2014-07-01 21:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn h() {
|
|
|
|
let s = SFnOnce {
|
|
|
|
x: "hello".to_string(),
|
|
|
|
};
|
|
|
|
s(" world".to_string());
|
|
|
|
s(" world".to_string()); //~ ERROR use of moved value: `s`
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|
2018-11-05 15:29:41 +00:00
|
|
|
|
|
|
|
fn use_mut<T>(_: &mut T) { }
|