Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #![feature(set_stdio)]
  2.  
  3. extern crate argdata;
  4. extern crate cloudabi;
  5.  
  6. use argdata::Argdata;
  7. use argdata::ArgdataExt;
  8.  
  9. pub struct Console(cloudabi::fd);
  10.  
  11. impl std::io::Write for Console {
  12. fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
  13. unsafe {
  14. let iovs = [
  15. cloudabi::ciovec {
  16. buf: (buf.as_ptr() as *const _ as *const (), buf.len()),
  17. },
  18. ];
  19. let mut nwritten = std::mem::uninitialized();
  20. cloudabi::fd_write(self.0, &iovs, &mut nwritten);
  21. }
  22. Ok(buf.len())
  23. }
  24.  
  25. fn flush(&mut self) -> std::io::Result<()> {
  26. Ok(())
  27. }
  28. }
  29.  
  30. fn main() {
  31. let ad = argdata::env::argdata();
  32. let mut it = ad.read_map().expect("argdata should be a map").iter_map();
  33. while let Some(Ok((key, val))) = it.next() {
  34. match key.read_str().expect("keys should be strings") {
  35. "console" => {
  36. if let Some(fd) = val.read_fd().ok() {
  37. std::io::set_print(Some(Box::new(Console(cloudabi::fd(fd.0 as u32)))));
  38. std::io::set_panic(Some(Box::new(Console(cloudabi::fd(fd.0 as u32)))));
  39. }
  40. }
  41. _ => {}
  42. }
  43. }
  44.  
  45. println!("Hello, world!");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement