diff options
Diffstat (limited to 'src/libstd/io/mod.rs')
| -rw-r--r-- | src/libstd/io/mod.rs | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 403e0e48fd5..ccff857f606 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -97,8 +97,8 @@ Some examples of obvious things you might want to do ```rust # fn main() { } # fn foo() { - # #[allow(unused_must_use, dead_code)]; - use std::io::net::tcp::TcpListener; + # #![allow(dead_code)] + use std::io::{TcpListener, TcpStream}; use std::io::net::ip::{Ipv4Addr, SocketAddr}; use std::io::{Acceptor, Listener}; @@ -108,12 +108,19 @@ Some examples of obvious things you might want to do // bind the listener to the specified address let mut acceptor = listener.listen(); - // accept connections and process them - # fn handle_client<T>(_: T) {} + fn handle_client(mut stream: TcpStream) { + // ... + # &mut stream; // silence unused mutability/variable warning + } + // accept connections and process them, spawning a new tasks for each one for stream in acceptor.incoming() { - spawn(proc() { - handle_client(stream); - }); + match stream { + Err(e) => { /* connection failed */ } + Ok(stream) => spawn(proc() { + // connection succeeded + handle_client(stream) + }) + } } // close the socket server @@ -225,8 +232,8 @@ use str::{StrSlice, OwnedStr}; use str; use uint; use unstable::finally::try_finally; -use slice::{OwnedVector, MutableVector, ImmutableVector, OwnedCloneableVector}; -use slice; +use slice::{Vector, OwnedVector, MutableVector, ImmutableVector, OwnedCloneableVector}; +use vec::Vec; // Reexports pub use self::stdio::stdin; @@ -486,9 +493,9 @@ pub trait Reader { /// or EOF. If `Ok(())` is returned, then all of the requested bytes were /// pushed on to the vector, otherwise the amount `len` bytes couldn't be /// read (an error was encountered), and the error is returned. - fn push_exact(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> { + fn push_exact(&mut self, buf: &mut Vec<u8>, len: uint) -> IoResult<()> { struct State<'a> { - buf: &'a mut ~[u8], + buf: &'a mut Vec<u8>, total_read: uint } @@ -526,8 +533,8 @@ pub trait Reader { /// have already been consumed from the underlying reader, and they are lost /// (not returned as part of the error). If this is unacceptable, then it is /// recommended to use the `push_exact` or `read` methods. - fn read_exact(&mut self, len: uint) -> IoResult<~[u8]> { - let mut buf = slice::with_capacity(len); + fn read_exact(&mut self, len: uint) -> IoResult<Vec<u8>> { + let mut buf = Vec::with_capacity(len); match self.push_exact(&mut buf, len) { Ok(()) => Ok(buf), Err(e) => Err(e), @@ -542,8 +549,8 @@ pub trait Reader { /// discarded when an error is returned. /// /// When EOF is encountered, all bytes read up to that point are returned. - fn read_to_end(&mut self) -> IoResult<~[u8]> { - let mut buf = slice::with_capacity(DEFAULT_BUF_SIZE); + fn read_to_end(&mut self) -> IoResult<Vec<u8>> { + let mut buf = Vec::with_capacity(DEFAULT_BUF_SIZE); loop { match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) { Ok(()) => {} @@ -564,8 +571,8 @@ pub trait Reader { /// UTF-8 bytes. fn read_to_str(&mut self) -> IoResult<~str> { self.read_to_end().and_then(|s| { - match str::from_utf8_owned(s) { - Some(s) => Ok(s), + match str::from_utf8(s.as_slice()) { + Some(s) => Ok(s.to_owned()), None => Err(standard_error(InvalidInput)), } }) @@ -632,28 +639,28 @@ pub trait Reader { /// Reads a little-endian unsigned integer. /// - /// The number of bytes returned is system-dependant. + /// The number of bytes returned is system-dependent. fn read_le_uint(&mut self) -> IoResult<uint> { self.read_le_uint_n(uint::BYTES).map(|i| i as uint) } /// Reads a little-endian integer. /// - /// The number of bytes returned is system-dependant. + /// The number of bytes returned is system-dependent. fn read_le_int(&mut self) -> IoResult<int> { self.read_le_int_n(int::BYTES).map(|i| i as int) } /// Reads a big-endian unsigned integer. /// - /// The number of bytes returned is system-dependant. + /// The number of bytes returned is system-dependent. fn read_be_uint(&mut self) -> IoResult<uint> { self.read_be_uint_n(uint::BYTES).map(|i| i as uint) } /// Reads a big-endian integer. /// - /// The number of bytes returned is system-dependant. + /// The number of bytes returned is system-dependent. fn read_be_int(&mut self) -> IoResult<int> { self.read_be_int_n(int::BYTES).map(|i| i as int) } @@ -1198,8 +1205,8 @@ pub trait Buffer: Reader { /// valid UTF-8 sequence of bytes. fn read_line(&mut self) -> IoResult<~str> { self.read_until('\n' as u8).and_then(|line| - match str::from_utf8_owned(line) { - Some(s) => Ok(s), + match str::from_utf8(line.as_slice()) { + Some(s) => Ok(s.to_owned()), None => Err(standard_error(InvalidInput)), } ) @@ -1230,8 +1237,8 @@ pub trait Buffer: Reader { /// have been read, otherwise the pending byte buffer is returned. This /// is the reason that the byte buffer returned may not always contain the /// delimiter. - fn read_until(&mut self, byte: u8) -> IoResult<~[u8]> { - let mut res = ~[]; + fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> { + let mut res = Vec::new(); let mut used; loop { |
