Skip to content

Commit

Permalink
rustup. *phew*
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Jan 10, 2015
1 parent e405883 commit 0418a8f
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 159 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ name = "csv"
[dependencies]
rustc-serialize = "*"

[dev-dependencies]
regex = "*"

[profile.bench]
opt-level = 3
lto = true # this doesn't seem to work... why?
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() {
let mut rdr = csv::Reader::from_file(fp);

for record in rdr.decode() {
let (s1, s2, dist): (String, String, uint) = record.unwrap();
let (s1, s2, dist): (String, String, usize) = record.unwrap();
println!("({}, {}): {}", s1, s2, dist);
}
}
Expand All @@ -45,15 +45,15 @@ Don't like tuples? That's fine. Use a struct instead:

```rust
extern crate csv;
extern crate serialize;
extern crate "rustc-serialize" as rustc_serialize;

use std::path::Path;

#[deriving(Decodable)]
#[derive(RustcDecodable)]
struct Record {
s1: String,
s2: String,
dist: uint,
dist: u32,
}

fn main() {
Expand All @@ -70,11 +70,11 @@ fn main() {
Do some records not have a distance for some reason? Use an `Option` type!

```rust
#[deriving(Decodable)]
#[derive(Decodable)]
struct Record {
s1: String,
s2: String,
dist: Option<uint>,
dist: Option<u32>,
}
```

Expand Down
26 changes: 14 additions & 12 deletions examples/nfl_plays.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate csv;
extern crate "rustc-serialize" as rustc_serialize;

Expand All @@ -7,18 +9,18 @@ use std::path::Path;
#[derive(RustcDecodable)]
struct Play {
gameid: String,
qtr: uint,
min: Option<uint>,
sec: Option<uint>,
qtr: u32,
min: Option<u32>,
sec: Option<u32>,
team_off: String,
team_def: String,
down: Option<uint>,
togo: Option<uint>,
ydline: Option<uint>,
down: Option<u32>,
togo: Option<u32>,
ydline: Option<u32>,
description: String,
offscore: uint,
defscore: uint,
season: uint,
offscore: u32,
defscore: u32,
season: u32,
}

fn main() {
Expand All @@ -31,9 +33,9 @@ fn main() {
println!("Found {} plays.", plays.len());

let tfb = plays.iter().find(|&p| {
"NE" == p.team_off.as_slice() && "DEN" == p.team_def.as_slice()
&& p.description.as_slice().contains("TOUCHDOWN")
&& p.description.as_slice().contains("T.Brady")
"NE" == p.team_off && "DEN" == p.team_def
&& p.description.contains("TOUCHDOWN")
&& p.description.contains("T.Brady")
}).unwrap();
println!("Tom Brady touchdown: {}", tfb.description);
}
Expand Down
4 changes: 3 additions & 1 deletion examples/rational_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This example shows how to write your own custom implementation of
//! `Decodable` to parse rational numbers.

#![allow(unstable)]

extern crate csv;
extern crate regex;
extern crate "rustc-serialize" as rustc_serialize;
Expand Down Expand Up @@ -50,6 +52,6 @@ X,Y,Rational
let mut rdr = csv::Reader::from_string(data).has_headers(true);
for row in rdr.decode() {
let (x, y, r): (f64, f64, Rational) = row.unwrap();
println!("({}, {}): {}", x, y, r);
println!("({}, {}): {:?}", x, y, r);
}
}
4 changes: 3 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate csv;

use std::path::Path;
Expand All @@ -7,7 +9,7 @@ fn main() {
let mut rdr = csv::Reader::from_file(fp);

for record in rdr.decode() {
let (s1, s2, dist): (String, String, uint) = record.unwrap();
let (s1, s2, dist): (String, String, usize) = record.unwrap();
println!("({}, {}): {}", s1, s2, dist);
}
}
6 changes: 4 additions & 2 deletions examples/simple_missing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate csv;
extern crate "rustc-serialize" as rustc_serialize;

Expand All @@ -7,7 +9,7 @@ use std::path::Path;
struct Record {
s1: String,
s2: String,
dist: Option<uint>,
dist: Option<u32>,
}

fn main() {
Expand All @@ -16,6 +18,6 @@ fn main() {

for record in rdr.decode() {
let record: Record = record.unwrap();
println!("({}, {}): {}", record.s1, record.s2, record.dist);
println!("({}, {}): {:?}", record.s1, record.s2, record.dist);
}
}
4 changes: 3 additions & 1 deletion examples/simple_struct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate csv;
extern crate "rustc-serialize" as rustc_serialize;

Expand All @@ -7,7 +9,7 @@ use std::path::Path;
struct Record {
s1: String,
s2: String,
dist: uint,
dist: u32,
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions examples/stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unstable)]

extern crate csv;

use std::sync::mpsc::channel;
Expand All @@ -10,7 +12,7 @@ fn main() {
let _ = Thread::spawn(move || {
let w = io::ChanWriter::new(send);
let mut enc = csv::Writer::from_writer(w);
for x in range(1u, 6) {
for x in range(1, 6) {
if let Err(err) = enc.encode((x, x * x)) {
panic!("Failed encoding: {}", err);
}
Expand All @@ -21,6 +23,6 @@ fn main() {
let mut dec = csv::Reader::from_reader(io::ChanReader::new(recv))
.has_headers(false);
for r in dec.records() {
println!("Record: {}", r);
println!("Record: {:?}", r);
}
}
20 changes: 10 additions & 10 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Reader;
static CSV_DATA: &'static str = "./examples/data/bench.csv";

fn ordie<T, E: Show>(r: Result<T, E>) -> T {
r.or_else(|e: E| -> Result<T, E> panic!(e.to_string())).unwrap()
r.or_else(|e: E| -> Result<T, E> panic!(format!("{:?}", e))).unwrap()
}

fn file_to_mem(fp: &str) -> io::MemReader {
Expand Down Expand Up @@ -63,18 +63,18 @@ fn string_records(b: &mut Bencher) {
#[derive(RustcDecodable)]
struct Play {
gameid: String,
qtr: int,
min: Option<int>,
sec: Option<int>,
qtr: i32,
min: Option<i32>,
sec: Option<i32>,
team_off: String,
team_def: String,
down: Option<int>,
togo: Option<int>,
ydline: Option<int>,
down: Option<i32>,
togo: Option<i32>,
ydline: Option<i32>,
description: String,
offscore: int,
defscore: int,
season: int,
offscore: i32,
defscore: i32,
season: i32,
}

#[bench]
Expand Down
16 changes: 8 additions & 8 deletions src/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ use std::cmp;
use std::io::{Reader, Buffer, IoResult};
use std::slice;

static DEFAULT_BUF_SIZE: uint = 1024 * 64;
static DEFAULT_BUF_SIZE: usize = 1024 * 64;

pub struct BufferedReader<R> {
inner: R,
buf: Vec<u8>,
pos: uint,
cap: uint,
pos: usize,
cap: usize,
}

impl<R: Reader> BufferedReader<R> {
/// Creates a new `BufferedReader` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
pub fn with_capacity(cap: usize, inner: R) -> BufferedReader<R> {
// It's *much* faster to create an uninitialized buffer than it is to
// fill everything in with 0. This buffer is entirely an implementation
// detail and is never exposed, so we're safe to not initialize
Expand Down Expand Up @@ -53,24 +53,24 @@ impl<R: Reader> Buffer for BufferedReader<R> {
self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
self.pos = 0;
}
Ok(self.buf[self.pos..self.cap])
Ok(&self.buf[self.pos..self.cap])
}

fn consume(&mut self, amt: uint) {
fn consume(&mut self, amt: usize) {
self.pos += amt;
assert!(self.pos <= self.cap);
}
}

impl<R: Reader> Reader for BufferedReader<R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
if self.pos == self.cap && buf.len() >= self.buf.capacity() {
return self.inner.read(buf);
}
let nread = {
let available = try!(self.fill_buf());
let nread = cmp::min(available.len(), buf.len());
slice::bytes::copy_memory(buf, available[..nread]);
slice::bytes::copy_memory(buf, &available[..nread]);
nread
};
self.pos += nread;
Expand Down
53 changes: 32 additions & 21 deletions src/bytestr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ pub trait BorrowBytes {
}

impl BorrowBytes for String {
fn borrow_bytes(&self) -> &[u8] { self.as_slice().as_bytes() }
fn borrow_bytes(&self) -> &[u8] { self.as_bytes() }
}

impl BorrowBytes for str {
fn borrow_bytes(&self) -> &[u8] { self.as_bytes() }
}

impl BorrowBytes for Vec<u8> {
fn borrow_bytes(&self) -> &[u8] { self.as_slice() }
fn borrow_bytes(&self) -> &[u8] { &**self }
}

impl BorrowBytes for ByteString {
fn borrow_bytes(&self) -> &[u8] { self.as_slice() }
fn borrow_bytes(&self) -> &[u8] { &**self }
}

impl BorrowBytes for [u8] {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl ByteString {
}

/// Return the number of bytes in the string.
pub fn len(&self) -> uint {
pub fn len(&self) -> usize {
self.as_bytes().len()
}

Expand All @@ -160,7 +160,7 @@ impl fmt::Show for ByteString {
// encodable, which obviously doesn't work with raw byte strings.
//
// For now, we just show the bytes, e.g., `[255, 50, 48, 49, ...]`.
write!(f, "{}", self[])
write!(f, "{:?}", &**self)
}
}

Expand All @@ -173,34 +173,45 @@ impl AsSlice<u8> for ByteString {

impl ops::Deref for ByteString {
type Target = [u8];

fn deref<'a>(&'a self) -> &'a [u8] {
&*self.0
}
}

impl ops::Slice<uint, [u8]> for ByteString {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [u8] {
self.as_slice()
impl ops::Index<ops::FullRange> for ByteString {
type Output = [u8];

fn index<'a>(&'a self, _: &ops::FullRange) -> &'a [u8] {
&**self
}
}

#[inline]
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [u8] {
self.as_slice().slice_from_or_fail(start)
impl ops::Index<ops::RangeFrom<usize>> for ByteString {
type Output = [u8];

fn index<'a>(&'a self, index: &ops::RangeFrom<usize>) -> &'a [u8] {
&(&**self)[index.start..]
}
}

#[inline]
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [u8] {
self.as_slice().slice_to_or_fail(end)
impl ops::Index<ops::RangeTo<usize>> for ByteString {
type Output = [u8];

fn index<'a>(&'a self, index: &ops::RangeTo<usize>) -> &'a [u8] {
&(&**self)[..index.end]
}
}

#[inline]
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [u8] {
self.as_slice().slice_or_fail(start, end)
impl ops::Index<ops::Range<usize>> for ByteString {
type Output = [u8];

fn index<'a>(&'a self, index: &ops::Range<usize>) -> &'a [u8] {
&(&**self)[index.start..index.end]
}
}

impl<H: hash::Writer> hash::Hash<H> for ByteString {
impl<H: hash::Hasher> hash::Hash<H> for ByteString {
fn hash(&self, hasher: &mut H) {
// WHOA. This used to be `(&*self).hash(hasher);`, but it introduced
// a *major* performance regression that got fixed by using
Expand All @@ -211,7 +222,7 @@ impl<H: hash::Writer> hash::Hash<H> for ByteString {
// TODO: Try `(&*self)` again (maybe when 1.0 hits). If the regression
// remains, create a smaller reproducible example and report it as a
// bug.
self.as_slice().hash(hasher);
self.hash(hasher);
}
}

Expand All @@ -228,7 +239,7 @@ impl FromIterator<u8> for ByteString {
}

impl BorrowFrom<ByteString> for [u8] {
fn borrow_from(owned: &ByteString) -> &[u8] { owned.0.as_slice() }
fn borrow_from(owned: &ByteString) -> &[u8] { &*owned.0 }
}

impl ToOwned<ByteString> for [u8] {
Expand Down
Loading

0 comments on commit 0418a8f

Please sign in to comment.