Skip to content

Commit a246e8f

Browse files
committed
core::rt: Convert unsafe_borrow_io to a Local impl
1 parent 2042696 commit a246e8f

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/libcore/rt/io/net/tcp.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
use option::{Option, Some, None};
1212
use result::{Ok, Err};
13-
use rt::sched::unsafe_borrow_io;
1413
use rt::io::net::ip::IpAddr;
1514
use rt::io::{Reader, Writer, Listener};
1615
use rt::io::{io_error, read_error, EndOfFile};
17-
use rt::rtio::{IoFactory,
16+
use rt::rtio::{IoFactory, IoFactoryObject,
1817
RtioTcpListener, RtioTcpListenerObject,
1918
RtioTcpStream, RtioTcpStreamObject};
19+
use rt::local::Local;
2020

2121
pub struct TcpStream {
2222
rtstream: ~RtioTcpStreamObject
@@ -32,7 +32,7 @@ impl TcpStream {
3232
pub fn connect(addr: IpAddr) -> Option<TcpStream> {
3333
let stream = unsafe {
3434
rtdebug!("borrowing io to connect");
35-
let io = unsafe_borrow_io();
35+
let io = Local::unsafe_borrow::<IoFactoryObject>();
3636
rtdebug!("about to connect");
3737
(*io).tcp_connect(addr)
3838
};
@@ -88,7 +88,10 @@ pub struct TcpListener {
8888

8989
impl TcpListener {
9090
pub fn bind(addr: IpAddr) -> Option<TcpListener> {
91-
let listener = unsafe { (*unsafe_borrow_io()).tcp_bind(addr) };
91+
let listener = unsafe {
92+
let io = Local::unsafe_borrow::<IoFactoryObject>();
93+
(*io).tcp_bind(addr)
94+
};
9295
match listener {
9396
Ok(l) => {
9497
Some(TcpListener {

src/libcore/rt/local.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use option::{Option, Some, None};
1212
use rt::sched::Scheduler;
1313
use rt::task::Task;
1414
use rt::local_ptr;
15+
use rt::rtio::{EventLoop, IoFactoryObject};
1516

1617
pub trait Local {
1718
fn put(value: ~Self);
@@ -68,6 +69,20 @@ impl Local for Task {
6869
}
6970
}
7071

72+
// XXX: This formulation won't work once ~IoFactoryObject is a real trait pointer
73+
impl Local for IoFactoryObject {
74+
fn put(value: ~IoFactoryObject) { abort!("unimpl") }
75+
fn take() -> ~IoFactoryObject { abort!("unimpl") }
76+
fn exists() -> bool { abort!("unimpl") }
77+
fn borrow(f: &fn(&mut IoFactoryObject)) { abort!("unimpl") }
78+
unsafe fn unsafe_borrow() -> *mut IoFactoryObject {
79+
let sched = Local::unsafe_borrow::<Scheduler>();
80+
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
81+
return io;
82+
}
83+
unsafe fn try_unsafe_borrow() -> Option<*mut IoFactoryObject> { abort!("unimpl") }
84+
}
85+
7186
#[cfg(test)]
7287
mod test {
7388
use rt::sched::Scheduler;

src/libcore/rt/sched.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,6 @@ pub impl Coroutine {
401401
}
402402
}
403403

404-
pub unsafe fn unsafe_borrow_io() -> *mut IoFactoryObject {
405-
let sched = Local::unsafe_borrow::<Scheduler>();
406-
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
407-
return io;
408-
}
409-
410404
#[cfg(test)]
411405
mod test {
412406
use int;

src/libcore/rt/uv/uvio.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rt::io::net::ip::IpAddr;
1919
use rt::uv::*;
2020
use rt::uv::idle::IdleWatcher;
2121
use rt::rtio::*;
22-
use rt::sched::unsafe_borrow_io;
2322
use rt::sched::Scheduler;
2423
use rt::io::{standard_error, OtherIoError};
2524
use rt::tube::Tube;
@@ -359,7 +358,7 @@ impl RtioTcpStream for UvTcpStream {
359358
fn test_simple_io_no_connect() {
360359
do run_in_newsched_task {
361360
unsafe {
362-
let io = unsafe_borrow_io();
361+
let io = Local::unsafe_borrow::<IoFactoryObject>();
363362
let addr = next_test_ip4();
364363
let maybe_chan = (*io).tcp_connect(addr);
365364
assert!(maybe_chan.is_err());
@@ -375,7 +374,7 @@ fn test_simple_tcp_server_and_client() {
375374
// Start the server first so it's listening when we connect
376375
do spawntask_immediately {
377376
unsafe {
378-
let io = unsafe_borrow_io();
377+
let io = Local::unsafe_borrow::<IoFactoryObject>();
379378
let mut listener = (*io).tcp_bind(addr).unwrap();
380379
let mut stream = listener.accept().unwrap();
381380
let mut buf = [0, .. 2048];
@@ -390,7 +389,7 @@ fn test_simple_tcp_server_and_client() {
390389

391390
do spawntask_immediately {
392391
unsafe {
393-
let io = unsafe_borrow_io();
392+
let io = Local::unsafe_borrow::<IoFactoryObject>();
394393
let mut stream = (*io).tcp_connect(addr).unwrap();
395394
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
396395
}
@@ -404,7 +403,7 @@ fn test_read_and_block() {
404403
let addr = next_test_ip4();
405404

406405
do spawntask_immediately {
407-
let io = unsafe { unsafe_borrow_io() };
406+
let io = unsafe { Local::unsafe_borrow::<IoFactoryObject>() };
408407
let mut listener = unsafe { (*io).tcp_bind(addr).unwrap() };
409408
let mut stream = listener.accept().unwrap();
410409
let mut buf = [0, .. 2048];
@@ -440,7 +439,7 @@ fn test_read_and_block() {
440439

441440
do spawntask_immediately {
442441
unsafe {
443-
let io = unsafe_borrow_io();
442+
let io = Local::unsafe_borrow::<IoFactoryObject>();
444443
let mut stream = (*io).tcp_connect(addr).unwrap();
445444
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
446445
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
@@ -460,7 +459,7 @@ fn test_read_read_read() {
460459

461460
do spawntask_immediately {
462461
unsafe {
463-
let io = unsafe_borrow_io();
462+
let io = Local::unsafe_borrow::<IoFactoryObject>();
464463
let mut listener = (*io).tcp_bind(addr).unwrap();
465464
let mut stream = listener.accept().unwrap();
466465
let buf = [1, .. 2048];
@@ -474,7 +473,7 @@ fn test_read_read_read() {
474473

475474
do spawntask_immediately {
476475
unsafe {
477-
let io = unsafe_borrow_io();
476+
let io = Local::unsafe_borrow::<IoFactoryObject>();
478477
let mut stream = (*io).tcp_connect(addr).unwrap();
479478
let mut buf = [0, .. 2048];
480479
let mut total_bytes_read = 0;

0 commit comments

Comments
 (0)