-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example to demonstrate stream decoding.
- Loading branch information
1 parent
e567a2d
commit 79f0332
Showing
3 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
extern crate csv; | ||
|
||
use std::comm::channel; | ||
use std::io::{ChanReader, ChanWriter, Reader, Writer}; | ||
use std::io::timer::sleep; | ||
use std::task::spawn; | ||
|
||
use csv::{Decoder, Encoder}; | ||
|
||
fn main() { | ||
let (send, recv) = channel(); | ||
spawn(proc() { | ||
let mut w = ChanWriter::new(send); | ||
let mut enc = Encoder::to_writer(&mut w as &mut Writer); | ||
for x in range(1, 6) { | ||
match enc.encode((x, x * x)) { | ||
Ok(_) => {}, | ||
Err(err) => fail!("Failed encoding: {}", err), | ||
} | ||
sleep(1000); | ||
} | ||
}); | ||
|
||
let mut r = ChanReader::new(recv); | ||
// We create a CSV reader with a small buffer so that we can see streaming | ||
// in action on small inputs. | ||
let mut dec = Decoder::from_reader_capacity(&mut r as &mut Reader, 1); | ||
for r in dec { | ||
println!("Record: {}", r); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters