std: Expose that LocalIo may not always be available
It is not the case that all programs will always be able to acquire an instance of the LocalIo borrow, so this commit exposes this limitation by returning Option<LocalIo> from LocalIo::borrow(). At the same time, a helper method LocalIo::maybe_raise() has been added in order to encapsulate the functionality of raising on io_error if there is on local I/O available.
This commit is contained in:
@@ -26,17 +26,9 @@ impl TcpStream {
|
||||
}
|
||||
|
||||
pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
|
||||
let result = {
|
||||
let mut io = LocalIo::borrow();
|
||||
io.get().tcp_connect(addr)
|
||||
};
|
||||
match result {
|
||||
Ok(s) => Some(TcpStream::new(s)),
|
||||
Err(ioerr) => {
|
||||
io_error::cond.raise(ioerr);
|
||||
None
|
||||
}
|
||||
}
|
||||
LocalIo::maybe_raise(|io| {
|
||||
io.tcp_connect(addr).map(TcpStream::new)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn peer_name(&mut self) -> Option<SocketAddr> {
|
||||
@@ -94,14 +86,9 @@ pub struct TcpListener {
|
||||
|
||||
impl TcpListener {
|
||||
pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
|
||||
let mut io = LocalIo::borrow();
|
||||
match io.get().tcp_bind(addr) {
|
||||
Ok(l) => Some(TcpListener { obj: l }),
|
||||
Err(ioerr) => {
|
||||
io_error::cond.raise(ioerr);
|
||||
None
|
||||
}
|
||||
}
|
||||
LocalIo::maybe_raise(|io| {
|
||||
io.tcp_bind(addr).map(|l| TcpListener { obj: l })
|
||||
})
|
||||
}
|
||||
|
||||
pub fn socket_name(&mut self) -> Option<SocketAddr> {
|
||||
|
||||
Reference in New Issue
Block a user