Skip to content

Commit f18b255

Browse files
author
Jorge Aparicio
committed
libcore: use unboxed closures in the finally module
1 parent e2a362f commit f18b255

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

src/libcore/finally.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,19 @@
3030
3131
#![experimental]
3232

33-
use ops::Drop;
33+
use ops::{Drop, FnMut, FnOnce};
3434

3535
/// A trait for executing a destructor unconditionally after a block of code,
3636
/// regardless of whether the blocked fails.
3737
pub trait Finally<T> {
3838
/// Executes this object, unconditionally running `dtor` after this block of
3939
/// code has run.
40-
fn finally(&mut self, dtor: ||) -> T;
40+
fn finally<F>(&mut self, dtor: F) -> T where F: FnMut();
4141
}
4242

43-
impl<'a,T> Finally<T> for ||: 'a -> T {
44-
fn finally(&mut self, dtor: ||) -> T {
45-
try_finally(&mut (), self,
46-
|_, f| (*f)(),
47-
|_| dtor())
48-
}
49-
}
50-
51-
impl<T> Finally<T> for fn() -> T {
52-
fn finally(&mut self, dtor: ||) -> T {
53-
try_finally(&mut (), (),
54-
|_, _| (*self)(),
55-
|_| dtor())
43+
impl<T, F> Finally<T> for F where F: FnMut() -> T {
44+
fn finally<G>(&mut self, mut dtor: G) -> T where G: FnMut() {
45+
try_finally(&mut (), self, |_, f| (*f)(), |_| dtor())
5646
}
5747
}
5848

@@ -86,25 +76,24 @@ impl<T> Finally<T> for fn() -> T {
8676
/// // use state.buffer, state.len to cleanup
8777
/// })
8878
/// ```
89-
pub fn try_finally<T,U,R>(mutate: &mut T,
90-
drop: U,
91-
try_fn: |&mut T, U| -> R,
92-
finally_fn: |&mut T|)
93-
-> R {
79+
pub fn try_finally<T, U, R, F, G>(mutate: &mut T, drop: U, try_fn: F, finally_fn: G) -> R where
80+
F: FnOnce(&mut T, U) -> R,
81+
G: FnMut(&mut T),
82+
{
9483
let f = Finallyalizer {
9584
mutate: mutate,
9685
dtor: finally_fn,
9786
};
9887
try_fn(&mut *f.mutate, drop)
9988
}
10089

101-
struct Finallyalizer<'a,A:'a> {
90+
struct Finallyalizer<'a, A:'a, F> where F: FnMut(&mut A) {
10291
mutate: &'a mut A,
103-
dtor: |&mut A|: 'a
92+
dtor: F,
10493
}
10594

10695
#[unsafe_destructor]
107-
impl<'a,A> Drop for Finallyalizer<'a,A> {
96+
impl<'a, A, F> Drop for Finallyalizer<'a, A, F> where F: FnMut(&mut A) {
10897
#[inline]
10998
fn drop(&mut self) {
11099
(self.dtor)(self.mutate);

0 commit comments

Comments
 (0)