Rewrite input buffer management. Update for Rust master.