Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 17, 2018 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/3de46ebf1866f6417fd5fc2a95a8c639 to your computer and use it in GitHub Desktop.
Save rust-play/3de46ebf1866f6417fd5fc2a95a8c639 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::io::{self,BufRead,BufReader};
enum Utf8DecoderError<'a> {
InvalidBytes(&'a [u8]),
Io(io::Error),
}
struct Utf8Decoder<B: BufRead> {
reader : B,
bytes_read: usize
}
enum DecodeResult<'a> {
EOF,
Error(Utf8DecoderError<'a>),
Char(char)
}
impl<B: BufRead> Utf8Decoder<B> {
fn new(r : B) -> Self {
Self { reader: r, bytes_read: 0 }
}
fn decode(&mut self) -> DecodeResult {
DecodeResult::Char('\0')
}
}
fn main() {
let s = String::from("Hello, world!");
let b = BufReader::new(s.as_bytes());
let decoder = Utf8Decoder::new(b);
loop {
match decoder.decode() {
DecodeResult::EOF => {
break;
}
DecodeResult::Error(e) => panic!(e),
DecodeResult::Char(c) => {
println!("{}", c);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment