Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. use std::io::{Read, Write};
  2. use std::io::Error;
  3. use std::io::stdin;
  4. /*
  5. enum Action {
  6. ReadByte(u8),
  7. ReadString(Vec<u8>),
  8. Pause(u8),
  9. Failure(Error)
  10. }
  11.  
  12.  
  13. struct MockStdIo {
  14. actions: Vec<Action>
  15. }
  16.  
  17.  
  18. impl MockStdIo {
  19. fn build_from_actions(input:&[Action]) -> Self {
  20. unimplemented!();
  21. }
  22. }
  23.  
  24. impl Read for MockStdIo {
  25. fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
  26. match self.actions.pop() {
  27. Some(Action::Read(output)) => {
  28. let output = buf.write(&output);
  29.  
  30. unimplemented!();
  31. }
  32. None => {
  33. Ok(0)
  34. }
  35. _ => {
  36. unimplemented!();
  37. }
  38. }
  39. }
  40. }*/
  41.  
  42. fn main() {
  43. let mut test_input = "This is a test input".as_bytes();
  44.  
  45. generic(&mut test_input);
  46.  
  47. generic(&mut stdin());
  48. }
  49.  
  50.  
  51.  
  52. pub fn generic<R: Read>(r: &mut R){
  53. let mut x = String::new();
  54. println!("Reading out {:?}", r.read_to_string(&mut x));
  55. println!("With string {:?}", x);
  56. }
  57.  
  58.  
  59. pub fn by_trait(r: &mut Read){
  60. let mut x = String::new();
  61. println!("Reading out {:?}", r.read_to_string(&mut x));
  62. println!("With string {:?}", x);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement