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#
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#
pub struct OsHostInputThe 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#
impl OsHostInputAssociated function make#
pub fn make(inputs: NativeHostInput) -> OsHostInputReturns a provider that uses inputs. It does not allocate or read the process.
Implementation HostInput for OsHostInput#
impl HostInput for OsHostInputOperation argumentCount#
argumentCount: OsHostInput.argumentCountOperation argument#
argument: OsHostInput.argumentOperation variable#
variable: OsHostInput.variableOperation workingDirectory#
workingDirectory: OsHostInput.workingDirectoryCategory: System.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu.