Unified character reading interface to str, String, bytes, File and Stdin for Rust language.
In Cargo.toml:
[dependencies]
char_stream = "*"
``` use char_stream::CharStream;
let mut stream = CharStream::from("Hello 世界❤");
asserteq!('H', stream.next().unwrap()); asserteq!('e', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('o', stream.next().unwrap()); asserteq!(' ', stream.next().unwrap()); asserteq!('世', stream.next().unwrap()); asserteq!('界', stream.next().unwrap()); asserteq!('❤', stream.next().unwrap()); asserteq!(None, stream.next()); ```
``` use char_stream::CharStream;
let s = String::from("Hello 世界❤"); let mut stream = CharStream::from_string(s);
asserteq!('H', stream.next().unwrap()); asserteq!('e', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('o', stream.next().unwrap()); asserteq!(' ', stream.next().unwrap()); asserteq!('世', stream.next().unwrap()); asserteq!('界', stream.next().unwrap()); asserteq!('❤', stream.next().unwrap()); asserteq!(None, stream.next()); ```
``` use char_stream::CharStream;
let bytes: [u8; 15] = [72, 101, 108, 108, 111, 32, 228, 184, 150, 231, 149, 140, 226, 157, 164]; if let Ok(mut stream) = CharStream::frombytes(&bytes) { asserteq!('H', stream.next().unwrap()); asserteq!('e', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('l', stream.next().unwrap()); asserteq!('o', stream.next().unwrap()); asserteq!(' ', stream.next().unwrap()); asserteq!('世', stream.next().unwrap()); asserteq!('界', stream.next().unwrap()); asserteq!('❤', stream.next().unwrap()); assert_eq!(None, stream.next()); } ```
``` extern crate tempfile; extern crate char_stream;
use std::io::prelude::*; use std::io::{Seek, SeekFrom}; use std::fs::File; use char_stream::CharStream;
fn main(){ let test_data = "Hello\n 世界❤";
// write test data to tempfile
let mut tmpfile: File = tempfile::tempfile().unwrap();
tmpfile.write_all(test_data.as_bytes()).unwrap();
// Seek to start
tmpfile.seek(SeekFrom::Start(0)).unwrap();
// read test data from tempfile
let mut stream = CharStream::from_file(tmpfile);
assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!('\n', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
} ```
``` extern crate char_stream;
use char_stream::CharStream;
fn main() { let mut stream = CharStream::from_stdin(); while let Some(ch) = stream.next() { println!("ch: {}", ch); } } ```