Skip to content

Commit

Permalink
Merge pull request chris-morgan#12 from larsbergstrom/rustup
Browse files Browse the repository at this point in the history
Rust upgrade
  • Loading branch information
jdm committed Apr 4, 2014
2 parents 4a381f3 + 163c706 commit fa82ccf
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/codegen/branchify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::str::Chars;
use std::io::IoResult;

struct ParseBranch {
pub struct ParseBranch {
matches: ~[u8],
result: Option<~str>,
children: ~[ParseBranch],
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ fn main() {
3 => {
let output_dir = Path::new(args[2].as_slice());

match args[1] {
~"read_method.rs" => read_method::generate(&output_dir).unwrap(),
~"status.rs" => status::generate(&output_dir).unwrap(),
match args[1].as_slice() {
"read_method.rs" => read_method::generate(&output_dir).unwrap(),
"status.rs" => status::generate(&output_dir).unwrap(),
s => {
println!("unknown thing-to-generate '{}'", s);
os::set_exit_status(1);
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use collections::hashmap::HashSet;
use std::ascii::StrAsciiExt;
use std::vec;
use std::slice;
use std::io::IoResult;
use super::get_writer;

Expand Down Expand Up @@ -52,7 +52,7 @@ impl Status {
/// "ImATeaPot"
fn camel_case(msg: &str) -> ~str {
let msg = msg.replace("-", " ").replace("'", "");
let mut result: ~[Ascii] = vec::with_capacity(msg.len());
let mut result: ~[Ascii] = slice::with_capacity(msg.len());
let mut capitalise = true;
for c in msg.chars() {
let c = match capitalise {
Expand Down
7 changes: 4 additions & 3 deletions src/examples/server/apache_fake/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

#[crate_id = "apache_fake"];

extern crate extra;
extern crate time;
extern crate http;

use std::vec::Vec;

use std::io::net::ip::{SocketAddr, Ipv4Addr};
use std::io::Writer;

Expand Down Expand Up @@ -44,13 +45,13 @@ impl Server for ApacheFakeServer {
weak: false,
opaque_tag: ~"501b29-b1-4a285ed47404a" });
w.headers.accept_ranges = Some(headers::accept_ranges::RangeUnits(
~[headers::accept_ranges::Bytes]));
vec!(headers::accept_ranges::Bytes)));
w.headers.content_length = Some(177);
w.headers.vary = Some(~"Accept-Encoding");
w.headers.content_type = Some(headers::content_type::MediaType {
type_: ~"text",
subtype: ~"html",
parameters: ~[]
parameters: Vec::new()
});
w.headers.extensions.insert(~"X-Pad", ~"avoid browser bug");

Expand Down
3 changes: 1 addition & 2 deletions src/examples/server/hello_world/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#[crate_id = "hello_world"];

extern crate extra;
extern crate time;
extern crate http;

Expand All @@ -26,7 +25,7 @@ impl Server for HelloWorldServer {
w.headers.content_type = Some(MediaType {
type_: ~"text",
subtype: ~"plain",
parameters: ~[(~"charset", ~"UTF-8")]
parameters: vec!((~"charset", ~"UTF-8"))
});
w.headers.server = Some(~"Example");

Expand Down
3 changes: 1 addition & 2 deletions src/examples/server/info/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#[crate_id = "info"];

extern crate extra;
extern crate time;
extern crate http;

Expand All @@ -27,7 +26,7 @@ impl Server for InfoServer {
w.headers.content_type = Some(MediaType {
type_: ~"text",
subtype: ~"html",
parameters: ~[(~"charset", ~"UTF-8")]
parameters: vec!((~"charset", ~"UTF-8"))
});
w.headers.server = Some(~"Rust Thingummy/0.0-pre");
w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>")).unwrap();
Expand Down
7 changes: 4 additions & 3 deletions src/examples/server/request_uri/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#[crate_id = "request_uri"];

extern crate extra;
extern crate time;
extern crate http;

use std::vec::Vec;

use std::io::net::ip::{SocketAddr, Ipv4Addr};
use std::io::Writer;

Expand Down Expand Up @@ -47,7 +48,7 @@ impl Server for RequestUriServer {
(&Options, &Star) => {
// Querying server capabilities. That's nice and simple. I can handle these methods:
// (TODO: let user code override this, providing a default method.)
w.headers.allow = Some(~[Get, Head, Post, Put, Delete, Trace, Options, Connect, Patch]);
w.headers.allow = Some(vec!(Get, Head, Post, Put, Delete, Trace, Options, Connect, Patch));
w.headers.content_length = Some(0);
return;
},
Expand All @@ -62,7 +63,7 @@ impl Server for RequestUriServer {
w.headers.content_type = Some(MediaType {
type_: ~"text",
subtype: ~"html",
parameters: ~[]
parameters: Vec::new()
});

w.write(bytes!("<!DOCTYPE html><title>Rust HTTP server</title>")).unwrap();
Expand Down
28 changes: 16 additions & 12 deletions src/http/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use std::io::{IoResult, Stream};
use std::cmp::min;
use std::vec;
use std::vec::Vec;
use std::slice;
use std::num::ToStrRadix;

// 64KB chunks (moderately arbitrary)
Expand All @@ -12,22 +13,22 @@ static WRITE_BUF_SIZE: uint = 0x10000;

pub struct BufferedStream<T> {
wrapped: T,
read_buffer: ~[u8],
read_buffer: Vec<u8>,
// The current position in the buffer
read_pos: uint,
// The last valid position in the reader
read_max: uint,
write_buffer: ~[u8],
write_buffer: Vec<u8>,
write_len: uint,

writing_chunked_body: bool,
}

impl<T: Stream> BufferedStream<T> {
pub fn new(stream: T) -> BufferedStream<T> {
let mut read_buffer = vec::with_capacity(READ_BUF_SIZE);
let mut read_buffer = Vec::with_capacity(READ_BUF_SIZE);
unsafe { read_buffer.set_len(READ_BUF_SIZE); }
let mut write_buffer = vec::with_capacity(WRITE_BUF_SIZE);
let mut write_buffer = Vec::with_capacity(WRITE_BUF_SIZE);
unsafe { write_buffer.set_len(WRITE_BUF_SIZE); }
BufferedStream {
wrapped: stream,
Expand All @@ -52,14 +53,14 @@ impl<T: Reader> BufferedStream<T> {
(0, _) => fail!("poke called when buffer is full"),
(_, _) => self.read_pos -= 1,
}
self.read_buffer[self.read_pos] = byte;
self.read_buffer.as_mut_slice()[self.read_pos] = byte;
}

#[inline]
fn fill_buffer(&mut self) -> IoResult<()> {
assert_eq!(self.read_pos, self.read_max);
self.read_pos = 0;
match self.wrapped.read(self.read_buffer) {
match self.wrapped.read(self.read_buffer.as_mut_slice()) {
Ok(i) => {
self.read_max = i;
Ok(())
Expand All @@ -80,7 +81,7 @@ impl<T: Reader> BufferedStream<T> {
try!(self.fill_buffer());
}
self.read_pos += 1;
Ok(self.read_buffer[self.read_pos - 1])
Ok(self.read_buffer.as_slice()[self.read_pos - 1])
}
}

Expand Down Expand Up @@ -110,7 +111,7 @@ impl<T: Reader> Reader for BufferedStream<T> {
try!(self.fill_buffer());
}
let size = min(self.read_max - self.read_pos, buf.len());
vec::bytes::copy_memory(buf, self.read_buffer.slice_from(self.read_pos).slice_to(size));
slice::bytes::copy_memory(buf, self.read_buffer.slice_from(self.read_pos).slice_to(size));
self.read_pos += size;
Ok(size)
}
Expand All @@ -135,17 +136,20 @@ impl<T: Writer> Writer for BufferedStream<T> {
try!(self.wrapped.write(bytes!("\r\n")));
}
} else {
unsafe { self.write_buffer.mut_slice_from(self.write_len).copy_memory(buf); }
unsafe {
let len = self.write_buffer.len();
self.write_buffer.mut_slice(self.write_len, len).copy_memory(buf);
}

self.write_len += buf.len();
if self.write_len == self.write_buffer.len() {
if self.writing_chunked_body {
let s = format!("{}\r\n", self.write_len.to_str_radix(16));
try!(self.wrapped.write(s.as_bytes()));
try!(self.wrapped.write(self.write_buffer));
try!(self.wrapped.write(self.write_buffer.as_slice()));
try!(self.wrapped.write(bytes!("\r\n")));
} else {
try!(self.wrapped.write(self.write_buffer));
try!(self.wrapped.write(self.write_buffer.as_slice()));
}
self.write_len = 0;
}
Expand Down
5 changes: 2 additions & 3 deletions src/http/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ let response = match request.read_response() {
```
*/

use extra::url::Url;
use extra::url;
use url;
use url::Url;
use method::Method;
use std::io::{IoError, IoResult};
use std::io::net::get_host_addresses;
Expand Down
5 changes: 3 additions & 2 deletions src/http/headers/accept_ranges.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The Accept-Ranges request header, defined in RFC 2616, Section 14.5.

use std::vec::Vec;
use std::io::IoResult;
use std::ascii::StrAsciiExt;

Expand All @@ -13,7 +14,7 @@ pub enum RangeUnit {
#[deriving(Clone,Eq)]
// RFC 2616: acceptable-ranges = 1#range-unit | "none"
pub enum AcceptableRanges {
RangeUnits(~[RangeUnit]),
RangeUnits(Vec<RangeUnit>),
NoAcceptableRanges,
}

Expand All @@ -23,7 +24,7 @@ static BYTES: &'static [u8] = bytes!("bytes");
impl super::HeaderConvertible for AcceptableRanges {
fn from_stream<R: Reader>(reader: &mut super::HeaderValueByteIterator<R>)
-> Option<AcceptableRanges> {
let mut range_units = ~[];
let mut range_units = Vec::new();
loop {
match reader.read_token() {
Some(token) => {
Expand Down
31 changes: 16 additions & 15 deletions src/http/headers/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use headers::serialization_utils::normalise_header_name;

/// A value for the Connection header. Note that should it be a ``Token``, the string is in
/// normalised header case (e.g. "Keep-Alive").
#[deriving(Clone, DeepClone, Eq)]
#[deriving(Clone, Eq)]
pub enum Connection {
Token(~str),
Close,
Expand Down Expand Up @@ -58,24 +58,25 @@ impl super::HeaderConvertible for Connection {

#[test]
fn test_connection() {
use std::vec::Vec;
use headers::test_utils::{assert_conversion_correct,
assert_interpretation_correct,
assert_invalid};
assert_conversion_correct("close", ~[Close]);
assert_conversion_correct("Foo", ~[Token(~"Foo")]);
assert_conversion_correct("Foo, Keep-Alive", ~[Token(~"Foo"), Token(~"Keep-Alive")]);
assert_conversion_correct("Foo, close", ~[Token(~"Foo"), Close]);
assert_conversion_correct("close, Bar", ~[Close, Token(~"Bar")]);
assert_conversion_correct("close", vec!(Close));
assert_conversion_correct("Foo", vec!(Token(~"Foo")));
assert_conversion_correct("Foo, Keep-Alive", vec!(Token(~"Foo"), Token(~"Keep-Alive")));
assert_conversion_correct("Foo, close", vec!(Token(~"Foo"), Close));
assert_conversion_correct("close, Bar", vec!(Close, Token(~"Bar")));

assert_interpretation_correct("close", ~[Close]);
assert_interpretation_correct("foo", ~[Token(~"Foo")]);
assert_interpretation_correct("close \r\n , keep-ALIVE", ~[Close, Token(~"Keep-Alive")]);
assert_interpretation_correct("foo,close", ~[Token(~"Foo"), Close]);
assert_interpretation_correct("close, bar", ~[Close, Token(~"Bar")]);
assert_interpretation_correct("close", vec!(Close));
assert_interpretation_correct("foo", vec!(Token(~"Foo")));
assert_interpretation_correct("close \r\n , keep-ALIVE", vec!(Close, Token(~"Keep-Alive")));
assert_interpretation_correct("foo,close", vec!(Token(~"Foo"), Close));
assert_interpretation_correct("close, bar", vec!(Close, Token(~"Bar")));
assert_interpretation_correct("CLOSE", Close);

assert_invalid::<~[Connection]>("foo bar");
assert_invalid::<~[Connection]>("foo bar");
assert_invalid::<~[Connection]>("foo, bar baz");
assert_invalid::<~[Connection]>("foo, , baz");
assert_invalid::<Vec<Connection>>("foo bar");
assert_invalid::<Vec<Connection>>("foo bar");
assert_invalid::<Vec<Connection>>("foo, bar baz");
assert_invalid::<Vec<Connection>>("foo, , baz");
}
17 changes: 9 additions & 8 deletions src/http/headers/content_type.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! The Content-Type entity header, defined in RFC 2616, Section 14.17.
use headers::serialization_utils::{push_parameters, WriterUtil};
use std::vec::Vec;
use std::io::IoResult;
use std::fmt;

#[deriving(Clone, Eq)]
pub struct MediaType {
type_: ~str,
subtype: ~str,
parameters: ~[(~str, ~str)],
parameters: Vec<(~str, ~str)>,
}

pub fn MediaType(type_: ~str, subtype: ~str, parameters: ~[(~str, ~str)]) -> MediaType {
pub fn MediaType(type_: ~str, subtype: ~str, parameters: Vec<(~str, ~str)>) -> MediaType {
MediaType {
type_: type_,
subtype: subtype,
Expand All @@ -28,7 +29,7 @@ impl fmt::Show for MediaType {
//s.push_parameters(self.parameters);
//s
let s = format!("{}/{}", self.type_, self.subtype);
f.buf.write(push_parameters(s, self.parameters).as_bytes())
f.buf.write(push_parameters(s, self.parameters.as_slice()).as_bytes())
}
}

Expand Down Expand Up @@ -63,7 +64,7 @@ impl super::HeaderConvertible for MediaType {
try!(writer.write_token(self.type_));
try!(writer.write(['/' as u8]));
try!(writer.write_token(self.subtype));
writer.write_parameters(self.parameters)
writer.write_parameters(self.parameters.as_slice())
}

fn http_value(&self) -> ~str {
Expand All @@ -75,13 +76,13 @@ impl super::HeaderConvertible for MediaType {
fn test_content_type() {
use headers::test_utils::{assert_conversion_correct, assert_interpretation_correct,
assert_invalid};
assert_conversion_correct("type/subtype", MediaType(~"type", ~"subtype", ~[]));
assert_conversion_correct("type/subtype", MediaType(~"type", ~"subtype", Vec::new()));
assert_conversion_correct("type/subtype;key=value",
MediaType(~"type", ~"subtype", ~[(~"key", ~"value")]));
MediaType(~"type", ~"subtype", vec!((~"key", ~"value"))));
assert_conversion_correct("type/subtype;key=value;q=0.1",
MediaType(~"type", ~"subtype", ~[(~"key", ~"value"), (~"q", ~"0.1")]));
MediaType(~"type", ~"subtype", vec!((~"key", ~"value"), (~"q", ~"0.1"))));
assert_interpretation_correct("type/subtype ; key = value ; q = 0.1",
MediaType(~"type", ~"subtype", ~[(~"key", ~"value"), (~"q", ~"0.1")]));
MediaType(~"type", ~"subtype", vec!((~"key", ~"value"), (~"q", ~"0.1"))));

assert_invalid::<MediaType>("");
assert_invalid::<MediaType>("/");
Expand Down
Loading

0 comments on commit fa82ccf

Please sign in to comment.