Skip to content

Commit cdbb3ca

Browse files
author
Jorge Aparicio
committed
libstd: use unboxed closures
1 parent be53d61 commit cdbb3ca

29 files changed

+171
-95
lines changed

src/libstd/ascii.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use fmt;
2020
use iter::IteratorExt;
2121
use kinds::Copy;
2222
use mem;
23+
use ops::FnMut;
2324
use option::Option;
2425
use option::Option::{Some, None};
2526
use slice::{SlicePrelude, AsSlice};
@@ -527,7 +528,9 @@ impl OwnedAsciiExt for Vec<u8> {
527528
/// - Any other chars are given hex escapes.
528529
/// - Unicode escapes are never generated by this function.
529530
#[unstable = "needs to be updated to use an iterator"]
530-
pub fn escape_default(c: u8, f: |u8|) {
531+
pub fn escape_default<F>(c: u8, mut f: F) where
532+
F: FnMut(u8),
533+
{
531534
match c {
532535
b'\t' => { f(b'\\'); f(b't'); }
533536
b'\r' => { f(b'\\'); f(b'r'); }

src/libstd/collections/hash/map.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use iter::{mod, Iterator, IteratorExt, FromIterator, Extend};
2424
use kinds::Sized;
2525
use mem::{mod, replace};
2626
use num::{Int, UnsignedInt};
27-
use ops::{Deref, Index, IndexMut};
27+
use ops::{Deref, FnMut, Index, IndexMut};
2828
use option::Option;
2929
use option::Option::{Some, None};
3030
use result::Result;
@@ -296,10 +296,13 @@ pub struct HashMap<K, V, H = RandomSipHasher> {
296296
}
297297

298298
/// Search for a pre-hashed key.
299-
fn search_hashed<K, V, M: Deref<RawTable<K, V>>>(table: M,
300-
hash: &SafeHash,
301-
is_match: |&K| -> bool)
302-
-> SearchResult<K, V, M> {
299+
fn search_hashed<K, V, M, F>(table: M,
300+
hash: &SafeHash,
301+
mut is_match: F)
302+
-> SearchResult<K, V, M> where
303+
M: Deref<RawTable<K, V>>,
304+
F: FnMut(&K) -> bool,
305+
{
303306
let size = table.size();
304307
let mut probe = Bucket::new(table, hash);
305308
let ib = probe.index();
@@ -749,12 +752,14 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
749752
self.insert_or_replace_with(hash, k, v, |_, _, _| ())
750753
}
751754

752-
fn insert_or_replace_with<'a>(&'a mut self,
753-
hash: SafeHash,
754-
k: K,
755-
v: V,
756-
found_existing: |&mut K, &mut V, V|)
757-
-> &'a mut V {
755+
fn insert_or_replace_with<'a, F>(&'a mut self,
756+
hash: SafeHash,
757+
k: K,
758+
v: V,
759+
mut found_existing: F)
760+
-> &'a mut V where
761+
F: FnMut(&mut K, &mut V, V),
762+
{
758763
// Worst case, we'll find one empty bucket among `size + 1` buckets.
759764
let size = self.table.size();
760765
let mut probe = Bucket::new(&mut self.table, &hash);

src/libstd/dynamic_lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub mod dl {
216216
use c_str::{CString, ToCStr};
217217
use libc;
218218
use kinds::Copy;
219+
use ops::FnOnce;
219220
use ptr;
220221
use result::*;
221222
use result::Result::{Err, Ok};
@@ -231,7 +232,9 @@ pub mod dl {
231232
dlopen(ptr::null(), Lazy as libc::c_int) as *mut u8
232233
}
233234

234-
pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, String> {
235+
pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
236+
F: FnOnce() -> T,
237+
{
235238
use sync::{StaticMutex, MUTEX_INIT};
236239
static LOCK: StaticMutex = MUTEX_INIT;
237240
unsafe {
@@ -312,7 +315,9 @@ pub mod dl {
312315
handle as *mut u8
313316
}
314317

315-
pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, String> {
318+
pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
319+
F: FnOnce() -> T,
320+
{
316321
unsafe {
317322
SetLastError(0);
318323

src/libstd/io/extensions.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use io::{IoError, IoResult, Reader};
1919
use io;
2020
use iter::Iterator;
2121
use num::Int;
22+
use ops::FnOnce;
2223
use option::Option;
2324
use option::Option::{Some, None};
2425
use ptr::RawPtr;
@@ -76,7 +77,9 @@ impl<'r, R: Reader> Iterator<IoResult<u8>> for Bytes<'r, R> {
7677
/// * `f`: A callback that receives the value.
7778
///
7879
/// This function returns the value returned by the callback, for convenience.
79-
pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
80+
pub fn u64_to_le_bytes<T, F>(n: u64, size: uint, f: F) -> T where
81+
F: FnOnce(&[u8]) -> T,
82+
{
8083
use mem::transmute;
8184

8285
// LLVM fails to properly optimize this when using shifts instead of the to_le* intrinsics
@@ -115,7 +118,9 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
115118
/// * `f`: A callback that receives the value.
116119
///
117120
/// This function returns the value returned by the callback, for convenience.
118-
pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
121+
pub fn u64_to_be_bytes<T, F>(n: u64, size: uint, f: F) -> T where
122+
F: FnOnce(&[u8]) -> T,
123+
{
119124
use mem::transmute;
120125

121126
// LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics

src/libstd/io/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ use int;
233233
use iter::{Iterator, IteratorExt};
234234
use kinds::Copy;
235235
use mem::transmute;
236-
use ops::{BitOr, BitXor, BitAnd, Sub, Not};
236+
use ops::{BitOr, BitXor, BitAnd, Sub, Not, FnOnce};
237237
use option::Option;
238238
use option::Option::{Some, None};
239239
use os;
@@ -426,27 +426,33 @@ impl Copy for IoErrorKind {}
426426
/// A trait that lets you add a `detail` to an IoError easily
427427
trait UpdateIoError<T> {
428428
/// Returns an IoError with updated description and detail
429-
fn update_err(self, desc: &'static str, detail: |&IoError| -> String) -> Self;
429+
fn update_err<D>(self, desc: &'static str, detail: D) -> Self where
430+
D: FnOnce(&IoError) -> String;
430431

431432
/// Returns an IoError with updated detail
432-
fn update_detail(self, detail: |&IoError| -> String) -> Self;
433+
fn update_detail<D>(self, detail: D) -> Self where
434+
D: FnOnce(&IoError) -> String;
433435

434436
/// Returns an IoError with update description
435437
fn update_desc(self, desc: &'static str) -> Self;
436438
}
437439

438440
impl<T> UpdateIoError<T> for IoResult<T> {
439-
fn update_err(self, desc: &'static str, detail: |&IoError| -> String) -> IoResult<T> {
440-
self.map_err(|mut e| {
441+
fn update_err<D>(self, desc: &'static str, detail: D) -> IoResult<T> where
442+
D: FnOnce(&IoError) -> String,
443+
{
444+
self.map_err(move |mut e| {
441445
let detail = detail(&e);
442446
e.desc = desc;
443447
e.detail = Some(detail);
444448
e
445449
})
446450
}
447451

448-
fn update_detail(self, detail: |&IoError| -> String) -> IoResult<T> {
449-
self.map_err(|mut e| { e.detail = Some(detail(&e)); e })
452+
fn update_detail<D>(self, detail: D) -> IoResult<T> where
453+
D: FnOnce(&IoError) -> String,
454+
{
455+
self.map_err(move |mut e| { e.detail = Some(detail(&e)); e })
450456
}
451457

452458
fn update_desc(self, desc: &'static str) -> IoResult<T> {

src/libstd/io/net/ip.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use kinds::Copy;
2222
use io::{mod, IoResult, IoError};
2323
use io::net;
2424
use iter::{Iterator, IteratorExt};
25+
use ops::FnOnce;
2526
use option::Option;
2627
use option::Option::{None, Some};
2728
use result::Result::{Ok, Err};
@@ -100,8 +101,9 @@ impl<'a> Parser<'a> {
100101
}
101102

102103
// Commit only if parser returns Some
103-
fn read_atomically<T>(&mut self, cb: |&mut Parser| -> Option<T>)
104-
-> Option<T> {
104+
fn read_atomically<T, F>(&mut self, cb: F) -> Option<T> where
105+
F: FnOnce(&mut Parser) -> Option<T>,
106+
{
105107
let pos = self.pos;
106108
let r = cb(self);
107109
if r.is_none() {
@@ -111,9 +113,10 @@ impl<'a> Parser<'a> {
111113
}
112114

113115
// Commit only if parser read till EOF
114-
fn read_till_eof<T>(&mut self, cb: |&mut Parser| -> Option<T>)
115-
-> Option<T> {
116-
self.read_atomically(|p| {
116+
fn read_till_eof<T, F>(&mut self, cb: F) -> Option<T> where
117+
F: FnOnce(&mut Parser) -> Option<T>,
118+
{
119+
self.read_atomically(move |p| {
117120
match cb(p) {
118121
Some(x) => if p.is_eof() {Some(x)} else {None},
119122
None => None,
@@ -134,15 +137,16 @@ impl<'a> Parser<'a> {
134137
}
135138

136139
// Apply 3 parsers sequentially
137-
fn read_seq_3<A,
138-
B,
139-
C>(
140-
&mut self,
141-
pa: |&mut Parser| -> Option<A>,
142-
pb: |&mut Parser| -> Option<B>,
143-
pc: |&mut Parser| -> Option<C>)
144-
-> Option<(A, B, C)> {
145-
self.read_atomically(|p| {
140+
fn read_seq_3<A, B, C, PA, PB, PC>(&mut self,
141+
pa: PA,
142+
pb: PB,
143+
pc: PC)
144+
-> Option<(A, B, C)> where
145+
PA: FnOnce(&mut Parser) -> Option<A>,
146+
PB: FnOnce(&mut Parser) -> Option<B>,
147+
PC: FnOnce(&mut Parser) -> Option<C>,
148+
{
149+
self.read_atomically(move |p| {
146150
let a = pa(p);
147151
let b = if a.is_some() { pb(p) } else { None };
148152
let c = if b.is_some() { pc(p) } else { None };
@@ -327,22 +331,22 @@ impl<'a> Parser<'a> {
327331
}
328332

329333
fn read_socket_addr(&mut self) -> Option<SocketAddr> {
330-
let ip_addr = |p: &mut Parser| {
334+
let ip_addr = |&: p: &mut Parser| {
331335
let ipv4_p = |p: &mut Parser| p.read_ip_addr();
332336
let ipv6_p = |p: &mut Parser| {
333-
let open_br = |p: &mut Parser| p.read_given_char('[');
334-
let ip_addr = |p: &mut Parser| p.read_ipv6_addr();
335-
let clos_br = |p: &mut Parser| p.read_given_char(']');
336-
p.read_seq_3::<char, IpAddr, char>(open_br, ip_addr, clos_br)
337+
let open_br = |&: p: &mut Parser| p.read_given_char('[');
338+
let ip_addr = |&: p: &mut Parser| p.read_ipv6_addr();
339+
let clos_br = |&: p: &mut Parser| p.read_given_char(']');
340+
p.read_seq_3::<char, IpAddr, char, _, _, _>(open_br, ip_addr, clos_br)
337341
.map(|t| match t { (_, ip, _) => ip })
338342
};
339343
p.read_or(&mut [ipv4_p, ipv6_p])
340344
};
341-
let colon = |p: &mut Parser| p.read_given_char(':');
342-
let port = |p: &mut Parser| p.read_number(10, 5, 0x10000).map(|n| n as u16);
345+
let colon = |&: p: &mut Parser| p.read_given_char(':');
346+
let port = |&: p: &mut Parser| p.read_number(10, 5, 0x10000).map(|n| n as u16);
343347

344348
// host, colon, port
345-
self.read_seq_3::<IpAddr, char, u16>(ip_addr, colon, port)
349+
self.read_seq_3::<IpAddr, char, u16, _, _, _>(ip_addr, colon, port)
346350
.map(|t| match t { (ip, _, port) => SocketAddr { ip: ip, port: port } })
347351
}
348352
}

src/libstd/io/net/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Networking I/O
1212
1313
use io::{IoError, IoResult, InvalidInput};
14+
use ops::FnMut;
1415
use option::Option::None;
1516
use result::Result::{Ok, Err};
1617
use self::ip::{SocketAddr, ToSocketAddr};
@@ -23,8 +24,10 @@ pub mod udp;
2324
pub mod ip;
2425
pub mod pipe;
2526

26-
fn with_addresses<A: ToSocketAddr, T>(addr: A, action: |SocketAddr| -> IoResult<T>)
27-
-> IoResult<T> {
27+
fn with_addresses<A, T, F>(addr: A, mut action: F) -> IoResult<T> where
28+
A: ToSocketAddr,
29+
F: FnMut(SocketAddr) -> IoResult<T>,
30+
{
2831
const DEFAULT_ERROR: IoError = IoError {
2932
kind: InvalidInput,
3033
desc: "no addresses found for hostname",

src/libstd/io/net/udp.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use clone::Clone;
1919
use io::net::ip::{SocketAddr, IpAddr, ToSocketAddr};
2020
use io::{Reader, Writer, IoResult};
21+
use ops::FnOnce;
2122
use option::Option;
2223
use result::Result::{Ok, Err};
2324
use sys::udp::UdpSocket as UdpSocketImp;
@@ -210,7 +211,9 @@ impl UdpStream {
210211
/// Allows access to the underlying UDP socket owned by this stream. This
211212
/// is useful to, for example, use the socket to send data to hosts other
212213
/// than the one that this stream is connected to.
213-
pub fn as_socket<T>(&mut self, f: |&mut UdpSocket| -> T) -> T {
214+
pub fn as_socket<T, F>(&mut self, f: F) -> T where
215+
F: FnOnce(&mut UdpSocket) -> T,
216+
{
214217
f(&mut self.socket)
215218
}
216219

src/libstd/io/stdio.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use libc;
3939
use mem;
4040
use option::Option;
4141
use option::Option::{Some, None};
42-
use ops::{Deref, DerefMut};
42+
use ops::{Deref, DerefMut, FnOnce};
4343
use result::Result::{Ok, Err};
4444
use rustrt;
4545
use rustrt::local::Local;
@@ -85,7 +85,9 @@ enum StdSource {
8585
File(fs::FileDesc),
8686
}
8787

88-
fn src<T>(fd: libc::c_int, _readable: bool, f: |StdSource| -> T) -> T {
88+
fn src<T, F>(fd: libc::c_int, _readable: bool, f: F) -> T where
89+
F: FnOnce(StdSource) -> T,
90+
{
8991
match tty::TTY::new(fd) {
9092
Ok(tty) => f(TTY(tty)),
9193
Err(_) => f(File(fs::FileDesc::new(fd, false))),
@@ -318,7 +320,9 @@ pub fn set_stderr(stderr: Box<Writer + Send>) -> Option<Box<Writer + Send>> {
318320
// // io1 aliases io2
319321
// })
320322
// })
321-
fn with_task_stdout(f: |&mut Writer| -> IoResult<()>) {
323+
fn with_task_stdout<F>(f: F) where
324+
F: FnOnce(&mut Writer) -> IoResult<()>,
325+
{
322326
let result = if Local::exists(None::<Task>) {
323327
let mut my_stdout = LOCAL_STDOUT.with(|slot| {
324328
slot.borrow_mut().take()

src/libstd/num/strconv.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use char::Char;
2121
use kinds::Copy;
2222
use num;
2323
use num::{Int, Float, FPNaN, FPInfinite, ToPrimitive};
24+
use ops::FnMut;
2425
use slice::{SlicePrelude, CloneSliceAllocPrelude};
2526
use str::StrPrelude;
2627
use string::String;
@@ -93,7 +94,10 @@ impl Copy for SignFormat {}
9394
/// # Panics
9495
///
9596
/// - Panics if `radix` < 2 or `radix` > 36.
96-
fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f: |u8|) {
97+
fn int_to_str_bytes_common<T, F>(num: T, radix: uint, sign: SignFormat, mut f: F) where
98+
T: Int,
99+
F: FnMut(u8),
100+
{
97101
assert!(2 <= radix && radix <= 36);
98102

99103
let _0: T = Int::zero();

src/libstd/num/u16.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
pub use core::u16::{BITS, BYTES, MIN, MAX};
1717

18+
use ops::FnOnce;
19+
1820
uint_module!(u16)

src/libstd/num/u32.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
pub use core::u32::{BITS, BYTES, MIN, MAX};
1717

18+
use ops::FnOnce;
19+
1820
uint_module!(u32)

src/libstd/num/u64.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
pub use core::u64::{BITS, BYTES, MIN, MAX};
1717

18+
use ops::FnOnce;
19+
1820
uint_module!(u64)

src/libstd/num/u8.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
pub use core::u8::{BITS, BYTES, MIN, MAX};
1717

18+
use ops::FnOnce;
19+
1820
uint_module!(u8)

src/libstd/num/uint.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
pub use core::uint::{BITS, BYTES, MIN, MAX};
1717

18+
use ops::FnOnce;
19+
1820
uint_module!(uint)

0 commit comments

Comments
 (0)