Skip to content

Commit 9a4de3f

Browse files
committed
libsyntax: Introduce routines and remove all @fns from libsyntax save the old visitor
1 parent e959963 commit 9a4de3f

File tree

6 files changed

+85
-55
lines changed

6 files changed

+85
-55
lines changed

src/libextra/c_vec.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@
3636
* still held if needed.
3737
*/
3838

39-
40-
use std::option;
39+
use std::cast;
4140
use std::ptr;
41+
use std::routine::Runnable;
42+
use std::util;
4243

4344
/**
4445
* The type representing a foreign chunk of memory
45-
*
4646
*/
4747
pub struct CVec<T> {
4848
priv base: *mut T,
4949
priv len: uint,
50-
priv rsrc: @DtorRes
50+
priv rsrc: @DtorRes,
5151
}
5252

5353
struct DtorRes {
54-
dtor: Option<@fn()>,
54+
dtor: Option<~Runnable>,
5555
}
5656

5757
#[unsafe_destructor]
@@ -64,9 +64,11 @@ impl Drop for DtorRes {
6464
}
6565
}
6666

67-
fn DtorRes(dtor: Option<@fn()>) -> DtorRes {
68-
DtorRes {
69-
dtor: dtor
67+
impl DtorRes {
68+
fn new(dtor: Option<~Runnable>) -> DtorRes {
69+
DtorRes {
70+
dtor: dtor,
71+
}
7072
}
7173
}
7274

@@ -83,10 +85,10 @@ fn DtorRes(dtor: Option<@fn()>) -> DtorRes {
8385
* * len - The number of elements in the buffer
8486
*/
8587
pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
86-
return CVec{
88+
return CVec {
8789
base: base,
8890
len: len,
89-
rsrc: @DtorRes(option::None)
91+
rsrc: @DtorRes::new(None)
9092
};
9193
}
9294

@@ -101,12 +103,12 @@ pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
101103
* * dtor - A function to run when the value is destructed, useful
102104
* for freeing the buffer, etc.
103105
*/
104-
pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
105-
-> CVec<T> {
106+
pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: ~Runnable)
107+
-> CVec<T> {
106108
return CVec{
107109
base: base,
108110
len: len,
109-
rsrc: @DtorRes(option::Some(dtor))
111+
rsrc: @DtorRes::new(Some(dtor))
110112
};
111113
}
112114

@@ -153,6 +155,17 @@ mod tests {
153155

154156
use std::libc::*;
155157
use std::libc;
158+
use std::routine::Runnable;
159+
160+
struct LibcFree {
161+
mem: *c_void,
162+
}
163+
164+
impl Runnable for LibcFree {
165+
fn run(~self) {
166+
libc::free(self.mem)
167+
}
168+
}
156169

157170
fn malloc(n: size_t) -> CVec<u8> {
158171
#[fixed_stack_segment];
@@ -163,12 +176,11 @@ mod tests {
163176

164177
assert!(mem as int != 0);
165178

166-
return c_vec_with_dtor(mem as *mut u8, n as uint, || f(mem));
167-
}
168-
169-
fn f(mem: *c_void) {
170-
#[fixed_stack_segment]; #[inline(never)];
171-
unsafe { libc::free(mem) }
179+
return c_vec_with_dtor(mem as *mut u8,
180+
n as uint,
181+
~LibcFree {
182+
mem: mem,
183+
} as ~Runnable);
172184
}
173185
}
174186

src/libstd/routine.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/*!
12+
* Routines are like closures except that they own their arguments and can
13+
* only run once.
14+
*/
15+
16+
/// A routine that takes no arguments and returns nothing.
17+
pub trait Runnable {
18+
/// The entry point for the routine.
19+
fn run(~self);
20+
}
21+
22+
/// A convenience routine that does nothing.
23+
pub struct NoOpRunnable;
24+
25+
impl Runnable for NoOpRunnable {
26+
fn run(~self) {}
27+
}
28+

src/libstd/std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub mod reflect;
189189
pub mod condition;
190190
pub mod logging;
191191
pub mod util;
192-
192+
pub mod routine;
193193

194194
/* Unsupported interfaces */
195195

src/libstd/unstable/extfmt.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,14 @@ pub mod ct {
158158

159159
// A fragment of the output sequence
160160
#[deriving(Eq)]
161-
pub enum Piece { PieceString(~str), PieceConv(Conv), }
161+
pub enum Piece {
162+
PieceString(~str),
163+
PieceConv(Conv),
164+
}
162165

163-
pub type ErrorFn = @fn(&str) -> !;
166+
pub type ErrorFn<'self> = &'self fn(&str) -> !;
164167

165-
pub fn parse_fmt_string(s: &str, err: ErrorFn) -> ~[Piece] {
168+
pub fn parse_fmt_string<'a>(s: &str, err: ErrorFn<'a>) -> ~[Piece] {
166169
fn push_slice(ps: &mut ~[Piece], s: &str, from: uint, to: uint) {
167170
if to > from {
168171
ps.push(PieceString(s.slice(from, to).to_owned()));
@@ -185,7 +188,10 @@ pub mod ct {
185188
i += 1;
186189
} else {
187190
push_slice(&mut pieces, s, h, i - 1);
188-
let Parsed {val, next} = parse_conversion(s, i, lim, err);
191+
let Parsed {
192+
val,
193+
next
194+
} = parse_conversion(s, i, lim, |s| err(s));
189195
pieces.push(val);
190196
i = next;
191197
}
@@ -224,8 +230,8 @@ pub mod ct {
224230
}
225231
}
226232

227-
pub fn parse_conversion(s: &str, i: uint, lim: uint, err: ErrorFn) ->
228-
Parsed<Piece> {
233+
pub fn parse_conversion<'a>(s: &str, i: uint, lim: uint, err: ErrorFn<'a>)
234+
-> Parsed<Piece> {
229235
let param = parse_parameter(s, i, lim);
230236
// avoid copying ~[Flag] by destructuring
231237
let Parsed {val: flags_val, next: flags_next} = parse_flags(s,
@@ -308,8 +314,8 @@ pub mod ct {
308314
}
309315
}
310316

311-
pub fn parse_type(s: &str, i: uint, lim: uint, err: ErrorFn) ->
312-
Parsed<Ty> {
317+
pub fn parse_type<'a>(s: &str, i: uint, lim: uint, err: ErrorFn<'a>)
318+
-> Parsed<Ty> {
313319
if i >= lim { err("missing type in conversion"); }
314320

315321
// FIXME (#2249): Do we really want two signed types here?

src/libsyntax/ext/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
3838
fn parse_fmt_err_(cx: @ExtCtxt, sp: Span, msg: &str) -> ! {
3939
cx.span_fatal(sp, msg);
4040
}
41-
let parse_fmt_err: @fn(&str) -> ! = |s| parse_fmt_err_(cx, fmtspan, s);
41+
let parse_fmt_err: &fn(&str) -> ! = |s| parse_fmt_err_(cx, fmtspan, s);
4242
let pieces = parse_fmt_string(fmt, parse_fmt_err);
4343
MRExpr(pieces_to_expr(cx, sp, pieces, args))
4444
}

src/libsyntax/fold.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -869,35 +869,19 @@ mod test {
869869
use parse::token;
870870
use print::pprust;
871871
use super::*;
872-
873-
struct IdentFolder {
874-
f: @fn(ast::ident)->ast::ident,
875-
}
876-
877-
impl ast_fold for IdentFolder {
878-
fn fold_ident(@self, i: ident) -> ident {
879-
(self.f)(i)
880-
}
881-
}
882-
883-
// taken from expand
884-
// given a function from idents to idents, produce
885-
// an ast_fold that applies that function:
886-
pub fn fun_to_ident_folder(f: @fn(ast::ident)->ast::ident) -> @ast_fold {
887-
@IdentFolder {
888-
f: f,
889-
} as @ast_fold
890-
}
891-
872+
892873
// this version doesn't care about getting comments or docstrings in.
893874
fn fake_print_crate(s: @pprust::ps, crate: &ast::Crate) {
894875
pprust::print_mod(s, &crate.module, crate.attrs);
895876
}
896877

897878
// change every identifier to "zz"
898-
pub fn to_zz() -> @fn(ast::Ident)->ast::Ident {
899-
let zz_id = token::str_to_ident("zz");
900-
|_id| {zz_id}
879+
struct ToZzIdentFolder;
880+
881+
impl ast_fold for ToZzIdentFolder {
882+
fn fold_ident(&self, _: ident) -> ident {
883+
token::str_to_ident("zz")
884+
}
901885
}
902886

903887
// maybe add to expand.rs...
@@ -917,7 +901,7 @@ mod test {
917901

918902
// make sure idents get transformed everywhere
919903
#[test] fn ident_transformation () {
920-
let zz_fold = fun_to_ident_folder(to_zz());
904+
let zz_fold = ToZzIdentFolder;
921905
let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
922906
assert_pred!(matches_codepattern,
923907
"matches_codepattern",
@@ -928,7 +912,7 @@ mod test {
928912
929913
// even inside macro defs....
930914
#[test] fn ident_transformation_in_defs () {
931-
let zz_fold = fun_to_ident_folder(to_zz());
915+
let zz_fold = ToZzIdentFolder;
932916
let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
933917
=> (g $(d $d $e)+))} ");
934918
assert_pred!(matches_codepattern,
@@ -940,7 +924,7 @@ mod test {
940924
941925
// and in cast expressions... this appears to be an existing bug.
942926
#[test] fn ident_transformation_in_types () {
943-
let zz_fold = fun_to_ident_folder(to_zz());
927+
let zz_fold = ToZzIdentFolder;
944928
let ast = string_to_crate(@"fn a() {let z = 13 as int;}");
945929
assert_pred!(matches_codepattern,
946930
"matches_codepattern",

0 commit comments

Comments
 (0)