@@ -32,7 +32,9 @@ macro_rules! uint_module (($T:ty) => (
/// ```
#[inline]
#[deprecated = "just use .to_string(), or a BufWriter with write! if you mustn't allocate"]
pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
pub fn to_str_bytes<U, F>(n: $T, radix: uint, f: F) -> U where
F: FnOnce(&[u8]) -> U,
{
use io::{Writer, Seek};
// The radix can be as low as 2, so we need at least 64 characters for a
// base 2 number, and then we need another for a possible '-' character.
@@ -40,7 +40,7 @@ use kinds::Copy;
use libc::{c_void, c_int};
use libc;
use boxed::Box;
use ops::Drop;
use ops::{Drop, FnOnce};
use option::Option;
use option::Option::{Some, None};
use os;
@@ -172,8 +172,9 @@ pub mod windoze {
use str::StrPrelude;
use vec::Vec;

pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD)
-> Option<String> {
pub fn fill_utf16_buf_and_decode<F>(mut f: F) -> Option<String> where
F: FnMut(*mut u16, DWORD) -> DWORD,
{

unsafe {
let mut n = TMPBUF_SZ as DWORD;
@@ -212,7 +213,9 @@ pub mod windoze {
Accessing environment variables is not generally threadsafe.
Serialize access through a global lock.
*/
fn with_env_lock<T>(f: || -> T) -> T {
fn with_env_lock<T, F>(f: F) -> T where
F: FnOnce() -> T,
{
use sync::{StaticMutex, MUTEX_INIT};

static LOCK: StaticMutex = MUTEX_INIT;
@@ -15,6 +15,7 @@

use int;
use mem::drop;
use ops::FnOnce;
use sync::atomic;
use sync::{StaticMutex, MUTEX_INIT};

@@ -57,7 +58,7 @@ impl Once {
///
/// When this function returns, it is guaranteed that some initialization
/// has run and completed (it may not be the closure specified).
pub fn doit(&'static self, f: ||) {
pub fn doit<F>(&'static self, f: F) where F: FnOnce() {
// Optimize common path: load is much cheaper than fetch_add.
if self.cnt.load(atomic::SeqCst) < 0 {
return
@@ -70,9 +70,10 @@ impl<M: Send> Helper<M> {
/// passed to the helper thread in a separate task.
///
/// This function is safe to be called many times.
pub fn boot<T: Send>(&'static self,
f: || -> T,
helper: fn(helper_signal::signal, Receiver<M>, T)) {
pub fn boot<T, F>(&'static self, f: F, helper: fn(helper_signal::signal, Receiver<M>, T)) where
T: Send,
F: FnOnce() -> T,
{
unsafe {
let _guard = self.lock.lock();
if !*self.initialized.get() {
@@ -69,7 +69,9 @@ pub fn mkerr_libc<T: Int>(ret: T) -> IoResult<()> {
}
}

pub fn keep_going(data: &[u8], f: |*const u8, uint| -> i64) -> i64 {
pub fn keep_going<F>(data: &[u8], mut f: F) -> i64 where
F: FnMut(*const u8, uint) -> i64,
{
let origamt = data.len();
let mut data = data.as_ptr();
let mut amt = origamt;
@@ -344,10 +344,10 @@ pub fn get_host_addresses(host: Option<&str>, servname: Option<&str>,
// [1] http://twistedmatrix.com/pipermail/twisted-commits/2012-April/034692.html
// [2] http://stackoverflow.com/questions/19819198/does-send-msg-dontwait

pub fn read<T>(fd: sock_t,
deadline: u64,
lock: || -> T,
read: |bool| -> libc::c_int) -> IoResult<uint> {
pub fn read<T, L, R>(fd: sock_t, deadline: u64, mut lock: L, mut read: R) -> IoResult<uint> where
L: FnMut() -> T,
R: FnMut(bool) -> libc::c_int,
{
let mut ret = -1;
if deadline == 0 {
ret = retry(|| read(false));
@@ -386,12 +386,15 @@ pub fn read<T>(fd: sock_t,
}
}

pub fn write<T>(fd: sock_t,
deadline: u64,
buf: &[u8],
write_everything: bool,
lock: || -> T,
write: |bool, *const u8, uint| -> i64) -> IoResult<uint> {
pub fn write<T, L, W>(fd: sock_t,
deadline: u64,
buf: &[u8],
write_everything: bool,
mut lock: L,
mut write: W) -> IoResult<uint> where
L: FnMut() -> T,
W: FnMut(bool, *const u8, uint) -> i64,
{
let mut ret = -1;
let mut written = 0;
if deadline == 0 {
@@ -674,8 +677,8 @@ impl TcpStream {

pub fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
let fd = self.fd();
let dolock = || self.lock_nonblocking();
let doread = |nb| unsafe {
let dolock = |&:| self.lock_nonblocking();
let doread = |&mut: nb| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::recv(fd,
buf.as_mut_ptr() as *mut libc::c_void,
@@ -687,8 +690,8 @@ impl TcpStream {

pub fn write(&mut self, buf: &[u8]) -> IoResult<()> {
let fd = self.fd();
let dolock = || self.lock_nonblocking();
let dowrite = |nb: bool, buf: *const u8, len: uint| unsafe {
let dolock = |&:| self.lock_nonblocking();
let dowrite = |&: nb: bool, buf: *const u8, len: uint| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::send(fd,
buf as *const _,
@@ -822,7 +825,7 @@ impl UdpSocket {
let mut addrlen: libc::socklen_t =
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

let dolock = || self.lock_nonblocking();
let dolock = |&:| self.lock_nonblocking();
let n = try!(read(fd, self.read_deadline, dolock, |nb| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::recvfrom(fd,
@@ -843,8 +846,8 @@ impl UdpSocket {
let dstp = &storage as *const _ as *const libc::sockaddr;

let fd = self.fd();
let dolock = || self.lock_nonblocking();
let dowrite = |nb, buf: *const u8, len: uint| unsafe {
let dolock = |&: | self.lock_nonblocking();
let dowrite = |&mut: nb, buf: *const u8, len: uint| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::sendto(fd,
buf as *const libc::c_void,
@@ -125,7 +125,10 @@ pub fn decode_error_detailed(errno: i32) -> IoError {
}

#[inline]
pub fn retry<T: SignedInt> (f: || -> T) -> T {
pub fn retry<T, F> (mut f: F) -> T where
T: SignedInt,
F: FnMut() -> T,
{
let one: T = Int::one();
loop {
let n = f();
@@ -149,8 +149,8 @@ impl UnixStream {

pub fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
let fd = self.fd();
let dolock = || self.lock_nonblocking();
let doread = |nb| unsafe {
let dolock = |&:| self.lock_nonblocking();
let doread = |&mut: nb| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::recv(fd,
buf.as_mut_ptr() as *mut libc::c_void,
@@ -162,8 +162,8 @@ impl UnixStream {

pub fn write(&mut self, buf: &[u8]) -> IoResult<()> {
let fd = self.fd();
let dolock = || self.lock_nonblocking();
let dowrite = |nb: bool, buf: *const u8, len: uint| unsafe {
let dolock = |&: | self.lock_nonblocking();
let dowrite = |&: nb: bool, buf: *const u8, len: uint| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::send(fd,
buf as *const _,
@@ -138,7 +138,7 @@ pub fn decode_error_detailed(errno: i32) -> IoError {
}

#[inline]
pub fn retry<I> (f: || -> I) -> I { f() } // PR rust-lang/rust/#17020
pub fn retry<I, F>(f: F) -> I where F: FnOnce() -> I { f() } // PR rust-lang/rust/#17020

pub fn ms_to_timeval(ms: u64) -> libc::timeval {
libc::timeval {
@@ -418,9 +418,8 @@ fn make_command_line(prog: &CString, args: &[CString]) -> String {
}
}

fn with_envp<K, V, T>(env: Option<&collections::HashMap<K, V>>,
cb: |*mut c_void| -> T) -> T
where K: BytesContainer + Eq + Hash, V: BytesContainer
fn with_envp<K, V, T, F>(env: Option<&collections::HashMap<K, V>>, cb: F) -> T where
K: BytesContainer + Eq + Hash, V: BytesContainer, F: FnOnce(*mut c_void) -> T,
{
// On Windows we pass an "environment block" which is not a char**, but
// rather a concatenation of null-terminated k=v\0 sequences, with a final
@@ -445,7 +444,9 @@ fn with_envp<K, V, T>(env: Option<&collections::HashMap<K, V>>,
}
}

fn with_dirp<T>(d: Option<&CString>, cb: |*const u16| -> T) -> T {
fn with_dirp<T, F>(d: Option<&CString>, cb: F) -> T where
F: FnOnce(*const u16) -> T,
{
match d {
Some(dir) => {
let dir_str = dir.as_str()
@@ -381,7 +381,9 @@ mod test {
rx.recv();
}

fn avoid_copying_the_body(spawnfn: |v: proc():Send|) {
fn avoid_copying_the_body<F>(spawnfn: F) where
F: FnOnce(proc():Send),
{
let (tx, rx) = channel::<uint>();

let x = box 1;
@@ -218,7 +218,9 @@ impl<T: 'static> Key<T> {
/// This function will `panic!()` if the key currently has its
/// destructor running, and it **may** panic if the destructor has
/// previously been run for this thread.
pub fn with<R>(&'static self, f: |&T| -> R) -> R {
pub fn with<R, F>(&'static self, f: F) -> R where
F: FnOnce(&T) -> R,
{
let slot = (self.inner)();
unsafe {
let slot = slot.get().expect("cannot access a TLS value during or \
@@ -135,7 +135,9 @@ impl<T> Key<T> {
/// assert_eq!(val, 100);
/// });
/// ```
pub fn set<R>(&'static self, t: &T, cb: || -> R) -> R {
pub fn set<R, F>(&'static self, t: &T, cb: F) -> R where
F: FnOnce() -> R,
{
struct Reset<'a, T: 'a> {
key: &'a KeyInner<T>,
val: *mut T,
@@ -175,7 +177,9 @@ impl<T> Key<T> {
/// // work with `slot`
/// });
/// ```
pub fn with<R>(&'static self, cb: |&T| -> R) -> R {
pub fn with<R, F>(&'static self, cb: F) -> R where
F: FnOnce(&T) -> R
{
unsafe {
let ptr = self.inner.get();
assert!(!ptr.is_null(), "cannot access a scoped thread local \
@@ -14,7 +14,7 @@

use {fmt, i64};
use kinds::Copy;
use ops::{Add, Sub, Mul, Div, Neg};
use ops::{Add, Sub, Mul, Div, Neg, FnOnce};
use option::Option;
use option::Option::{Some, None};
use num::Int;
@@ -141,7 +141,7 @@ impl Duration {

/// Runs a closure, returning the duration of time it took to run the
/// closure.
pub fn span(f: ||) -> Duration {
pub fn span<F>(f: F) -> Duration where F: FnOnce() {
let before = super::precise_time_ns();
f();
Duration::nanoseconds((super::precise_time_ns() - before) as i64)