corevm-guest 0.1.28

API/SDK for CoreVM guest programs
Documentation
#![allow(clippy::missing_safety_doc)]

use crate::c;
use corevm_types::flags::ReadConsoleData;

/// Read data from the _console_ input stream (standard input) into `buf`.
///
/// Returns the number of bytes read.
#[inline]
pub fn read_console_data_into(buf: &mut [u8]) -> usize {
	unsafe { c::read_console_data(buf.as_ptr() as u64, buf.len() as u64, 0) as usize }
}

/// Read data from the _console_ input stream (standard input) into the supplied buffer.
///
/// Returns `None` if the there are no more data in the standard input.
/// Returns `Some(Err(n))` where `n` is the data size if the buffer is too small.
/// On success returns `Some(Ok(n))` where `n` is the data size.
#[inline]
pub fn try_read_console_data_into(buf: &mut [u8]) -> Option<Result<usize, usize>> {
	let len = unsafe {
		c::read_console_data(
			buf.as_mut_ptr() as u64,
			buf.len() as u64,
			ReadConsoleData::NON_BLOCKING.bits(),
		)
	};
	if len == u64::MAX {
		return None;
	}
	if len > buf.len() as u64 {
		return Some(Err(len as usize));
	}
	Some(Ok(len as usize))
}