Browse the docs

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.

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, Darwin on aarch64, and Linux with GNU libc on x86_64 or aarch64.

Examples#

Read standard input#

silk
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#

silk
pub struct OsStandardInput

The 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.

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#

silk
impl OsStandardInput

Implementation OsStandardInput#

silk
impl OsStandardInput

Associated function make#

silk
pub fn make() -> OsStandardInput

Returns a provider. It does not read.

Implementation StandardInput for OsStandardInput#

silk
impl StandardInput for OsStandardInput

Operation read#

silk
read: OsStandardInput.read

Category: Input and output.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, wasm32-wasip1, wasm32-wasip2.

View the source of this page