Created
October 17, 2018 06:27
-
-
Save rust-play/3de46ebf1866f6417fd5fc2a95a8c639 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
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