|
| 1 | +usecrate::io; |
| 2 | + |
| 3 | +pubstructStdin; |
| 4 | +pubstructStdout; |
| 5 | +pubstructStderr; |
| 6 | + |
| 7 | +implStdin{ |
| 8 | +pubconstfnnew() -> Stdin{ |
| 9 | +Stdin |
| 10 | +} |
| 11 | +} |
| 12 | + |
| 13 | +impl io::ReadforStdin{ |
| 14 | +fnread(&mutself,_buf:&mut[u8]) -> io::Result<usize>{ |
| 15 | +Ok(0) |
| 16 | +} |
| 17 | +} |
| 18 | + |
| 19 | +implStdout{ |
| 20 | +pubconstfnnew() -> Stdout{ |
| 21 | +Stdout |
| 22 | +} |
| 23 | +} |
| 24 | + |
| 25 | +impl io::WriteforStdout{ |
| 26 | +fnwrite(&mutself,buf:&[u8]) -> io::Result<usize>{ |
| 27 | +_write(libc::STDOUT_FILENO, buf) |
| 28 | +} |
| 29 | + |
| 30 | +fnflush(&mutself) -> io::Result<()>{ |
| 31 | +Ok(()) |
| 32 | +} |
| 33 | +} |
| 34 | + |
| 35 | +implStderr{ |
| 36 | +pubconstfnnew() -> Stderr{ |
| 37 | +Stderr |
| 38 | +} |
| 39 | +} |
| 40 | + |
| 41 | +impl io::WriteforStderr{ |
| 42 | +fnwrite(&mutself,buf:&[u8]) -> io::Result<usize>{ |
| 43 | +_write(libc::STDERR_FILENO, buf) |
| 44 | +} |
| 45 | + |
| 46 | +fnflush(&mutself) -> io::Result<()>{ |
| 47 | +Ok(()) |
| 48 | +} |
| 49 | +} |
| 50 | + |
| 51 | +pubconstSTDIN_BUF_SIZE:usize = 0; |
| 52 | + |
| 53 | +pubfnis_ebadf(_err:&io::Error) -> bool{ |
| 54 | +true |
| 55 | +} |
| 56 | + |
| 57 | +pubfnpanic_output() -> Option<impl io::Write>{ |
| 58 | +Some(Stderr) |
| 59 | +} |
| 60 | + |
| 61 | +fn_write(fd:i32,message:&[u8]) -> io::Result<usize>{ |
| 62 | +letmut iov = |
| 63 | + libc::iovec{iov_base: message.as_ptr()as*mut_,iov_len: message.len()}; |
| 64 | +loop{ |
| 65 | +// SAFETY: syscall, safe arguments. |
| 66 | +let ret = unsafe{ libc::writev(fd,&iov,1)}; |
| 67 | +if ret < 0{ |
| 68 | +returnErr(io::Error::last_os_error()); |
| 69 | +} |
| 70 | +let ret = ret asusize; |
| 71 | +if ret > iov.iov_len{ |
| 72 | +returnErr(io::Error::last_os_error()); |
| 73 | +} |
| 74 | +if ret == iov.iov_len{ |
| 75 | +returnOk(message.len()); |
| 76 | +} |
| 77 | +// SAFETY: ret has been checked to be less than the length of |
| 78 | +// the buffer |
| 79 | + iov.iov_base = unsafe{ iov.iov_base.add(ret)}; |
| 80 | + iov.iov_len -= ret; |
| 81 | +} |
| 82 | +} |
0 commit comments