2013-10-22 17:13:18 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
|
|
Bindings to libuv, along with the default implementation of `std::rt::rtio`.
|
|
|
|
|
|
|
|
UV types consist of the event loop (Loop), Watchers, Requests and
|
|
|
|
Callbacks.
|
|
|
|
|
|
|
|
Watchers and Requests encapsulate pointers to uv *handles*, which have
|
|
|
|
subtyping relationships with each other. This subtyping is reflected
|
|
|
|
in the bindings with explicit or implicit coercions. For example, an
|
|
|
|
upcast from TcpWatcher to StreamWatcher is done with
|
|
|
|
`tcp_watcher.as_stream()`. In other cases a callback on a specific
|
|
|
|
type of watcher will be passed a watcher of a supertype.
|
|
|
|
|
|
|
|
Currently all use of Request types (connect/write requests) are
|
|
|
|
encapsulated in the bindings and don't need to be dealt with by the
|
|
|
|
caller.
|
|
|
|
|
|
|
|
# Safety note
|
|
|
|
|
|
|
|
Due to the complex lifecycle of uv handles, as well as compiler bugs,
|
|
|
|
this module is not memory safe and requires explicit memory management,
|
|
|
|
via `close` and `delete` methods.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-01-20 21:42:50 -06:00
|
|
|
#[crate_id = "rustuv#0.10-pre"];
|
2013-10-22 17:13:18 -05:00
|
|
|
#[license = "MIT/ASL2"];
|
Add generation of static libraries to rustc
This commit implements the support necessary for generating both intermediate
and result static rust libraries. This is an implementation of my thoughts in
https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html.
When compiling a library, we still retain the "lib" option, although now there
are "rlib", "staticlib", and "dylib" as options for crate_type (and these are
stackable). The idea of "lib" is to generate the "compiler default" instead of
having too choose (although all are interchangeable). For now I have left the
"complier default" to be a dynamic library for size reasons.
Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an
rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a
dynamic object. I chose this for size reasons, but also because you're probably
not going to be embedding the rustc compiler anywhere any time soon.
Other than the options outlined above, there are a few defaults/preferences that
are now opinionated in the compiler:
* If both a .dylib and .rlib are found for a rust library, the compiler will
prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option
* If generating a "lib", the compiler will generate a dynamic library. This is
overridable by explicitly saying what flavor you'd like (rlib, staticlib,
dylib).
* If no options are passed to the command line, and no crate_type is found in
the destination crate, then an executable is generated
With this change, you can successfully build a rust program with 0 dynamic
dependencies on rust libraries. There is still a dynamic dependency on
librustrt, but I plan on removing that in a subsequent commit.
This change includes no tests just yet. Our current testing
infrastructure/harnesses aren't very amenable to doing flavorful things with
linking, so I'm planning on adding a new mode of testing which I believe belongs
as a separate commit.
Closes #552
2013-11-15 16:03:29 -06:00
|
|
|
#[crate_type = "rlib"];
|
|
|
|
#[crate_type = "dylib"];
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-12-12 19:47:48 -06:00
|
|
|
#[feature(macro_rules)];
|
2014-01-30 16:28:36 -06:00
|
|
|
#[deny(unused_result, unused_must_use)];
|
2014-02-27 01:48:21 -06:00
|
|
|
#[allow(visible_private_types)];
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2014-02-14 12:10:06 -06:00
|
|
|
#[cfg(test)] extern crate green;
|
2013-12-13 13:30:59 -06:00
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
use std::cast;
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::fmt;
|
2013-12-12 19:47:48 -06:00
|
|
|
use std::io::IoError;
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::io;
|
2014-02-10 21:59:35 -06:00
|
|
|
use std::libc::{c_int, c_void};
|
2013-11-07 17:13:06 -06:00
|
|
|
use std::ptr::null;
|
2013-10-22 17:13:18 -05:00
|
|
|
use std::ptr;
|
2013-11-07 17:13:06 -06:00
|
|
|
use std::rt::local::Local;
|
2013-12-12 19:47:48 -06:00
|
|
|
use std::rt::task::{BlockedTask, Task};
|
2013-11-07 17:13:06 -06:00
|
|
|
use std::str::raw::from_c_str;
|
2013-10-22 17:13:18 -05:00
|
|
|
use std::str;
|
2013-11-07 17:13:06 -06:00
|
|
|
use std::task;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 13:29:45 -06:00
|
|
|
pub use self::async::AsyncWatcher;
|
2013-11-04 23:08:25 -06:00
|
|
|
pub use self::file::{FsRequest, FileWatcher};
|
2013-10-22 17:13:18 -05:00
|
|
|
pub use self::idle::IdleWatcher;
|
2013-11-05 13:29:45 -06:00
|
|
|
pub use self::net::{TcpWatcher, TcpListener, TcpAcceptor, UdpWatcher};
|
2013-11-05 02:27:41 -06:00
|
|
|
pub use self::pipe::{PipeWatcher, PipeListener, PipeAcceptor};
|
2013-11-05 13:29:45 -06:00
|
|
|
pub use self::process::Process;
|
2013-10-22 17:13:18 -05:00
|
|
|
pub use self::signal::SignalWatcher;
|
2013-11-05 13:29:45 -06:00
|
|
|
pub use self::timer::TimerWatcher;
|
2013-11-04 18:42:05 -06:00
|
|
|
pub use self::tty::TtyWatcher;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
mod macros;
|
|
|
|
|
2014-01-22 21:32:16 -06:00
|
|
|
mod access;
|
2013-12-12 19:47:48 -06:00
|
|
|
mod homing;
|
2014-01-22 21:32:16 -06:00
|
|
|
mod queue;
|
|
|
|
mod rc;
|
2013-12-12 19:47:48 -06:00
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
/// The implementation of `rtio` for libuv
|
|
|
|
pub mod uvio;
|
|
|
|
|
|
|
|
/// C bindings to libuv
|
|
|
|
pub mod uvll;
|
|
|
|
|
|
|
|
pub mod file;
|
|
|
|
pub mod net;
|
|
|
|
pub mod idle;
|
|
|
|
pub mod timer;
|
|
|
|
pub mod async;
|
|
|
|
pub mod addrinfo;
|
|
|
|
pub mod process;
|
|
|
|
pub mod pipe;
|
|
|
|
pub mod tty;
|
|
|
|
pub mod signal;
|
2013-11-04 18:42:05 -06:00
|
|
|
pub mod stream;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
/// A type that wraps a uv handle
|
|
|
|
pub trait UvHandle<T> {
|
|
|
|
fn uv_handle(&self) -> *T;
|
|
|
|
|
2014-02-10 21:59:35 -06:00
|
|
|
fn uv_loop(&self) -> Loop {
|
|
|
|
Loop::wrap(unsafe { uvll::get_loop_for_uv_handle(self.uv_handle()) })
|
|
|
|
}
|
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
// FIXME(#8888) dummy self
|
|
|
|
fn alloc(_: Option<Self>, ty: uvll::uv_handle_type) -> *T {
|
|
|
|
unsafe {
|
|
|
|
let handle = uvll::malloc_handle(ty);
|
|
|
|
assert!(!handle.is_null());
|
|
|
|
handle as *T
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn from_uv_handle<'a>(h: &'a *T) -> &'a mut Self {
|
|
|
|
cast::transmute(uvll::get_data_for_uv_handle(*h))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install(~self) -> ~Self {
|
|
|
|
unsafe {
|
2013-11-01 12:26:43 -05:00
|
|
|
let myptr = cast::transmute::<&~Self, &*u8>(&self);
|
|
|
|
uvll::set_data_for_uv_handle(self.uv_handle(), *myptr);
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close_async_(&mut self) {
|
|
|
|
// we used malloc to allocate all handles, so we must always have at
|
|
|
|
// least a callback to free all the handles we allocated.
|
|
|
|
extern fn close_cb(handle: *uvll::uv_handle_t) {
|
|
|
|
unsafe { uvll::free_handle(handle) }
|
|
|
|
}
|
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
unsafe {
|
|
|
|
uvll::set_data_for_uv_handle(self.uv_handle(), null::<()>());
|
2013-11-04 16:03:32 -06:00
|
|
|
uvll::uv_close(self.uv_handle() as *uvll::uv_handle_t, close_cb)
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 17:13:06 -06:00
|
|
|
|
|
|
|
fn close(&mut self) {
|
|
|
|
let mut slot = None;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
uvll::uv_close(self.uv_handle() as *uvll::uv_handle_t, close_cb);
|
|
|
|
uvll::set_data_for_uv_handle(self.uv_handle(), ptr::null::<()>());
|
|
|
|
|
2014-02-10 21:59:35 -06:00
|
|
|
wait_until_woken_after(&mut slot, &self.uv_loop(), || {
|
2013-11-07 17:13:06 -06:00
|
|
|
uvll::set_data_for_uv_handle(self.uv_handle(), &slot);
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-11-07 17:13:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
extern fn close_cb(handle: *uvll::uv_handle_t) {
|
|
|
|
unsafe {
|
|
|
|
let data = uvll::get_data_for_uv_handle(handle);
|
|
|
|
uvll::free_handle(handle);
|
|
|
|
if data == ptr::null() { return }
|
|
|
|
let slot: &mut Option<BlockedTask> = cast::transmute(data);
|
2013-12-12 19:47:48 -06:00
|
|
|
wakeup(slot);
|
2013-11-07 17:13:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 22:13:25 -06:00
|
|
|
pub struct ForbidSwitch {
|
2013-12-12 19:47:48 -06:00
|
|
|
priv msg: &'static str,
|
|
|
|
priv io: uint,
|
2013-11-07 22:13:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ForbidSwitch {
|
|
|
|
fn new(s: &'static str) -> ForbidSwitch {
|
|
|
|
ForbidSwitch {
|
2013-12-03 21:18:58 -06:00
|
|
|
msg: s,
|
2013-12-18 12:14:44 -06:00
|
|
|
io: homing::local_id(),
|
2013-11-07 22:13:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ForbidSwitch {
|
|
|
|
fn drop(&mut self) {
|
2013-12-18 12:14:44 -06:00
|
|
|
assert!(self.io == homing::local_id(),
|
2013-12-03 21:18:58 -06:00
|
|
|
"didnt want a scheduler switch: {}",
|
|
|
|
self.msg);
|
2013-11-07 22:13:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:13:06 -06:00
|
|
|
pub struct ForbidUnwind {
|
|
|
|
msg: &'static str,
|
|
|
|
failing_before: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ForbidUnwind {
|
|
|
|
fn new(s: &'static str) -> ForbidUnwind {
|
|
|
|
ForbidUnwind {
|
|
|
|
msg: s, failing_before: task::failing(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ForbidUnwind {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
assert!(self.failing_before == task::failing(),
|
2013-11-07 22:13:25 -06:00
|
|
|
"didnt want an unwind during: {}", self.msg);
|
2013-11-07 17:13:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 21:59:35 -06:00
|
|
|
fn wait_until_woken_after(slot: *mut Option<BlockedTask>,
|
|
|
|
loop_: &Loop,
|
|
|
|
f: ||) {
|
2013-11-07 17:13:06 -06:00
|
|
|
let _f = ForbidUnwind::new("wait_until_woken_after");
|
|
|
|
unsafe {
|
|
|
|
assert!((*slot).is_none());
|
2013-12-12 19:47:48 -06:00
|
|
|
let task: ~Task = Local::take();
|
2014-02-10 21:59:35 -06:00
|
|
|
loop_.modify_blockers(1);
|
2013-12-12 19:47:48 -06:00
|
|
|
task.deschedule(1, |task| {
|
2013-11-07 17:13:06 -06:00
|
|
|
*slot = Some(task);
|
2013-12-12 19:47:48 -06:00
|
|
|
f();
|
|
|
|
Ok(())
|
|
|
|
});
|
2014-02-10 21:59:35 -06:00
|
|
|
loop_.modify_blockers(-1);
|
2013-11-07 17:13:06 -06:00
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
|
|
|
|
2013-12-12 19:47:48 -06:00
|
|
|
fn wakeup(slot: &mut Option<BlockedTask>) {
|
|
|
|
assert!(slot.is_some());
|
2014-01-16 21:58:42 -06:00
|
|
|
let _ = slot.take_unwrap().wake().map(|t| t.reawaken());
|
2013-12-12 19:47:48 -06:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
pub struct Request {
|
|
|
|
handle: *uvll::uv_req_t,
|
2013-11-07 17:13:06 -06:00
|
|
|
priv defused: bool,
|
2013-11-05 02:27:41 -06:00
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
impl Request {
|
|
|
|
pub fn new(ty: uvll::uv_req_type) -> Request {
|
2013-11-07 17:13:06 -06:00
|
|
|
unsafe {
|
|
|
|
let handle = uvll::malloc_req(ty);
|
|
|
|
uvll::set_data_for_req(handle, null::<()>());
|
|
|
|
Request::wrap(handle)
|
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
pub fn wrap(handle: *uvll::uv_req_t) -> Request {
|
2013-11-07 17:13:06 -06:00
|
|
|
Request { handle: handle, defused: false }
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
pub fn set_data<T>(&self, t: *T) {
|
|
|
|
unsafe { uvll::set_data_for_req(self.handle, t) }
|
|
|
|
}
|
|
|
|
|
2013-11-07 17:13:06 -06:00
|
|
|
pub unsafe fn get_data<T>(&self) -> &'static mut T {
|
|
|
|
let data = uvll::get_data_for_req(self.handle);
|
|
|
|
assert!(data != null());
|
|
|
|
cast::transmute(data)
|
2013-11-05 02:27:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function should be used when the request handle has been given to an
|
|
|
|
// underlying uv function, and the uv function has succeeded. This means
|
|
|
|
// that uv will at some point invoke the callback, and in the meantime we
|
|
|
|
// can't deallocate the handle because libuv could be using it.
|
|
|
|
//
|
|
|
|
// This is still a problem in blocking situations due to linked failure. In
|
|
|
|
// the connection callback the handle should be re-wrapped with the `wrap`
|
|
|
|
// function to ensure its destruction.
|
2013-11-07 17:13:06 -06:00
|
|
|
pub fn defuse(&mut self) {
|
|
|
|
self.defused = true;
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
2013-11-05 02:27:41 -06:00
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
impl Drop for Request {
|
|
|
|
fn drop(&mut self) {
|
2013-11-07 17:13:06 -06:00
|
|
|
if !self.defused {
|
|
|
|
unsafe { uvll::free_req(self.handle) }
|
2013-11-05 02:27:41 -06:00
|
|
|
}
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 02:43:42 -06:00
|
|
|
/// FIXME: Loop(*handle) is buggy with destructors. Normal structs
|
2013-11-05 13:29:45 -06:00
|
|
|
/// with dtors may not be destructured, but tuple structs can,
|
|
|
|
/// but the results are not correct.
|
|
|
|
pub struct Loop {
|
|
|
|
priv handle: *uvll::uv_loop_t
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
impl Loop {
|
|
|
|
pub fn new() -> Loop {
|
|
|
|
let handle = unsafe { uvll::loop_new() };
|
|
|
|
assert!(handle.is_not_null());
|
2014-02-10 21:59:35 -06:00
|
|
|
unsafe { uvll::set_data_for_uv_loop(handle, 0 as *c_void) }
|
2013-11-05 13:29:45 -06:00
|
|
|
Loop::wrap(handle)
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 13:29:45 -06:00
|
|
|
pub fn wrap(handle: *uvll::uv_loop_t) -> Loop { Loop { handle: handle } }
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn run(&mut self) {
|
2014-01-30 16:28:36 -06:00
|
|
|
assert_eq!(unsafe { uvll::uv_run(self.handle, uvll::RUN_DEFAULT) }, 0);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&mut self) {
|
2013-11-05 13:29:45 -06:00
|
|
|
unsafe { uvll::uv_loop_delete(self.handle) };
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
2014-02-10 21:59:35 -06:00
|
|
|
|
|
|
|
// The 'data' field of the uv_loop_t is used to count the number of tasks
|
|
|
|
// that are currently blocked waiting for I/O to complete.
|
|
|
|
fn modify_blockers(&self, amt: uint) {
|
|
|
|
unsafe {
|
|
|
|
let cur = uvll::get_data_for_uv_loop(self.handle) as uint;
|
|
|
|
uvll::set_data_for_uv_loop(self.handle, (cur + amt) as *c_void)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_blockers(&self) -> uint {
|
|
|
|
unsafe { uvll::get_data_for_uv_loop(self.handle) as uint }
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: Need to define the error constants like EOF so they can be
|
2013-10-22 17:13:18 -05:00
|
|
|
// compared to the UvError type
|
|
|
|
|
|
|
|
pub struct UvError(c_int);
|
|
|
|
|
|
|
|
impl UvError {
|
|
|
|
pub fn name(&self) -> ~str {
|
|
|
|
unsafe {
|
|
|
|
let inner = match self { &UvError(a) => a };
|
2013-11-03 12:39:39 -06:00
|
|
|
let name_str = uvll::uv_err_name(inner);
|
2013-10-22 17:13:18 -05:00
|
|
|
assert!(name_str.is_not_null());
|
|
|
|
from_c_str(name_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn desc(&self) -> ~str {
|
|
|
|
unsafe {
|
|
|
|
let inner = match self { &UvError(a) => a };
|
2013-11-03 12:39:39 -06:00
|
|
|
let desc_str = uvll::uv_strerror(inner);
|
2013-10-22 17:13:18 -05:00
|
|
|
assert!(desc_str.is_not_null());
|
|
|
|
from_c_str(desc_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_eof(&self) -> bool {
|
2013-11-01 20:06:31 -05:00
|
|
|
let UvError(handle) = *self;
|
|
|
|
handle == uvll::EOF
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
impl fmt::Show for UvError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f.buf, "{}: {}", self.name(), self.desc())
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_smoke_test() {
|
|
|
|
let err: UvError = UvError(uvll::EOF);
|
|
|
|
assert_eq!(err.to_str(), ~"EOF: end of file");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uv_error_to_io_error(uverr: UvError) -> IoError {
|
|
|
|
unsafe {
|
|
|
|
// Importing error constants
|
|
|
|
|
|
|
|
// uv error descriptions are static
|
2013-11-01 20:06:31 -05:00
|
|
|
let UvError(errcode) = uverr;
|
|
|
|
let c_desc = uvll::uv_strerror(errcode);
|
2013-10-22 17:13:18 -05:00
|
|
|
let desc = str::raw::c_str_to_static_slice(c_desc);
|
|
|
|
|
2013-11-01 20:06:31 -05:00
|
|
|
let kind = match errcode {
|
2013-12-12 19:47:48 -06:00
|
|
|
uvll::UNKNOWN => io::OtherIoError,
|
|
|
|
uvll::OK => io::OtherIoError,
|
|
|
|
uvll::EOF => io::EndOfFile,
|
|
|
|
uvll::EACCES => io::PermissionDenied,
|
|
|
|
uvll::ECONNREFUSED => io::ConnectionRefused,
|
|
|
|
uvll::ECONNRESET => io::ConnectionReset,
|
|
|
|
uvll::ENOTCONN => io::NotConnected,
|
|
|
|
uvll::ENOENT => io::FileNotFound,
|
|
|
|
uvll::EPIPE => io::BrokenPipe,
|
|
|
|
uvll::ECONNABORTED => io::ConnectionAborted,
|
2013-12-27 19:50:16 -06:00
|
|
|
uvll::EADDRNOTAVAIL => io::ConnectionRefused,
|
2013-10-22 17:13:18 -05:00
|
|
|
err => {
|
|
|
|
uvdebug!("uverr.code {}", err as int);
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: Need to map remaining uv error types
|
2013-12-12 19:47:48 -06:00
|
|
|
io::OtherIoError
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IoError {
|
|
|
|
kind: kind,
|
|
|
|
desc: desc,
|
|
|
|
detail: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-03 13:02:19 -06:00
|
|
|
/// Given a uv error code, convert a callback status to a UvError
|
|
|
|
pub fn status_to_maybe_uv_error(status: c_int) -> Option<UvError> {
|
2013-10-22 17:13:18 -05:00
|
|
|
if status >= 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(UvError(status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-03 13:02:19 -06:00
|
|
|
pub fn status_to_io_result(status: c_int) -> Result<(), IoError> {
|
|
|
|
if status >= 0 {Ok(())} else {Err(uv_error_to_io_error(UvError(status)))}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
/// The uv buffer type
|
|
|
|
pub type Buf = uvll::uv_buf_t;
|
|
|
|
|
|
|
|
pub fn empty_buf() -> Buf {
|
|
|
|
uvll::uv_buf_t {
|
|
|
|
base: null(),
|
|
|
|
len: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrow a slice to a Buf
|
|
|
|
pub fn slice_to_uv_buf(v: &[u8]) -> Buf {
|
2013-12-15 06:35:12 -06:00
|
|
|
let data = v.as_ptr();
|
2013-11-01 11:36:21 -05:00
|
|
|
uvll::uv_buf_t { base: data, len: v.len() as uvll::uv_buf_len_t }
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-12-13 13:30:59 -06:00
|
|
|
// This function is full of lies!
|
2013-11-07 17:13:06 -06:00
|
|
|
#[cfg(test)]
|
2013-12-13 13:30:59 -06:00
|
|
|
fn local_loop() -> &'static mut uvio::UvIoFactory {
|
2013-11-07 17:13:06 -06:00
|
|
|
unsafe {
|
2013-12-03 21:18:58 -06:00
|
|
|
cast::transmute({
|
2013-12-13 13:30:59 -06:00
|
|
|
let mut task = Local::borrow(None::<Task>);
|
|
|
|
let mut io = task.get().local_io().unwrap();
|
2013-12-05 19:37:02 -06:00
|
|
|
let (_vtable, uvio): (uint, &'static mut uvio::UvIoFactory) =
|
2013-12-13 13:30:59 -06:00
|
|
|
cast::transmute(io.get());
|
2013-12-05 19:37:02 -06:00
|
|
|
uvio
|
2013-12-13 13:30:59 -06:00
|
|
|
})
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::cast::transmute;
|
|
|
|
use std::unstable::run_in_bare_thread;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
use super::{slice_to_uv_buf, Loop};
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
#[test]
|
|
|
|
fn test_slice_to_uv_buf() {
|
|
|
|
let slice = [0, .. 20];
|
|
|
|
let buf = slice_to_uv_buf(slice);
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
assert_eq!(buf.len, 20);
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
unsafe {
|
|
|
|
let base = transmute::<*u8, *mut u8>(buf.base);
|
|
|
|
(*base) = 1;
|
2014-02-10 15:50:42 -06:00
|
|
|
(*base.offset(1)) = 2;
|
2013-11-06 13:03:11 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
assert!(slice[0] == 1);
|
|
|
|
assert!(slice[1] == 2);
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn loop_smoke_test() {
|
2014-01-26 21:57:42 -06:00
|
|
|
run_in_bare_thread(proc() {
|
2013-11-06 13:03:11 -06:00
|
|
|
let mut loop_ = Loop::new();
|
|
|
|
loop_.run();
|
|
|
|
loop_.close();
|
2014-01-26 21:57:42 -06:00
|
|
|
});
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|