core::rt: Move all the uv callback definitions to one place

This commit is contained in:
Brian Anderson 2013-04-27 19:53:42 -07:00
parent dbf89664aa
commit a134503d74
4 changed files with 23 additions and 42 deletions

View File

@ -11,15 +11,11 @@
use prelude::*;
use ptr::null;
use libc::c_void;
use super::{UvError, Callback, Request, NativeHandle, Loop};
use rt::uv::{Request, NativeHandle, Loop, FsCallback};
use rt::uv::uvll;
use rt::uv::uvll::*;
pub type FsCallback = ~fn(FsRequest, Option<UvError>);
impl Callback for FsCallback { }
pub struct FsRequest(*uvll::uv_fs_t);
impl Request for FsRequest;
impl FsRequest {

View File

@ -9,17 +9,14 @@
// except according to those terms.
use libc::c_int;
use option::{Option, Some, None};
use option::{Some, None};
use rt::uv::uvll;
use rt::uv::{Watcher, Callback, Loop, UvError, NativeHandle};
use rt::uv::{Watcher, Loop, NativeHandle, IdleCallback};
use rt::uv::status_to_maybe_uv_error;
pub struct IdleWatcher(*uvll::uv_idle_t);
impl Watcher for IdleWatcher { }
pub type IdleCallback = ~fn(IdleWatcher, Option<UvError>);
impl Callback for IdleCallback { }
pub impl IdleWatcher {
fn new(loop_: &mut Loop) -> IdleWatcher {
unsafe {

View File

@ -51,10 +51,9 @@ use rt::io::IoError;
#[cfg(test)] use unstable::run_in_bare_thread;
pub use self::file::{FsRequest, FsCallback};
pub use self::file::FsRequest;
pub use self::net::{StreamWatcher, TcpWatcher};
pub use self::net::{ReadCallback, AllocCallback, ConnectionCallback, ConnectCallback};
pub use self::idle::{IdleWatcher, IdleCallback};
pub use self::idle::IdleWatcher;
/// The implementation of `rtio` for libuv
pub mod uvio;
@ -66,11 +65,12 @@ pub mod file;
pub mod net;
pub mod idle;
/// A trait for callbacks to implement. Provides a little extra type safety
/// for generic, unsafe interop functions like `set_watcher_callback`.
pub trait Callback { }
pub trait Request { }
/// XXX: Loop(*handle) is buggy with destructors. Normal structs
/// with dtors may not be destructured, but tuple structs can,
/// but the results are not correct.
pub struct Loop {
handle: *uvll::uv_loop_t
}
/// The trait implemented by uv 'watchers' (handles). Watchers are
/// non-owning wrappers around the uv handles and are not completely
@ -80,8 +80,7 @@ pub trait Request { }
/// entirely memory safe if used in unanticipated patterns.
pub trait Watcher { }
pub type NullCallback = ~fn();
impl Callback for NullCallback { }
pub trait Request { }
/// A type that wraps a native handle
pub trait NativeHandle<T> {
@ -89,13 +88,6 @@ pub trait NativeHandle<T> {
pub fn native_handle(&self) -> T;
}
/// XXX: Loop(*handle) is buggy with destructors. Normal structs
/// with dtors may not be destructured, but tuple structs can,
/// but the results are not correct.
pub struct Loop {
handle: *uvll::uv_loop_t
}
pub impl Loop {
fn new() -> Loop {
let handle = unsafe { uvll::loop_new() };
@ -121,6 +113,15 @@ impl NativeHandle<*uvll::uv_loop_t> for Loop {
}
}
// XXX: The uv alloc callback also has a *uv_handle_t arg
pub type AllocCallback = ~fn(uint) -> Buf;
pub type ReadCallback = ~fn(StreamWatcher, int, Buf, Option<UvError>);
pub type NullCallback = ~fn();
pub type IdleCallback = ~fn(IdleWatcher, Option<UvError>);
pub type ConnectionCallback = ~fn(StreamWatcher, Option<UvError>);
pub type FsCallback = ~fn(FsRequest, Option<UvError>);
/// Callbacks used by StreamWatchers, set as custom data on the foreign handle
struct WatcherData {
read_cb: Option<ReadCallback>,

View File

@ -13,7 +13,8 @@ use libc::{size_t, ssize_t, c_int, c_void};
use util::ignore;
use rt::uv::uvll;
use rt::uv::uvll::*;
use super::{Loop, Watcher, Request, UvError, Buf, Callback, NativeHandle, NullCallback,
use rt::uv::{AllocCallback, ConnectionCallback, ReadCallback};
use super::{Loop, Watcher, Request, UvError, Buf, NativeHandle, NullCallback,
status_to_maybe_uv_error, vec_to_uv_buf, vec_from_uv_buf, slice_to_uv_buf};
use super::super::io::net::ip::{IpAddr, Ipv4, Ipv6};
use rt::uv::last_uv_error;
@ -48,13 +49,6 @@ fn ip4_as_uv_ip4<T>(addr: IpAddr, f: &fn(*sockaddr_in) -> T) -> T {
pub struct StreamWatcher(*uvll::uv_stream_t);
impl Watcher for StreamWatcher { }
pub type ReadCallback = ~fn(StreamWatcher, int, Buf, Option<UvError>);
impl Callback for ReadCallback { }
// XXX: The uv alloc callback also has a *uv_handle_t arg
pub type AllocCallback = ~fn(uint) -> Buf;
impl Callback for AllocCallback { }
pub impl StreamWatcher {
fn read_start(&mut self, alloc: AllocCallback, cb: ReadCallback) {
@ -165,9 +159,6 @@ impl NativeHandle<*uvll::uv_stream_t> for StreamWatcher {
pub struct TcpWatcher(*uvll::uv_tcp_t);
impl Watcher for TcpWatcher { }
pub type ConnectionCallback = ~fn(StreamWatcher, Option<UvError>);
impl Callback for ConnectionCallback { }
pub impl TcpWatcher {
fn new(loop_: &mut Loop) -> TcpWatcher {
unsafe {
@ -268,12 +259,8 @@ impl NativeHandle<*uvll::uv_tcp_t> for TcpWatcher {
}
}
pub type ConnectCallback = ~fn(ConnectRequest, Option<UvError>);
impl Callback for ConnectCallback { }
// uv_connect_t is a subclass of uv_req_t
struct ConnectRequest(*uvll::uv_connect_t);
impl Request for ConnectRequest { }
impl ConnectRequest {