silk.io.standard_input_native.
The native provider of the StandardInput effect: it reads the standard input of the process.
When to use#
Provide OsStandardInput for StandardInput in a native program. Code that reads uses
silk.io.standard_input, so a test can provide a fake with fixed input.
Details#
Each read reads once from the descriptor, directly into your buffer. It returns the count that
the system returned, which can be less than the buffer. When the system reports the end of
input, the provider remembers it. Later reads return the end without a system call.
Windows reads raw bytes through ReadFile; console bytes use the active input code page.
Errors#
StreamReadError if the system read fails.
Gotchas#
An empty buffer returns a count of 0 without a read, also after the end of input.
A read waits until input arrives or ends.
The provider exists on WASI, Windows on x86_64, Darwin on x86_64 or aarch64, and GNU Linux on x86_64 or aarch64.
Examples#
Read standard input#
import silk.core.effect { Effect }
import silk.io.standard_input_native { OsStandardInput }
import silk.io.standard_input { StandardInput, StreamReadError }
// Counts all bytes of standard input. Each read waits for input or its end.
effect fn countBytes() -> usize
! StreamReadError
? &mut StandardInput {
let mut buffer: [u8; 4096] = [0; 4096]
let mut total: usize = 0
while true {
let outcome = run StandardInput.receive(&mut buffer)
if StandardInput.isEndOfInput(&outcome) {
return total
}
total = total + StandardInput.count(&outcome)
}
return total
}
pub effect fn main() -> i32
! StreamReadError {
let mut input = OsStandardInput.make()
// An empty buffer returns 0 at once and does not read. Call `countBytes` to read everything.
let mut empty: [u8; 0] = []
let outcome = run StandardInput.receive(&mut empty)
|> Effect.provideMut<StandardInput>(&mut input)
if StandardInput.count(&outcome) != 0 {
return 1
}
return 0
}Import as OsStandardInput with import silk.io.standard_input_native { OsStandardInput }.
Public declarations: 1.
OsStandardInput#
pub struct OsStandardInputThe native StandardInput provider. The process owns the descriptor, and the provider does
not close it.
Details#
Each read reads once from the descriptor, directly into your buffer, and returns the count
that the system returned, which can be less than the buffer. After the system reports the
end of input, later reads return the end without a system call.
On Windows, ReadFile preserves redirected bytes and uses the active console input code
page for console bytes. Broken pipes end input; other read failures raise StreamReadError.
Gotchas#
An empty buffer returns a count of 0 without a read, also after the end of input.
A read waits until input arrives or ends.
Implementation OsStandardInput#
impl OsStandardInputImplementation OsStandardInput#
impl OsStandardInputAssociated function make#
pub fn make() -> OsStandardInputReturns a provider. It does not read.
Implementation StandardInput for OsStandardInput#
impl StandardInput for OsStandardInputOperation read#
read: OsStandardInput.readCategory: Input and output.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc.