Skip to content

Commit

Permalink
doc: update examples
Browse files Browse the repository at this point in the history
This mostly gets rid of lingering `extern crate` statements, and uses
`Box<dyn Error>` instead of `Box<Error>`.
  • Loading branch information
BurntSushi committed Jun 26, 2019
1 parent 0433c67 commit 775b836
Show file tree
Hide file tree
Showing 54 changed files with 326 additions and 750 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ryu = "1"
serde = "1.0.55"

[dev-dependencies]
serde_derive = "1.0.55"
serde = { version = "1.0.55", features = ["derive"] }
serde_bytes = "0.10.4"

[profile.release]
Expand Down
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
csv = "1"
```

and this to your crate root:

```rust
extern crate csv;
csv = "1.1"
```

### Example
Expand All @@ -42,13 +36,11 @@ There are more examples in the
[cookbook](https://docs.rs/csv/1.0.0/csv/cookbook/index.html).

```rust
extern crate csv;

use std::error::Error;
use std::io;
use std::process;

fn example() -> Result<(), Box<Error>> {
fn example() -> Result<(), Box<dyn Error>> {
// Build the CSV reader and iterate over each record.
let mut rdr = csv::Reader::from_reader(io::stdin());
for result in rdr.records() {
Expand Down Expand Up @@ -83,23 +75,21 @@ By default, the member names of the struct are matched with the values in the
header record of your CSV data.

```rust
extern crate csv;
#[macro_use]
extern crate serde_derive;

use std::error::Error;
use std::io;
use std::process;

#[derive(Debug,Deserialize)]
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Record {
city: String,
region: String,
country: String,
population: Option<u64>,
}

fn example() -> Result<(), Box<Error>> {
fn example() -> Result<(), Box<dyn Error>> {
let mut rdr = csv::Reader::from_reader(io::stdin());
for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic
Expand Down
6 changes: 1 addition & 5 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#![feature(test)]

extern crate csv;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate test;

use std::io;

use serde::de::DeserializeOwned;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use test::Bencher;

use csv::{
Expand Down
8 changes: 1 addition & 7 deletions csv-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
csv-core = "0.1"
```

and this to your crate root:

```rust
extern crate csv_core;
csv-core = "0.2"
```

### Build features
Expand Down
1 change: 0 additions & 1 deletion csv-core/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![feature(test)]

extern crate csv_core;
extern crate test;

use test::Bencher;
Expand Down
8 changes: 3 additions & 5 deletions csv-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,13 @@ foo,\"bar,baz\"
#![deny(missing_docs)]
#![no_std]

#[cfg(test)]
extern crate arrayvec;
extern crate memchr;

pub use crate::reader::{
ReadFieldNoCopyResult, ReadFieldResult, ReadRecordNoCopyResult,
ReadRecordResult, Reader, ReaderBuilder,
};
pub use crate::writer::{is_non_numeric, quote, WriteResult, Writer, WriterBuilder};
pub use crate::writer::{
is_non_numeric, quote, WriteResult, Writer, WriterBuilder,
};

mod reader;
mod writer;
Expand Down
14 changes: 3 additions & 11 deletions csv-index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
csv-index = "0.1"
```

and this to your crate root:

```rust
extern crate csv_index;
csv-index = "0.2"
```

### Example: build a simple random access index
Expand All @@ -36,19 +30,17 @@ data. This example shows how to save this index to disk for a particular CSV
file.

```rust
extern crate csv;
extern crate csv_index;

use std::error::Error;
use std::fs::File;
use std::io::{self, Write};

use csv_index::RandomAccessSimple;

fn main() {
example().unwrap();
}

fn example() -> Result<(), Box<Error>> {
fn example() -> Result<(), Box<dyn Error>> {
// Open a normal CSV reader.
let mut rdr = csv::Reader::from_path("data.csv")?;

Expand Down
16 changes: 2 additions & 14 deletions csv-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ and can be used by adding `csv-index` to your dependencies in your project's
```toml
[dependencies]
csv-index = "0.1"
```
and this to your crate root:
```ignore
extern crate csv_index;
csv-index = "0.2"
```
# Example: build a simple random access index
Expand All @@ -31,16 +25,13 @@ your CSV data has changed since the index was created, then the index will need
to be regenerated.
```no_run
extern crate csv;
extern crate csv_index;
use std::error::Error;
use std::fs::File;
use std::io::{self, Write};
use csv_index::RandomAccessSimple;
# fn main() { example().unwrap(); }
fn example() -> Result<(), Box<Error>> {
fn example() -> Result<(), Box<dyn Error>> {
// Open a normal CSV reader.
let mut rdr = csv::Reader::from_path("data.csv")?;
Expand Down Expand Up @@ -81,9 +72,6 @@ amenable to serializing to disk.)

#![deny(missing_docs)]

extern crate byteorder;
extern crate csv;

pub use crate::simple::RandomAccessSimple;

mod simple;
14 changes: 1 addition & 13 deletions csv-index/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ impl<W: io::Write> RandomAccessSimple<W> {
/// and query the number of records in the index.
///
/// ```
/// extern crate csv;
/// extern crate csv_index;
///
/// use std::io;
/// use csv_index::RandomAccessSimple;
///
Expand All @@ -74,9 +71,6 @@ impl<W: io::Write> RandomAccessSimple<W> {
/// in memory with `std::io::Cursor`, we write the index to a file.
///
/// ```no_run
/// extern crate csv;
/// extern crate csv_index;
///
/// use std::fs::File;
/// use std::io;
/// use csv_index::RandomAccessSimple;
Expand Down Expand Up @@ -137,9 +131,6 @@ impl<R: io::Read + io::Seek> RandomAccessSimple<R> {
/// and query the number of records in the index.
///
/// ```
/// extern crate csv;
/// extern crate csv_index;
///
/// use std::io;
/// use csv_index::RandomAccessSimple;
///
Expand Down Expand Up @@ -184,15 +175,12 @@ impl<R: io::Read + io::Seek> RandomAccessSimple<R> {
/// and use it to seek a CSV reader to read an arbitrary record.
///
/// ```
/// extern crate csv;
/// extern crate csv_index;
///
/// use std::error::Error;
/// use std::io;
/// use csv_index::RandomAccessSimple;
///
/// # fn main() { example().unwrap(); }
/// fn example() -> Result<(), Box<Error>> {
/// fn example() -> Result<(), Box<dyn Error>> {
/// let data = "\
/// city,country,pop
/// Boston,United States,4628910
Expand Down
2 changes: 0 additions & 2 deletions examples/cookbook-read-basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand Down
7 changes: 3 additions & 4 deletions examples/cookbook-read-colon.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;

fn example() -> Result<(), Box<dyn Error>> {
let mut rdr =
csv::ReaderBuilder::new().delimiter(b':').from_reader(io::stdin());
let mut rdr = csv::ReaderBuilder::new()
.delimiter(b':')
.from_reader(io::stdin());
for result in rdr.records() {
let record = result?;
println!("{:?}", record);
Expand Down
7 changes: 3 additions & 4 deletions examples/cookbook-read-no-headers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;

fn example() -> Result<(), Box<dyn Error>> {
let mut rdr =
csv::ReaderBuilder::new().has_headers(false).from_reader(io::stdin());
let mut rdr = csv::ReaderBuilder::new()
.has_headers(false)
.from_reader(io::stdin());
for result in rdr.records() {
let record = result?;
println!("{:?}", record);
Expand Down
6 changes: 2 additions & 4 deletions examples/cookbook-read-serde.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
extern crate csv;
#[macro_use]
extern crate serde_derive;

use std::error::Error;
use std::io;
use std::process;

use serde::Deserialize;

// By default, struct field names are deserialized based on the position of
// a corresponding field in the CSV data's header record.
#[derive(Debug, Deserialize)]
Expand Down
2 changes: 0 additions & 2 deletions examples/cookbook-write-basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand Down
6 changes: 2 additions & 4 deletions examples/cookbook-write-serde.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
extern crate csv;
#[macro_use]
extern crate serde_derive;

use std::error::Error;
use std::io;
use std::process;

use serde::Serialize;

#[derive(Debug, Serialize)]
struct Record {
city: String,
Expand Down
2 changes: 0 additions & 2 deletions examples/tutorial-error-01.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::io;

fn main() {
Expand Down
2 changes: 0 additions & 2 deletions examples/tutorial-error-02.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::io;
use std::process;

Expand Down
6 changes: 2 additions & 4 deletions examples/tutorial-error-03.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand All @@ -16,11 +14,11 @@ fn run() -> Result<(), Box<dyn Error>> {
for result in rdr.records() {
// Examine our Result.
// If there was no problem, print the record.
// Otherwise, convert our error to a Box<Error> and return it.
// Otherwise, convert our error to a Box<dyn Error> and return it.
match result {
Err(err) => return Err(From::from(err)),
Ok(record) => {
println!("{:?}", record);
println!("{:?}", record);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions examples/tutorial-error-04.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand Down
2 changes: 0 additions & 2 deletions examples/tutorial-perf-alloc-01.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand Down
20 changes: 9 additions & 11 deletions examples/tutorial-perf-alloc-02.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate csv;

use std::error::Error;
use std::io;
use std::process;
Expand All @@ -18,13 +16,13 @@ fn run() -> Result<u64, Box<dyn Error>> {
}

fn main() {
match run() {
Ok(count) => {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
process::exit(1);
}
}
match run() {
Ok(count) => {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
process::exit(1);
}
}
}
Loading

0 comments on commit 775b836

Please sign in to comment.