Browse the docs

silk.system.host_input_native.

The native provider of the HostInput effect: command-line arguments, environment, and the working directory.

When to use#

A native main that requires &mut HostInput gets this provider from silk.platform.native.start. To make one yourself, capture a NativeHostInput from silk.platform.native.host_input, and pass it to OsHostInput.make.

Details#

The arguments and the environment come from the snapshot, so they do not change. The working directory is read from the process at each call. Each result is a new owned copy.

Gotchas#

The provider exists only on Darwin on aarch64, and Linux with GNU libc on x86_64 or aarch64.

Examples#

Read the working directory#

silk
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.collections.bytes { Bytes }
import silk.core.effect { Effect }
import silk.system.host_input { HostInput, HostInputError }
import silk.platform.native.host_input { NativeHostInput }
import silk.system.host_input_native { OsHostInput }

effect fn program() -> i32
  ! HostInputError | OutOfMemoryError
  ? &mut Allocator {
  // A snapshot of the environment, with no arguments. No other code changes the environment now.
  let snapshot = run unsafe NativeHostInput.environmentSnapshot()
  let mut inputs = OsHostInput.make(move snapshot)
  let count = run HostInput.argumentCount() |> Effect.provideMut<HostInput>(&mut inputs)
  let directory = run HostInput.workingDirectory() |> Effect.provideMut<HostInput>(&mut inputs)

  // The working directory is an absolute path.
  let path = Bytes.asSlice(&directory)
  if count != 0 || path.length == 0 || path[0] != 47 {
    return 1
  }
  return 0
}

effect fn failed<E>(error: E) -> i32 {
  drop error
  return 1
}

pub fn main() -> i32 {
  let mut allocator = Allocator.systemAllocatorProvider()
  return run Effect.catchAll(program(), failed) |> Effect.provideMut<Allocator>(&mut allocator)
}

Import as OsHostInput with import silk.system.host_input_native { OsHostInput }.

Public declarations: 1.

OsHostInput#

silk
pub struct OsHostInput

The native HostInput provider. It owns a snapshot of the arguments and the environment.

A native main that requires &mut HostInput gets one from silk.platform.native.start. The arguments and the environment come from the snapshot, so they do not change. The working directory is read from the process at each call. Each result is a new owned copy.

Implementation OsHostInput#

silk
impl OsHostInput

Associated function make#

silk
pub fn make(inputs: NativeHostInput) -> OsHostInput

Returns a provider that uses inputs. It does not allocate or read the process.

Implementation HostInput for OsHostInput#

silk
impl HostInput for OsHostInput

Operation argumentCount#

silk
argumentCount: OsHostInput.argumentCount

Operation argument#

silk
argument: OsHostInput.argument

Operation variable#

silk
variable: OsHostInput.variable

Operation workingDirectory#

silk
workingDirectory: OsHostInput.workingDirectory

Category: System.

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

View the source of this page