• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use crate::descriptor::AsRawDescriptor;
6 use std::io::{stdin, Error, Read, Result};
7 use winapi::{
8     shared::{minwindef::LPVOID, ntdef::NULL},
9     um::{fileapi::ReadFile, minwinbase::LPOVERLAPPED},
10 };
11 
12 pub struct Console;
13 
14 impl Read for Console {
read(&mut self, out: &mut [u8]) -> Result<usize>15     fn read(&mut self, out: &mut [u8]) -> Result<usize> {
16         let mut num_of_bytes_read: u32 = 0;
17         // Safe because `out` is guarenteed to be a valid mutable array
18         // and `num_of_bytes_read` is a valid u32.
19         let res = unsafe {
20             ReadFile(
21                 stdin().as_raw_descriptor(),
22                 out.as_mut_ptr() as LPVOID,
23                 out.len() as u32,
24                 &mut num_of_bytes_read,
25                 NULL as LPOVERLAPPED,
26             )
27         };
28         let error = Error::last_os_error();
29         if res == 0 {
30             Err(error)
31         } else {
32             Ok(num_of_bytes_read as usize)
33         }
34     }
35 }
36