# Rust reference

> Summary of the dvm-run crate's types and methods, plus links to generated rustdoc on docs.rs.

This is the Markdown of https://docs.dvm.run/reference/rust/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

This page summarizes the dvm-run crate from `rust/dvm-run/src` (version 0.3.0). For the full generated API reference, see [dvm-run](https://docs.rs/dvm-run) and [dvm-api](https://docs.rs/dvm-api) on docs.rs, which are generated directly from each release.

## `Client`

| Call                            | Returns           |                                                                                             |
| ------------------------------- | ----------------- | ------------------------------------------------------------------------------------------- |
| `Client::from_env()`            | `Result<Client>`  | `DVM_API_URL` or `https://sandbox.dvm.run`; `DVM_API_KEY` or the key `dvm-run login` saved. |
| `Client::new(api_url, api_key)` | `Client`          |                                                                                             |
| `.api_url()`                    | `&str`            |                                                                                             |
| `.create_sandbox(image)`        | `SandboxBuilder`  | Await it for the `Sandbox`, once it runs, or has exited.                                    |
| `.connect_sandbox(id).await`    | `Result<Sandbox>` | A handle to a sandbox made elsewhere.                                                       |

`SandboxBuilder` configures `.command(argv)`, `.entrypoint(s)`, `.cpus(n)`, `.memory_mib(n)`, `.seed(n)`, `.env(name, value)`, `.name(s)`, and `.network(SandboxNetwork)`. Dropping the future before the sandbox starts stops the sandbox.

## `Sandbox`

Implements `Clone`; clones share the same session. Dropping a handle neither ends nor stops the sandbox.

| Call                                                                                | Returns                              |                                                                                                                                     |
| ----------------------------------------------------------------------------------- | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| `.id()`, `.name()`, `.spec()`                                                       | `SandboxId`, `String`, `SandboxSpec` |                                                                                                                                     |
| `.state().await`                                                                    | `Result<RunState>`                   |                                                                                                                                     |
| `.pause().await`, `.resume().await`                                                 | `Result<()>`                         |                                                                                                                                     |
| `.set_network(&SandboxNetwork).await`                                               | `Result<()>`                         | From now on; open connections stay.                                                                                                 |
| `.stop().await`                                                                     | `Result<()>`                         | One that already ended stays as it is.                                                                                              |
| `.wait().await`                                                                     | `Result<i32>`                        | The exit code; a stopped or failed run returns its error.                                                                           |
| `.run(command)`                                                                     | `RunBuilder`                         | A session’s command, to its end: `.env(name, value)`, `.cwd(path)`, `.user(user)`, `.stdin(bytes)`, then await a `CommandResult`.   |
| `.start(command)`                                                                   | `StartBuilder`                       | The same options; await a `Command` once started. Without `.stdin`, stdin stays open.                                               |
| `.write_file(path, data)`                                                           | `WriteFileBuilder`                   | `.mode(m)`, then await.                                                                                                             |
| `.read_file(path).await`                                                            | `Result<Vec<u8>>`                    |                                                                                                                                     |
| `.end().await`                                                                      | `Result<i32>`                        | Ends a session’s input; waits for the exit code.                                                                                    |
| `.output().await`                                                                   | `Result<RunOutput>`                  | What the first process wrote so far.                                                                                                |
| `.seek_end()`, `.seek_at_ns(ns)`, `.seek_event(thread, event)`, `.seek(SeekTarget)` | `SeekBuilder`                        | `.watch(WatchPoint)`, `.watches(…)`, then await a `SandboxPoint`.                                                                   |
| `.scope(async \|sandbox\| { … }).await`                                             | `Result<T>`                          | Runs the work, then ends a session’s input and waits, or stops a paused sandbox or a program, even if the work fails or is dropped. |

Commands accept `impl Into<CommandLine>`: a `&str` or `String` runs as a shell script, and an array or `Vec` of strings runs as argv.

## `SandboxPoint`

| Call                                                                    | Returns                           |                                                                       |
| ----------------------------------------------------------------------- | --------------------------------- | --------------------------------------------------------------------- |
| `.sandbox_id()`, `.thread()`, `.process()`, `.event()`, `.operations()` |                                   | Where it stopped.                                                     |
| `.ns()`                                                                 | `Option<u64>`                     | About when, in the recording’s time.                                  |
| `.at()`                                                                 | `&StopEvent`                      |                                                                       |
| `.stats()`                                                              | `&HashMap<String, u64>`           |                                                                       |
| `.step()`                                                               | `StepBuilder`                     | `.watch(…)`, then await an `Option<SandboxPoint>`: `None` at the end. |
| `.read_file(path).await`                                                | `Result<Vec<u8>>`                 |                                                                       |
| `.list_dir(path).await`                                                 | `Result<Vec<PathDirectoryEntry>>` |                                                                       |
| `.stat(path)`                                                           | `StatBuilder`                     | `.follow_symlinks(bool)`, then await a `FileStatus`.                  |

## `Command`

| Call                        | Returns                 |                                  |
| --------------------------- | ----------------------- | -------------------------------- |
| `.wait().await`             | `Result<CommandResult>` | `exit_code`, `stdout`, `stderr`. |
| `.signal(n).await`          | `Result<()>`            | 1 to 64, to its process group.   |
| `.write_stdin(bytes).await` | `Result<()>`            | Queues the bytes.                |
| `.close_stdin().await`      | `Result<()>`            | After the queued bytes.          |

## API types

`dvm_run::api` re-exports `dvm-api`: `SandboxNetwork`, `RunState`, `SeekTarget`, `WatchPoint`, `SyscallName`, `StopEvent`, `FileStatus`, `PathDirectoryEntry`, `RunOutput`, the frame codec, and the remaining HTTP API types.

## Errors

`dvm_run::Error` variants: `InvalidRequest`, `Authentication`, `NotFound`, `Conflict`, `Refused { status, reason }`, `SandboxEnded`, `SandboxFailed`, `NoApiKey`, `Credentials`, `Network`, `SandboxRequest`, and `Unexpected`. See [Errors](/reference/errors/).

## Also exported

`login` and `LoggedIn` (the flow behind `dvm-run login`), `save_key` and `credentials_path` (for the saved credentials file), and `DEFAULT_API_URL`.

## Command line

Install with `cargo install dvm-run`, then run `dvm-run login [--api-url URL] [--no-browser]` ([Getting in](/start/access/)).
