Skip to main content

corevm_guest/input/
console.rs

1#![allow(clippy::missing_safety_doc)]
2
3use crate::c;
4use corevm_types::flags::ReadConsoleData;
5
6/// Read data from the _console_ input stream (standard input) into `buf`.
7///
8/// Returns the number of bytes read.
9#[inline]
10pub fn read_console_data_into(buf: &mut [u8]) -> usize {
11	unsafe { c::read_console_data(buf.as_ptr() as u64, buf.len() as u64, 0) as usize }
12}
13
14/// Read data from the _console_ input stream (standard input) into the supplied buffer.
15///
16/// Returns `None` if the there are no more data in the standard input.
17/// Returns `Some(Err(n))` where `n` is the data size if the buffer is too small.
18/// On success returns `Some(Ok(n))` where `n` is the data size.
19#[inline]
20pub fn try_read_console_data_into(buf: &mut [u8]) -> Option<Result<usize, usize>> {
21	let len = unsafe {
22		c::read_console_data(
23			buf.as_mut_ptr() as u64,
24			buf.len() as u64,
25			ReadConsoleData::NON_BLOCKING.bits(),
26		)
27	};
28	if len == u64::MAX {
29		return None;
30	}
31	if len > buf.len() as u64 {
32		return Some(Err(len as usize));
33	}
34	Some(Ok(len as usize))
35}