From cdbb3ca9b776b066e2c93acfb60da8537d2b1c9b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 7 Dec 2014 14:15:25 -0500 Subject: libstd: use unboxed closures --- src/libstd/io/extensions.rs | 9 +++++++-- src/libstd/io/mod.rs | 20 ++++++++++++------- src/libstd/io/net/ip.rs | 48 ++++++++++++++++++++++++--------------------- src/libstd/io/net/mod.rs | 7 +++++-- src/libstd/io/net/udp.rs | 5 ++++- src/libstd/io/stdio.rs | 10 +++++++--- 6 files changed, 62 insertions(+), 37 deletions(-) (limited to 'src/libstd/io') diff --git a/src/libstd/io/extensions.rs b/src/libstd/io/extensions.rs index 1bdf99f6d6d..69712e39d91 100644 --- a/src/libstd/io/extensions.rs +++ b/src/libstd/io/extensions.rs @@ -19,6 +19,7 @@ use io::{IoError, IoResult, Reader}; use io; use iter::Iterator; use num::Int; +use ops::FnOnce; use option::Option; use option::Option::{Some, None}; use ptr::RawPtr; @@ -76,7 +77,9 @@ impl<'r, R: Reader> Iterator> for Bytes<'r, R> { /// * `f`: A callback that receives the value. /// /// This function returns the value returned by the callback, for convenience. -pub fn u64_to_le_bytes(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { +pub fn u64_to_le_bytes(n: u64, size: uint, f: F) -> T where + F: FnOnce(&[u8]) -> T, +{ use mem::transmute; // 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(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { /// * `f`: A callback that receives the value. /// /// This function returns the value returned by the callback, for convenience. -pub fn u64_to_be_bytes(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { +pub fn u64_to_be_bytes(n: u64, size: uint, f: F) -> T where + F: FnOnce(&[u8]) -> T, +{ use mem::transmute; // LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index dc212e7cab3..bad86258bb8 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -233,7 +233,7 @@ use int; use iter::{Iterator, IteratorExt}; use kinds::Copy; use mem::transmute; -use ops::{BitOr, BitXor, BitAnd, Sub, Not}; +use ops::{BitOr, BitXor, BitAnd, Sub, Not, FnOnce}; use option::Option; use option::Option::{Some, None}; use os; @@ -426,18 +426,22 @@ impl Copy for IoErrorKind {} /// A trait that lets you add a `detail` to an IoError easily trait UpdateIoError { /// Returns an IoError with updated description and detail - fn update_err(self, desc: &'static str, detail: |&IoError| -> String) -> Self; + fn update_err(self, desc: &'static str, detail: D) -> Self where + D: FnOnce(&IoError) -> String; /// Returns an IoError with updated detail - fn update_detail(self, detail: |&IoError| -> String) -> Self; + fn update_detail(self, detail: D) -> Self where + D: FnOnce(&IoError) -> String; /// Returns an IoError with update description fn update_desc(self, desc: &'static str) -> Self; } impl UpdateIoError for IoResult { - fn update_err(self, desc: &'static str, detail: |&IoError| -> String) -> IoResult { - self.map_err(|mut e| { + fn update_err(self, desc: &'static str, detail: D) -> IoResult where + D: FnOnce(&IoError) -> String, + { + self.map_err(move |mut e| { let detail = detail(&e); e.desc = desc; e.detail = Some(detail); @@ -445,8 +449,10 @@ impl UpdateIoError for IoResult { }) } - fn update_detail(self, detail: |&IoError| -> String) -> IoResult { - self.map_err(|mut e| { e.detail = Some(detail(&e)); e }) + fn update_detail(self, detail: D) -> IoResult where + D: FnOnce(&IoError) -> String, + { + self.map_err(move |mut e| { e.detail = Some(detail(&e)); e }) } fn update_desc(self, desc: &'static str) -> IoResult { diff --git a/src/libstd/io/net/ip.rs b/src/libstd/io/net/ip.rs index f59dd37c0da..62965c48a26 100644 --- a/src/libstd/io/net/ip.rs +++ b/src/libstd/io/net/ip.rs @@ -22,6 +22,7 @@ use kinds::Copy; use io::{mod, IoResult, IoError}; use io::net; use iter::{Iterator, IteratorExt}; +use ops::FnOnce; use option::Option; use option::Option::{None, Some}; use result::Result::{Ok, Err}; @@ -100,8 +101,9 @@ impl<'a> Parser<'a> { } // Commit only if parser returns Some - fn read_atomically(&mut self, cb: |&mut Parser| -> Option) - -> Option { + fn read_atomically(&mut self, cb: F) -> Option where + F: FnOnce(&mut Parser) -> Option, + { let pos = self.pos; let r = cb(self); if r.is_none() { @@ -111,9 +113,10 @@ impl<'a> Parser<'a> { } // Commit only if parser read till EOF - fn read_till_eof(&mut self, cb: |&mut Parser| -> Option) - -> Option { - self.read_atomically(|p| { + fn read_till_eof(&mut self, cb: F) -> Option where + F: FnOnce(&mut Parser) -> Option, + { + self.read_atomically(move |p| { match cb(p) { Some(x) => if p.is_eof() {Some(x)} else {None}, None => None, @@ -134,15 +137,16 @@ impl<'a> Parser<'a> { } // Apply 3 parsers sequentially - fn read_seq_3( - &mut self, - pa: |&mut Parser| -> Option, - pb: |&mut Parser| -> Option, - pc: |&mut Parser| -> Option) - -> Option<(A, B, C)> { - self.read_atomically(|p| { + fn read_seq_3(&mut self, + pa: PA, + pb: PB, + pc: PC) + -> Option<(A, B, C)> where + PA: FnOnce(&mut Parser) -> Option, + PB: FnOnce(&mut Parser) -> Option, + PC: FnOnce(&mut Parser) -> Option, + { + self.read_atomically(move |p| { let a = pa(p); let b = if a.is_some() { pb(p) } else { None }; let c = if b.is_some() { pc(p) } else { None }; @@ -327,22 +331,22 @@ impl<'a> Parser<'a> { } fn read_socket_addr(&mut self) -> Option { - let ip_addr = |p: &mut Parser| { + let ip_addr = |&: p: &mut Parser| { let ipv4_p = |p: &mut Parser| p.read_ip_addr(); let ipv6_p = |p: &mut Parser| { - let open_br = |p: &mut Parser| p.read_given_char('['); - let ip_addr = |p: &mut Parser| p.read_ipv6_addr(); - let clos_br = |p: &mut Parser| p.read_given_char(']'); - p.read_seq_3::(open_br, ip_addr, clos_br) + let open_br = |&: p: &mut Parser| p.read_given_char('['); + let ip_addr = |&: p: &mut Parser| p.read_ipv6_addr(); + let clos_br = |&: p: &mut Parser| p.read_given_char(']'); + p.read_seq_3::(open_br, ip_addr, clos_br) .map(|t| match t { (_, ip, _) => ip }) }; p.read_or(&mut [ipv4_p, ipv6_p]) }; - let colon = |p: &mut Parser| p.read_given_char(':'); - let port = |p: &mut Parser| p.read_number(10, 5, 0x10000).map(|n| n as u16); + let colon = |&: p: &mut Parser| p.read_given_char(':'); + let port = |&: p: &mut Parser| p.read_number(10, 5, 0x10000).map(|n| n as u16); // host, colon, port - self.read_seq_3::(ip_addr, colon, port) + self.read_seq_3::(ip_addr, colon, port) .map(|t| match t { (ip, _, port) => SocketAddr { ip: ip, port: port } }) } } diff --git a/src/libstd/io/net/mod.rs b/src/libstd/io/net/mod.rs index 09e5639bea9..2056933e6df 100644 --- a/src/libstd/io/net/mod.rs +++ b/src/libstd/io/net/mod.rs @@ -11,6 +11,7 @@ //! Networking I/O use io::{IoError, IoResult, InvalidInput}; +use ops::FnMut; use option::Option::None; use result::Result::{Ok, Err}; use self::ip::{SocketAddr, ToSocketAddr}; @@ -23,8 +24,10 @@ pub mod udp; pub mod ip; pub mod pipe; -fn with_addresses(addr: A, action: |SocketAddr| -> IoResult) - -> IoResult { +fn with_addresses(addr: A, mut action: F) -> IoResult where + A: ToSocketAddr, + F: FnMut(SocketAddr) -> IoResult, +{ const DEFAULT_ERROR: IoError = IoError { kind: InvalidInput, desc: "no addresses found for hostname", diff --git a/src/libstd/io/net/udp.rs b/src/libstd/io/net/udp.rs index a2ad365dd2a..b23921ba359 100644 --- a/src/libstd/io/net/udp.rs +++ b/src/libstd/io/net/udp.rs @@ -18,6 +18,7 @@ use clone::Clone; use io::net::ip::{SocketAddr, IpAddr, ToSocketAddr}; use io::{Reader, Writer, IoResult}; +use ops::FnOnce; use option::Option; use result::Result::{Ok, Err}; use sys::udp::UdpSocket as UdpSocketImp; @@ -210,7 +211,9 @@ impl UdpStream { /// Allows access to the underlying UDP socket owned by this stream. This /// is useful to, for example, use the socket to send data to hosts other /// than the one that this stream is connected to. - pub fn as_socket(&mut self, f: |&mut UdpSocket| -> T) -> T { + pub fn as_socket(&mut self, f: F) -> T where + F: FnOnce(&mut UdpSocket) -> T, + { f(&mut self.socket) } diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 48c333f0733..8438c9fb441 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -39,7 +39,7 @@ use libc; use mem; use option::Option; use option::Option::{Some, None}; -use ops::{Deref, DerefMut}; +use ops::{Deref, DerefMut, FnOnce}; use result::Result::{Ok, Err}; use rustrt; use rustrt::local::Local; @@ -85,7 +85,9 @@ enum StdSource { File(fs::FileDesc), } -fn src(fd: libc::c_int, _readable: bool, f: |StdSource| -> T) -> T { +fn src(fd: libc::c_int, _readable: bool, f: F) -> T where + F: FnOnce(StdSource) -> T, +{ match tty::TTY::new(fd) { Ok(tty) => f(TTY(tty)), Err(_) => f(File(fs::FileDesc::new(fd, false))), @@ -318,7 +320,9 @@ pub fn set_stderr(stderr: Box) -> Option> { // // io1 aliases io2 // }) // }) -fn with_task_stdout(f: |&mut Writer| -> IoResult<()>) { +fn with_task_stdout(f: F) where + F: FnOnce(&mut Writer) -> IoResult<()>, +{ let result = if Local::exists(None::) { let mut my_stdout = LOCAL_STDOUT.with(|slot| { slot.borrow_mut().take() -- cgit 1.4.1-3-g733a5