Skip to content

Instantly share code, notes, and snippets.

@vsajip
Forked from rust-play/playground.rs
Last active October 17, 2018 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsajip/eaa14008d6cad7bf4d23ec42a3ad841d to your computer and use it in GitHub Desktop.
Save vsajip/eaa14008d6cad7bf4d23ec42a3ad841d 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!("Failed"),
DecodeResult::Char(c) => {
println!("{}", c);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment