File tree Expand file tree Collapse file tree 1 file changed +42
-1
lines changed Expand file tree Collapse file tree 1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ do || {
2525
2626use ops:: Drop ;
2727
28- #[ cfg( test) ] use task:: failing;
28+ #[ cfg( test) ] use task:: { failing, spawn } ;
2929
3030pub trait Finally < T > {
3131 fn finally ( & self , dtor : & fn ( ) ) -> T ;
@@ -41,6 +41,26 @@ impl<'self,T> Finally<T> for &'self fn() -> T {
4141 }
4242}
4343
44+ impl < T > Finally < T > for ~fn ( ) -> T {
45+ fn finally ( & self , dtor : & fn ( ) ) -> T {
46+ let _d = Finallyalizer {
47+ dtor : dtor
48+ } ;
49+
50+ ( * self ) ( )
51+ }
52+ }
53+
54+ impl < T > Finally < T > for @fn ( ) -> T {
55+ fn finally ( & self , dtor : & fn ( ) ) -> T {
56+ let _d = Finallyalizer {
57+ dtor : dtor
58+ } ;
59+
60+ ( * self ) ( )
61+ }
62+ }
63+
4464struct Finallyalizer < ' self > {
4565 dtor : & ' self fn ( )
4666}
@@ -96,3 +116,24 @@ fn test_compact() {
96116 do_some_fallible_work. finally (
97117 but_always_run_this_function) ;
98118}
119+
120+ #[ test]
121+ fn test_owned ( ) {
122+ fn spawn_with_finalizer ( f : ~fn ( ) ) {
123+ do spawn { do f. finally { } }
124+ }
125+ let owned: ~fn ( ) = || { } ;
126+ spawn_with_finalizer ( owned) ;
127+ }
128+
129+ #[ test]
130+ fn test_managed ( ) {
131+ let i = @mut 10 ;
132+ let managed: @fn ( ) -> int = || {
133+ let r = * i;
134+ * i += 10 ;
135+ r
136+ } ;
137+ assert ! ( do managed. finally { } == 10 ) ;
138+ assert ! ( * i == 20 ) ;
139+ }
You can’t perform that action at this time.
0 commit comments