1
Fork 0

std: remove unused unsafe blocks/functions

This commit is contained in:
Alex Crichton 2013-04-09 01:31:42 -04:00
parent d9595d1737
commit 52445129fd
11 changed files with 405 additions and 455 deletions

View file

@ -57,11 +57,9 @@ struct DtorRes {
#[unsafe_destructor]
impl Drop for DtorRes {
fn finalize(&self) {
unsafe {
match self.dtor {
option::None => (),
option::Some(f) => f()
}
match self.dtor {
option::None => (),
option::Some(f) => f()
}
}
}
@ -84,7 +82,7 @@ fn DtorRes(dtor: Option<@fn()>) -> DtorRes {
* * base - A foreign pointer to a buffer
* * len - The number of elements in the buffer
*/
pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
pub fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
return CVec{
base: base,
len: len,
@ -103,7 +101,7 @@ pub unsafe fn CVec<T>(base: *mut T, len: uint) -> CVec<T> {
* * dtor - A function to run when the value is destructed, useful
* for freeing the buffer, etc.
*/
pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
pub fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
-> CVec<T> {
return CVec{
base: base,
@ -144,7 +142,7 @@ pub fn set<T:Copy>(t: CVec<T>, ofs: uint, v: T) {
pub fn len<T>(t: CVec<T>) -> uint { t.len }
/// Returns a pointer to the first element of the vector
pub unsafe fn ptr<T>(t: CVec<T>) -> *mut T { t.base }
pub fn ptr<T>(t: CVec<T>) -> *mut T { t.base }
#[cfg(test)]
mod tests {

View file

@ -223,128 +223,126 @@ pub type Result = result::Result<Matches, Fail_>;
* Use <fail_str> to get an error message.
*/
pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
unsafe {
let n_opts = opts.len();
fn f(_x: uint) -> ~[Optval] { return ~[]; }
let mut vals = vec::from_fn(n_opts, f);
let mut free: ~[~str] = ~[];
let l = args.len();
let mut i = 0;
while i < l {
let cur = args[i];
let curlen = cur.len();
if !is_arg(cur) {
free.push(cur);
} else if cur == ~"--" {
let mut j = i + 1;
while j < l { free.push(args[j]); j += 1; }
break;
} else {
let mut names;
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2, curlen).to_owned();
let mut tail_eq = ~[];
for str::each_splitn_char(tail, '=', 1) |s| { tail_eq.push(s.to_owned()) }
if tail_eq.len() <= 1 {
names = ~[Long(tail)];
} else {
names =
~[Long(tail_eq[0])];
i_arg = Some(tail_eq[1]);
}
let n_opts = opts.len();
fn f(_x: uint) -> ~[Optval] { return ~[]; }
let mut vals = vec::from_fn(n_opts, f);
let mut free: ~[~str] = ~[];
let l = args.len();
let mut i = 0;
while i < l {
let cur = args[i];
let curlen = cur.len();
if !is_arg(cur) {
free.push(cur);
} else if cur == ~"--" {
let mut j = i + 1;
while j < l { free.push(args[j]); j += 1; }
break;
} else {
let mut names;
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2, curlen).to_owned();
let mut tail_eq = ~[];
for str::each_splitn_char(tail, '=', 1) |s| { tail_eq.push(s.to_owned()) }
if tail_eq.len() <= 1 {
names = ~[Long(tail)];
} else {
let mut j = 1;
let mut last_valid_opt_id = None;
names = ~[];
while j < curlen {
let range = str::char_range_at(cur, j);
let opt = Short(range.ch);
/* In a series of potential options (eg. -aheJ), if we
see one which takes an argument, we assume all
subsequent characters make up the argument. This
allows options such as -L/usr/local/lib/foo to be
interpreted correctly
*/
match find_opt(opts, opt) {
Some(id) => last_valid_opt_id = Some(id),
None => {
let arg_follows =
last_valid_opt_id.is_some() &&
match opts[last_valid_opt_id.get()]
.hasarg {
Yes | Maybe => true,
No => false
};
if arg_follows && j < curlen {
i_arg = Some(cur.slice(j, curlen).to_owned());
break;
} else {
last_valid_opt_id = None;
}
}
}
names.push(opt);
j = range.next;
}
names =
~[Long(tail_eq[0])];
i_arg = Some(tail_eq[1]);
}
let mut name_pos = 0;
for names.each() |nm| {
name_pos += 1;
let optid = match find_opt(opts, *nm) {
Some(id) => id,
None => return Err(UnrecognizedOption(name_str(nm)))
};
match opts[optid].hasarg {
No => {
if !i_arg.is_none() {
return Err(UnexpectedArgument(name_str(nm)));
} else {
let mut j = 1;
let mut last_valid_opt_id = None;
names = ~[];
while j < curlen {
let range = str::char_range_at(cur, j);
let opt = Short(range.ch);
/* In a series of potential options (eg. -aheJ), if we
see one which takes an argument, we assume all
subsequent characters make up the argument. This
allows options such as -L/usr/local/lib/foo to be
interpreted correctly
*/
match find_opt(opts, opt) {
Some(id) => last_valid_opt_id = Some(id),
None => {
let arg_follows =
last_valid_opt_id.is_some() &&
match opts[last_valid_opt_id.get()]
.hasarg {
Yes | Maybe => true,
No => false
};
if arg_follows && j < curlen {
i_arg = Some(cur.slice(j, curlen).to_owned());
break;
} else {
last_valid_opt_id = None;
}
}
}
names.push(opt);
j = range.next;
}
}
let mut name_pos = 0;
for names.each() |nm| {
name_pos += 1;
let optid = match find_opt(opts, *nm) {
Some(id) => id,
None => return Err(UnrecognizedOption(name_str(nm)))
};
match opts[optid].hasarg {
No => {
if !i_arg.is_none() {
return Err(UnexpectedArgument(name_str(nm)));
}
vals[optid].push(Given);
}
Maybe => {
if !i_arg.is_none() {
vals[optid].push(Val(i_arg.get()));
} else if name_pos < names.len() ||
i + 1 == l || is_arg(args[i + 1]) {
vals[optid].push(Given);
}
Maybe => {
if !i_arg.is_none() {
vals[optid].push(Val(i_arg.get()));
} else if name_pos < names.len() ||
i + 1 == l || is_arg(args[i + 1]) {
vals[optid].push(Given);
} else { i += 1; vals[optid].push(Val(args[i])); }
}
Yes => {
if !i_arg.is_none() {
vals[optid].push(Val(i_arg.get()));
} else if i + 1 == l {
return Err(ArgumentMissing(name_str(nm)));
} else { i += 1; vals[optid].push(Val(args[i])); }
}
}
} else { i += 1; vals[optid].push(Val(args[i])); }
}
Yes => {
if !i_arg.is_none() {
vals[optid].push(Val(i_arg.get()));
} else if i + 1 == l {
return Err(ArgumentMissing(name_str(nm)));
} else { i += 1; vals[optid].push(Val(args[i])); }
}
}
}
i += 1;
}
i = 0u;
while i < n_opts {
let n = vals[i].len();
let occ = opts[i].occur;
if occ == Req {
if n == 0 {
return Err(OptionMissing(name_str(&(opts[i].name))));
}
}
if occ != Multi {
if n > 1 {
return Err(OptionDuplicated(name_str(&(opts[i].name))));
}
}
i += 1;
}
return Ok(Matches {opts: vec::from_slice(opts),
vals: vals,
free: free});
i += 1;
}
i = 0u;
while i < n_opts {
let n = vals[i].len();
let occ = opts[i].occur;
if occ == Req {
if n == 0 {
return Err(OptionMissing(name_str(&(opts[i].name))));
}
}
if occ != Multi {
if n > 1 {
return Err(OptionDuplicated(name_str(&(opts[i].name))));
}
}
i += 1;
}
return Ok(Matches {opts: vec::from_slice(opts),
vals: vals,
free: free});
}
fn opt_vals(mm: &Matches, nm: &str) -> ~[Optval] {

View file

@ -116,35 +116,33 @@ pub fn get_addr(node: &str, iotask: &iotask)
let mut output_ch = Some(SharedChan(output_ch));
do str::as_buf(node) |node_ptr, len| {
let output_ch = output_ch.swap_unwrap();
unsafe {
debug!("slice len %?", len);
let handle = create_uv_getaddrinfo_t();
let handle_ptr = ptr::addr_of(&handle);
let handle_data = GetAddrData {
output_ch: output_ch.clone()
};
let handle_data_ptr = ptr::addr_of(&handle_data);
do interact(iotask) |loop_ptr| {
unsafe {
let result = uv_getaddrinfo(
loop_ptr,
handle_ptr,
get_addr_cb,
node_ptr,
ptr::null(),
ptr::null());
match result {
0i32 => {
set_data_for_req(handle_ptr, handle_data_ptr);
}
_ => {
output_ch.send(result::Err(GetAddrUnknownError));
}
debug!("slice len %?", len);
let handle = create_uv_getaddrinfo_t();
let handle_ptr = ptr::addr_of(&handle);
let handle_data = GetAddrData {
output_ch: output_ch.clone()
};
let handle_data_ptr = ptr::addr_of(&handle_data);
do interact(iotask) |loop_ptr| {
unsafe {
let result = uv_getaddrinfo(
loop_ptr,
handle_ptr,
get_addr_cb,
node_ptr,
ptr::null(),
ptr::null());
match result {
0i32 => {
set_data_for_req(handle_ptr, handle_data_ptr);
}
_ => {
output_ch.send(result::Err(GetAddrUnknownError));
}
}
};
output_po.recv()
}
}
};
output_po.recv()
}
}

View file

@ -57,9 +57,7 @@ pub struct TcpSocket {
#[unsafe_destructor]
impl Drop for TcpSocket {
fn finalize(&self) {
unsafe {
tear_down_socket_data(self.socket_data)
}
tear_down_socket_data(self.socket_data)
}
}
@ -302,11 +300,10 @@ pub fn connect(input_ip: ip::IpAddr, port: uint,
* `TcpErrData` value as the `Err` variant
*/
pub fn write(sock: &TcpSocket, raw_write_data: ~[u8])
-> result::Result<(), TcpErrData> {
unsafe {
let socket_data_ptr = ptr::addr_of(&(*(sock.socket_data)));
write_common_impl(socket_data_ptr, raw_write_data)
}
-> result::Result<(), TcpErrData>
{
let socket_data_ptr = ptr::addr_of(&(*(sock.socket_data)));
write_common_impl(socket_data_ptr, raw_write_data)
}
/**
@ -341,13 +338,12 @@ pub fn write(sock: &TcpSocket, raw_write_data: ~[u8])
* value as the `Err` variant
*/
pub fn write_future(sock: &TcpSocket, raw_write_data: ~[u8])
-> future::Future<result::Result<(), TcpErrData>> {
unsafe {
let socket_data_ptr = ptr::addr_of(&(*(sock.socket_data)));
do future_spawn {
let data_copy = copy(raw_write_data);
write_common_impl(socket_data_ptr, data_copy)
}
-> future::Future<result::Result<(), TcpErrData>>
{
let socket_data_ptr = ptr::addr_of(&(*(sock.socket_data)));
do future_spawn {
let data_copy = copy(raw_write_data);
write_common_impl(socket_data_ptr, data_copy)
}
}
@ -369,10 +365,8 @@ pub fn write_future(sock: &TcpSocket, raw_write_data: ~[u8])
pub fn read_start(sock: &TcpSocket)
-> result::Result<@Port<
result::Result<~[u8], TcpErrData>>, TcpErrData> {
unsafe {
let socket_data = ptr::addr_of(&(*(sock.socket_data)));
read_start_common_impl(socket_data)
}
let socket_data = ptr::addr_of(&(*(sock.socket_data)));
read_start_common_impl(socket_data)
}
/**
@ -382,12 +376,9 @@ pub fn read_start(sock: &TcpSocket)
*
* * `sock` - a `net::tcp::TcpSocket` that you wish to stop reading on
*/
pub fn read_stop(sock: &TcpSocket) ->
result::Result<(), TcpErrData> {
unsafe {
let socket_data = ptr::addr_of(&(*sock.socket_data));
read_stop_common_impl(socket_data)
}
pub fn read_stop(sock: &TcpSocket) -> result::Result<(), TcpErrData> {
let socket_data = ptr::addr_of(&(*sock.socket_data));
read_stop_common_impl(socket_data)
}
/**
@ -654,150 +645,148 @@ fn listen_common(host_ip: ip::IpAddr,
on_establish_cb: ~fn(SharedChan<Option<TcpErrData>>),
on_connect_cb: ~fn(*uv::ll::uv_tcp_t))
-> result::Result<(), TcpListenErrData> {
unsafe {
let (stream_closed_po, stream_closed_ch) = stream::<()>();
let stream_closed_ch = SharedChan(stream_closed_ch);
let (kill_po, kill_ch) = stream::<Option<TcpErrData>>();
let kill_ch = SharedChan(kill_ch);
let server_stream = uv::ll::tcp_t();
let server_stream_ptr = ptr::addr_of(&server_stream);
let server_data: TcpListenFcData = TcpListenFcData {
server_stream_ptr: server_stream_ptr,
stream_closed_ch: stream_closed_ch,
kill_ch: kill_ch.clone(),
on_connect_cb: on_connect_cb,
iotask: iotask.clone(),
ipv6: match &host_ip {
&ip::Ipv4(_) => { false }
&ip::Ipv6(_) => { true }
},
mut active: true
};
let server_data_ptr = ptr::addr_of(&server_data);
let (stream_closed_po, stream_closed_ch) = stream::<()>();
let stream_closed_ch = SharedChan(stream_closed_ch);
let (kill_po, kill_ch) = stream::<Option<TcpErrData>>();
let kill_ch = SharedChan(kill_ch);
let server_stream = uv::ll::tcp_t();
let server_stream_ptr = ptr::addr_of(&server_stream);
let server_data: TcpListenFcData = TcpListenFcData {
server_stream_ptr: server_stream_ptr,
stream_closed_ch: stream_closed_ch,
kill_ch: kill_ch.clone(),
on_connect_cb: on_connect_cb,
iotask: iotask.clone(),
ipv6: match &host_ip {
&ip::Ipv4(_) => { false }
&ip::Ipv6(_) => { true }
},
mut active: true
};
let server_data_ptr = ptr::addr_of(&server_data);
let (setup_po, setup_ch) = stream();
let (setup_po, setup_ch) = stream();
// this is to address a compiler warning about
// an implicit copy.. it seems that double nested
// will defeat a move sigil, as is done to the host_ip
// arg above.. this same pattern works w/o complaint in
// tcp::connect (because the iotask::interact cb isn't
// nested within a core::comm::listen block)
let loc_ip = copy(host_ip);
do iotask::interact(iotask) |loop_ptr| {
unsafe {
match uv::ll::tcp_init(loop_ptr, server_stream_ptr) {
0i32 => {
uv::ll::set_data_for_uv_handle(
server_stream_ptr,
server_data_ptr);
let addr_str = ip::format_addr(&loc_ip);
let bind_result = match loc_ip {
ip::Ipv4(ref addr) => {
debug!("addr: %?", addr);
let in_addr = uv::ll::ip4_addr(
addr_str,
port as int);
uv::ll::tcp_bind(server_stream_ptr,
ptr::addr_of(&in_addr))
}
ip::Ipv6(ref addr) => {
debug!("addr: %?", addr);
let in_addr = uv::ll::ip6_addr(
addr_str,
port as int);
uv::ll::tcp_bind6(server_stream_ptr,
ptr::addr_of(&in_addr))
}
};
match bind_result {
0i32 => {
match uv::ll::listen(
server_stream_ptr,
backlog as libc::c_int,
tcp_lfc_on_connection_cb) {
0i32 => setup_ch.send(None),
_ => {
debug!(
"failure to uv_tcp_init");
let err_data =
uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
// this is to address a compiler warning about
// an implicit copy.. it seems that double nested
// will defeat a move sigil, as is done to the host_ip
// arg above.. this same pattern works w/o complaint in
// tcp::connect (because the iotask::interact cb isn't
// nested within a core::comm::listen block)
let loc_ip = copy(host_ip);
do iotask::interact(iotask) |loop_ptr| {
unsafe {
match uv::ll::tcp_init(loop_ptr, server_stream_ptr) {
0i32 => {
uv::ll::set_data_for_uv_handle(
server_stream_ptr,
server_data_ptr);
let addr_str = ip::format_addr(&loc_ip);
let bind_result = match loc_ip {
ip::Ipv4(ref addr) => {
debug!("addr: %?", addr);
let in_addr = uv::ll::ip4_addr(
addr_str,
port as int);
uv::ll::tcp_bind(server_stream_ptr,
ptr::addr_of(&in_addr))
}
ip::Ipv6(ref addr) => {
debug!("addr: %?", addr);
let in_addr = uv::ll::ip6_addr(
addr_str,
port as int);
uv::ll::tcp_bind6(server_stream_ptr,
ptr::addr_of(&in_addr))
}
};
match bind_result {
0i32 => {
match uv::ll::listen(
server_stream_ptr,
backlog as libc::c_int,
tcp_lfc_on_connection_cb) {
0i32 => setup_ch.send(None),
_ => {
debug!(
"failure to uv_tcp_init");
let err_data =
uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
}
_ => {
debug!("failure to uv_tcp_bind");
let err_data = uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
}
_ => {
debug!("failure to uv_tcp_bind");
let err_data = uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
}
_ => {
debug!("failure to uv_tcp_bind");
let err_data = uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
}
_ => {
debug!("failure to uv_tcp_bind");
let err_data = uv::ll::get_last_err_data(
loop_ptr);
setup_ch.send(Some(err_data));
}
}
}
}
let setup_result = setup_po.recv();
let setup_result = setup_po.recv();
match setup_result {
Some(ref err_data) => {
do iotask::interact(iotask) |loop_ptr| {
unsafe {
debug!(
"tcp::listen post-kill recv hl interact %?",
loop_ptr);
(*server_data_ptr).active = false;
uv::ll::close(server_stream_ptr, tcp_lfc_close_cb);
}
};
stream_closed_po.recv();
match err_data.err_name {
~"EACCES" => {
debug!("Got EACCES error");
result::Err(AccessDenied)
}
~"EADDRINUSE" => {
debug!("Got EADDRINUSE error");
result::Err(AddressInUse)
}
_ => {
debug!("Got '%s' '%s' libuv error",
err_data.err_name, err_data.err_msg);
result::Err(
GenericListenErr(err_data.err_name,
err_data.err_msg))
}
match setup_result {
Some(ref err_data) => {
do iotask::interact(iotask) |loop_ptr| {
unsafe {
debug!(
"tcp::listen post-kill recv hl interact %?",
loop_ptr);
(*server_data_ptr).active = false;
uv::ll::close(server_stream_ptr, tcp_lfc_close_cb);
}
};
stream_closed_po.recv();
match err_data.err_name {
~"EACCES" => {
debug!("Got EACCES error");
result::Err(AccessDenied)
}
~"EADDRINUSE" => {
debug!("Got EADDRINUSE error");
result::Err(AddressInUse)
}
_ => {
debug!("Got '%s' '%s' libuv error",
err_data.err_name, err_data.err_msg);
result::Err(
GenericListenErr(err_data.err_name,
err_data.err_msg))
}
}
None => {
on_establish_cb(kill_ch.clone());
let kill_result = kill_po.recv();
do iotask::interact(iotask) |loop_ptr| {
unsafe {
debug!(
"tcp::listen post-kill recv hl interact %?",
loop_ptr);
(*server_data_ptr).active = false;
uv::ll::close(server_stream_ptr, tcp_lfc_close_cb);
}
};
stream_closed_po.recv();
match kill_result {
// some failure post bind/listen
Some(ref err_data) => result::Err(GenericListenErr(
err_data.err_name,
err_data.err_msg)),
// clean exit
None => result::Ok(())
}
None => {
on_establish_cb(kill_ch.clone());
let kill_result = kill_po.recv();
do iotask::interact(iotask) |loop_ptr| {
unsafe {
debug!(
"tcp::listen post-kill recv hl interact %?",
loop_ptr);
(*server_data_ptr).active = false;
uv::ll::close(server_stream_ptr, tcp_lfc_close_cb);
}
};
stream_closed_po.recv();
match kill_result {
// some failure post bind/listen
Some(ref err_data) => result::Err(GenericListenErr(
err_data.err_name,
err_data.err_msg)),
// clean exit
None => result::Ok(())
}
}
}
@ -1382,9 +1371,7 @@ extern fn stream_error_close_cb(handle: *uv::ll::uv_tcp_t) {
}
extern fn tcp_connect_close_cb(handle: *uv::ll::uv_tcp_t) {
unsafe {
debug!("closed client tcp handle %?", handle);
}
debug!("closed client tcp handle %?", handle);
}
extern fn tcp_connect_on_connect_cb(connect_req_ptr: *uv::ll::uv_connect_t,

View file

@ -118,8 +118,7 @@ fn encode_inner(s: &str, full_url: bool) -> ~str {
* This function is compliant with RFC 3986.
*/
pub fn encode(s: &str) -> ~str {
// FIXME(#3722): unsafe only because encode_inner does (string) IO
unsafe {encode_inner(s, true)}
encode_inner(s, true)
}
/**
@ -130,8 +129,7 @@ pub fn encode(s: &str) -> ~str {
*/
pub fn encode_component(s: &str) -> ~str {
// FIXME(#3722): unsafe only because encode_inner does (string) IO
unsafe {encode_inner(s, false)}
encode_inner(s, false)
}
fn decode_inner(s: &str, full_url: bool) -> ~str {
@ -178,16 +176,14 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
* This will only decode escape sequences generated by encode.
*/
pub fn decode(s: &str) -> ~str {
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {decode_inner(s, true)}
decode_inner(s, true)
}
/**
* Decode a string encoded with percent encoding.
*/
pub fn decode_component(s: &str) -> ~str {
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {decode_inner(s, false)}
decode_inner(s, false)
}
fn encode_plus(s: &str) -> ~str {
@ -301,18 +297,15 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) {
let len = str::len(s);
let mut index = len;
let mut mat = 0;
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {
do io::with_str_reader(s) |rdr| {
let mut ch;
while !rdr.eof() {
ch = rdr.read_byte() as char;
if ch == c {
// found a match, adjust markers
index = rdr.tell()-1;
mat = 1;
break;
}
do io::with_str_reader(s) |rdr| {
let mut ch;
while !rdr.eof() {
ch = rdr.read_byte() as char;
if ch == c {
// found a match, adjust markers
index = rdr.tell()-1;
mat = 1;
break;
}
}
}
@ -346,29 +339,25 @@ fn query_from_str(rawquery: &str) -> Query {
if str::len(rawquery) != 0 {
for str::each_split_char(rawquery, '&') |p| {
let (k, v) = split_char_first(p, '=');
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {query.push((decode_component(k), decode_component(v)));}
query.push((decode_component(k), decode_component(v)));
};
}
return query;
}
pub fn query_to_str(query: &Query) -> ~str {
unsafe {
// FIXME(#3722): unsafe only because decode_inner does (string) IO
let mut strvec = ~[];
for query.each |kv| {
match kv {
&(ref k, ref v) => {
strvec.push(fmt!("%s=%s",
encode_component(*k),
encode_component(*v))
);
}
let mut strvec = ~[];
for query.each |kv| {
match kv {
&(ref k, ref v) => {
strvec.push(fmt!("%s=%s",
encode_component(*k),
encode_component(*v))
);
}
}
return str::connect(strvec, ~"&");
}
return str::connect(strvec, ~"&");
}
// returns the scheme and the rest of the url, or a parsing error

View file

@ -59,12 +59,10 @@ pub unsafe fn load_history(file: ~str) -> bool {
/// Print out a prompt and then wait for input and return it
pub unsafe fn read(prompt: ~str) -> Option<~str> {
do str::as_c_str(prompt) |buf| {
unsafe {
let line = rustrt::linenoise(buf);
let line = rustrt::linenoise(buf);
if line.is_null() { None }
else { Some(str::raw::from_c_str(line)) }
}
if line.is_null() { None }
else { Some(str::raw::from_c_str(line)) }
}
}
@ -74,22 +72,20 @@ fn complete_key(_v: @CompletionCb) {}
/// Bind to the main completion callback
pub unsafe fn complete(cb: CompletionCb) {
unsafe {
task::local_data::local_data_set(complete_key, @(cb));
task::local_data::local_data_set(complete_key, @(cb));
extern fn callback(line: *c_char, completions: *()) {
unsafe {
let cb = *task::local_data::local_data_get(complete_key)
.get();
extern fn callback(line: *c_char, completions: *()) {
unsafe {
let cb = *task::local_data::local_data_get(complete_key)
.get();
do cb(str::raw::from_c_str(line)) |suggestion| {
do str::as_c_str(suggestion) |buf| {
rustrt::linenoiseAddCompletion(completions, buf);
}
do cb(str::raw::from_c_str(line)) |suggestion| {
do str::as_c_str(suggestion) |buf| {
rustrt::linenoiseAddCompletion(completions, buf);
}
}
}
rustrt::linenoiseSetCompletionCallback(callback);
}
rustrt::linenoiseSetCompletionCallback(callback);
}

View file

@ -862,17 +862,15 @@ pub mod node {
* This function executes in linear time.
*/
pub fn flatten(node: @Node) -> @Node {
unsafe {
match (*node) {
Leaf(_) => node,
Concat(ref x) => {
@Leaf(Leaf {
byte_offset: 0u,
byte_len: x.byte_len,
char_len: x.char_len,
content: @serialize_node(node),
})
}
match (*node) {
Leaf(_) => node,
Concat(ref x) => {
@Leaf(Leaf {
byte_offset: 0u,
byte_len: x.byte_len,
char_len: x.char_len,
content: @serialize_node(node),
})
}
}
}

View file

@ -101,17 +101,15 @@ fn new_sem_and_signal(count: int, num_condvars: uint)
pub impl<Q:Owned> Sem<Q> {
fn acquire(&self) {
let mut waiter_nobe = None;
unsafe {
do (**self).with |state| {
state.count -= 1;
if state.count < 0 {
// Create waiter nobe.
let (WaitEnd, SignalEnd) = comm::oneshot();
// Tell outer scope we need to block.
waiter_nobe = Some(WaitEnd);
// Enqueue ourself.
state.waiters.tail.send(SignalEnd);
}
do (**self).with |state| {
state.count -= 1;
if state.count < 0 {
// Create waiter nobe.
let (WaitEnd, SignalEnd) = comm::oneshot();
// Tell outer scope we need to block.
waiter_nobe = Some(WaitEnd);
// Enqueue ourself.
state.waiters.tail.send(SignalEnd);
}
}
// Uncomment if you wish to test for sem races. Not valgrind-friendly.
@ -122,12 +120,10 @@ pub impl<Q:Owned> Sem<Q> {
}
}
fn release(&self) {
unsafe {
do (**self).with |state| {
state.count += 1;
if state.count <= 0 {
signal_waitqueue(&state.waiters);
}
do (**self).with |state| {
state.count += 1;
if state.count <= 0 {
signal_waitqueue(&state.waiters);
}
}
}
@ -169,9 +165,7 @@ struct SemReleaseGeneric<'self, Q> { sem: &'self Sem<Q> }
#[unsafe_destructor]
impl<'self, Q:Owned> Drop for SemReleaseGeneric<'self, Q> {
fn finalize(&self) {
unsafe {
self.sem.release();
}
self.sem.release();
}
}
@ -291,13 +285,11 @@ pub impl<'self> Condvar<'self> {
fn signal_on(&self, condvar_id: uint) -> bool {
let mut out_of_bounds = None;
let mut result = false;
unsafe {
do (**self.sem).with |state| {
if condvar_id < vec::len(state.blocked) {
result = signal_waitqueue(&state.blocked[condvar_id]);
} else {
out_of_bounds = Some(vec::len(state.blocked));
}
do (**self.sem).with |state| {
if condvar_id < vec::len(state.blocked) {
result = signal_waitqueue(&state.blocked[condvar_id]);
} else {
out_of_bounds = Some(vec::len(state.blocked));
}
}
do check_cvar_bounds(out_of_bounds, condvar_id, "cond.signal_on()") {
@ -312,17 +304,15 @@ pub impl<'self> Condvar<'self> {
fn broadcast_on(&self, condvar_id: uint) -> uint {
let mut out_of_bounds = None;
let mut queue = None;
unsafe {
do (**self.sem).with |state| {
if condvar_id < vec::len(state.blocked) {
// To avoid :broadcast_heavy, we make a new waitqueue,
// swap it out with the old one, and broadcast on the
// old one outside of the little-lock.
queue = Some(util::replace(&mut state.blocked[condvar_id],
new_waitqueue()));
} else {
out_of_bounds = Some(vec::len(state.blocked));
}
do (**self.sem).with |state| {
if condvar_id < vec::len(state.blocked) {
// To avoid :broadcast_heavy, we make a new waitqueue,
// swap it out with the old one, and broadcast on the
// old one outside of the little-lock.
queue = Some(util::replace(&mut state.blocked[condvar_id],
new_waitqueue()));
} else {
out_of_bounds = Some(vec::len(state.blocked));
}
}
do check_cvar_bounds(out_of_bounds, condvar_id, "cond.signal_on()") {

View file

@ -42,47 +42,45 @@ pub fn delayed_send<T:Owned>(iotask: &IoTask,
msecs: uint,
ch: &Chan<T>,
val: T) {
let (timer_done_po, timer_done_ch) = stream::<()>();
let timer_done_ch = SharedChan(timer_done_ch);
let timer = uv::ll::timer_t();
let timer_ptr = ptr::addr_of(&timer);
do iotask::interact(iotask) |loop_ptr| {
unsafe {
let (timer_done_po, timer_done_ch) = stream::<()>();
let timer_done_ch = SharedChan(timer_done_ch);
let timer = uv::ll::timer_t();
let timer_ptr = ptr::addr_of(&timer);
do iotask::interact(iotask) |loop_ptr| {
unsafe {
let init_result = uv::ll::timer_init(loop_ptr, timer_ptr);
if (init_result == 0i32) {
let start_result = uv::ll::timer_start(
timer_ptr, delayed_send_cb, msecs, 0u);
if (start_result == 0i32) {
// Note: putting the channel into a ~
// to cast to *c_void
let timer_done_ch_clone = ~timer_done_ch.clone();
let timer_done_ch_ptr = transmute::<
~SharedChan<()>, *c_void>(
timer_done_ch_clone);
uv::ll::set_data_for_uv_handle(
timer_ptr,
timer_done_ch_ptr);
} else {
let error_msg = uv::ll::get_last_err_info(
loop_ptr);
fail!(~"timer::delayed_send() start failed: " +
error_msg);
}
} else {
let error_msg = uv::ll::get_last_err_info(loop_ptr);
fail!(~"timer::delayed_send() init failed: " +
error_msg);
}
let init_result = uv::ll::timer_init(loop_ptr, timer_ptr);
if (init_result == 0i32) {
let start_result = uv::ll::timer_start(
timer_ptr, delayed_send_cb, msecs, 0u);
if (start_result == 0i32) {
// Note: putting the channel into a ~
// to cast to *c_void
let timer_done_ch_clone = ~timer_done_ch.clone();
let timer_done_ch_ptr = transmute::<
~SharedChan<()>, *c_void>(
timer_done_ch_clone);
uv::ll::set_data_for_uv_handle(
timer_ptr,
timer_done_ch_ptr);
} else {
let error_msg = uv::ll::get_last_err_info(
loop_ptr);
fail!(~"timer::delayed_send() start failed: " +
error_msg);
}
};
// delayed_send_cb has been processed by libuv
timer_done_po.recv();
// notify the caller immediately
ch.send(val);
// uv_close for this timer has been processed
timer_done_po.recv();
} else {
let error_msg = uv::ll::get_last_err_info(loop_ptr);
fail!(~"timer::delayed_send() init failed: " +
error_msg);
}
}
};
// delayed_send_cb has been processed by libuv
timer_done_po.recv();
// notify the caller immediately
ch.send(val);
// uv_close for this timer has been processed
timer_done_po.recv();
}
/**

View file

@ -75,7 +75,7 @@ pub fn spawn_iotask(task: task::TaskBuilder) -> IoTask {
* module. It is not safe to send the `loop_ptr` param to this callback out
* via ports/chans.
*/
pub unsafe fn interact(iotask: &IoTask, cb: ~fn(*c_void)) {
pub fn interact(iotask: &IoTask, cb: ~fn(*c_void)) {
send_msg(iotask, Interaction(cb));
}
@ -87,9 +87,7 @@ pub unsafe fn interact(iotask: &IoTask, cb: ~fn(*c_void)) {
* closed, causing a failure otherwise.
*/
pub fn exit(iotask: &IoTask) {
unsafe {
send_msg(iotask, TeardownLoop);
}
send_msg(iotask, TeardownLoop);
}

View file

@ -1111,22 +1111,22 @@ pub unsafe fn freeaddrinfo(res: *addrinfo) {
}
// libuv struct initializers
pub unsafe fn tcp_t() -> uv_tcp_t {
pub fn tcp_t() -> uv_tcp_t {
return uv_ll_struct_stubgen::gen_stub_uv_tcp_t();
}
pub unsafe fn connect_t() -> uv_connect_t {
pub fn connect_t() -> uv_connect_t {
return uv_ll_struct_stubgen::gen_stub_uv_connect_t();
}
pub unsafe fn write_t() -> uv_write_t {
pub fn write_t() -> uv_write_t {
return uv_ll_struct_stubgen::gen_stub_uv_write_t();
}
pub unsafe fn async_t() -> uv_async_t {
pub fn async_t() -> uv_async_t {
return uv_ll_struct_stubgen::gen_stub_uv_async_t();
}
pub unsafe fn timer_t() -> uv_timer_t {
pub fn timer_t() -> uv_timer_t {
return uv_ll_struct_stubgen::gen_stub_uv_timer_t();
}
pub unsafe fn getaddrinfo_t() -> uv_getaddrinfo_t {
pub fn getaddrinfo_t() -> uv_getaddrinfo_t {
return uv_ll_struct_stubgen::gen_stub_uv_getaddrinfo_t();
}