提交 a246e8fa 编写于 作者: B Brian Anderson

core::rt: Convert unsafe_borrow_io to a Local impl

上级 20426962
......@@ -10,13 +10,13 @@
use option::{Option, Some, None};
use result::{Ok, Err};
use rt::sched::unsafe_borrow_io;
use rt::io::net::ip::IpAddr;
use rt::io::{Reader, Writer, Listener};
use rt::io::{io_error, read_error, EndOfFile};
use rt::rtio::{IoFactory,
use rt::rtio::{IoFactory, IoFactoryObject,
RtioTcpListener, RtioTcpListenerObject,
RtioTcpStream, RtioTcpStreamObject};
use rt::local::Local;
pub struct TcpStream {
rtstream: ~RtioTcpStreamObject
......@@ -32,7 +32,7 @@ fn new(s: ~RtioTcpStreamObject) -> TcpStream {
pub fn connect(addr: IpAddr) -> Option<TcpStream> {
let stream = unsafe {
rtdebug!("borrowing io to connect");
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
rtdebug!("about to connect");
(*io).tcp_connect(addr)
};
......@@ -88,7 +88,10 @@ pub struct TcpListener {
impl TcpListener {
pub fn bind(addr: IpAddr) -> Option<TcpListener> {
let listener = unsafe { (*unsafe_borrow_io()).tcp_bind(addr) };
let listener = unsafe {
let io = Local::unsafe_borrow::<IoFactoryObject>();
(*io).tcp_bind(addr)
};
match listener {
Ok(l) => {
Some(TcpListener {
......
......@@ -12,6 +12,7 @@
use rt::sched::Scheduler;
use rt::task::Task;
use rt::local_ptr;
use rt::rtio::{EventLoop, IoFactoryObject};
pub trait Local {
fn put(value: ~Self);
......@@ -68,6 +69,20 @@ unsafe fn try_unsafe_borrow() -> Option<*mut Task> {
}
}
// XXX: This formulation won't work once ~IoFactoryObject is a real trait pointer
impl Local for IoFactoryObject {
fn put(value: ~IoFactoryObject) { abort!("unimpl") }
fn take() -> ~IoFactoryObject { abort!("unimpl") }
fn exists() -> bool { abort!("unimpl") }
fn borrow(f: &fn(&mut IoFactoryObject)) { abort!("unimpl") }
unsafe fn unsafe_borrow() -> *mut IoFactoryObject {
let sched = Local::unsafe_borrow::<Scheduler>();
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
return io;
}
unsafe fn try_unsafe_borrow() -> Option<*mut IoFactoryObject> { abort!("unimpl") }
}
#[cfg(test)]
mod test {
use rt::sched::Scheduler;
......
......@@ -401,12 +401,6 @@ fn recycle(~self, stack_pool: &mut StackPool) {
}
}
pub unsafe fn unsafe_borrow_io() -> *mut IoFactoryObject {
let sched = Local::unsafe_borrow::<Scheduler>();
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
return io;
}
#[cfg(test)]
mod test {
use int;
......
......@@ -19,7 +19,6 @@
use rt::uv::*;
use rt::uv::idle::IdleWatcher;
use rt::rtio::*;
use rt::sched::unsafe_borrow_io;
use rt::sched::Scheduler;
use rt::io::{standard_error, OtherIoError};
use rt::tube::Tube;
......@@ -359,7 +358,7 @@ fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
fn test_simple_io_no_connect() {
do run_in_newsched_task {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let addr = next_test_ip4();
let maybe_chan = (*io).tcp_connect(addr);
assert!(maybe_chan.is_err());
......@@ -375,7 +374,7 @@ fn test_simple_tcp_server_and_client() {
// Start the server first so it's listening when we connect
do spawntask_immediately {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let mut listener = (*io).tcp_bind(addr).unwrap();
let mut stream = listener.accept().unwrap();
let mut buf = [0, .. 2048];
......@@ -390,7 +389,7 @@ fn test_simple_tcp_server_and_client() {
do spawntask_immediately {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let mut stream = (*io).tcp_connect(addr).unwrap();
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
}
......@@ -404,7 +403,7 @@ fn test_read_and_block() {
let addr = next_test_ip4();
do spawntask_immediately {
let io = unsafe { unsafe_borrow_io() };
let io = unsafe { Local::unsafe_borrow::<IoFactoryObject>() };
let mut listener = unsafe { (*io).tcp_bind(addr).unwrap() };
let mut stream = listener.accept().unwrap();
let mut buf = [0, .. 2048];
......@@ -440,7 +439,7 @@ fn test_read_and_block() {
do spawntask_immediately {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let mut stream = (*io).tcp_connect(addr).unwrap();
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
......@@ -460,7 +459,7 @@ fn test_read_read_read() {
do spawntask_immediately {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let mut listener = (*io).tcp_bind(addr).unwrap();
let mut stream = listener.accept().unwrap();
let buf = [1, .. 2048];
......@@ -474,7 +473,7 @@ fn test_read_read_read() {
do spawntask_immediately {
unsafe {
let io = unsafe_borrow_io();
let io = Local::unsafe_borrow::<IoFactoryObject>();
let mut stream = (*io).tcp_connect(addr).unwrap();
let mut buf = [0, .. 2048];
let mut total_bytes_read = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册