Crates.io | char_stream |
lib.rs | char_stream |
version | 0.1.8 |
source | src |
created_at | 2018-01-01 05:20:40.718846 |
updated_at | 2018-01-02 12:52:44.824337 |
description | Unified character reading interface to str, String, bytes, File and Stdin for Rust language. |
homepage | |
repository | https://github.com/JunSuzukiJapan/char_stream |
max_upload_size | |
id | 45095 |
size | 31,637 |
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 世界❤");
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!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());
use char_stream::CharStream;
let s = String::from("Hello 世界❤");
let mut stream = CharStream::from_string(s);
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!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(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::from_bytes(&bytes) {
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!(' ', 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 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);
}
}
extern crate char_stream;
use char_stream::CharStream;
fn main() {
let input = "stressed";
let stream = CharStream::from(input);
let rev_stream = stream.wend_iter().rev();
let result: String = rev_stream.collect();
println!("'{}' reverse to '{}'", input , result);
}