silk.build.source.

Files of the package read while it compiles: shaders, tables and other data the program keeps as part of its code.

When to use#

Use Source.read for a file's bytes and Source.text for UTF-8 text, from runtime code or from a static fn that parses the file while the package compiles. Use Source.compressed and Source.compressedText for a large file the program does not need at once: the program holds it compressed and expands it when asked. Use silk.system.filesystem for files the program reads when it runs.

Details#

The path is relative to the directory that holds the package's silk.toml, uses / separators and has no . or .. segment, so a package reads only its own files. Write it as a string literal in the call: the build finds the files a package reads from those literals. The file's contents become a constant of the program. Editing the file rebuilds only the functions that read it. The file is read as bytes and never as Silk source, whatever it holds, and a package that depends on this one builds with the same contents.

Examples#

Embed a shader#

silk
import silk.build.source { Source }

pub fn sceneShader() -> &'static [u8] {
  return Source.read("shaders/scene.wgsl")
}

Keep a large script compressed#

silk
import silk.build.source { CompressedText, Source }
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.text.string { String }

pub effect fn library() -> String ! OutOfMemoryError ? &mut Allocator {
  let packed = Source.compressedText("js/index.js")
  return run CompressedText.text(&packed)
}

Import as Source with import silk.build.source { Source }.

Public declarations: 3.

Source#

silk
pub struct Source

The owner type of the package file reads. It has no fields and is never constructed.

Details#

Each operation is static: runtime code that calls one receives its result as a constant.

Implementation Source#

silk
impl Source

Associated function read#

silk
pub static fn read(path: string<'static>) -> &'static [u8]

Returns the bytes of the package file at path.

Gotchas#

A path that leaves the package, or names no file, is a compile error at the call.

Associated function text#

silk
pub static fn text(path: string<'static>) -> string<'static>

Returns the text of the package file at path.

Gotchas#

A file that is not valid UTF-8 is a compile error at the call, as is a path that leaves the package or names no file.

Associated function compressed#

silk
pub static fn compressed(path: string<'static>) -> Compressed

Returns the package file at path, kept compressed in the program.

Details#

The build compresses the file as one Zstandard frame, and the program holds only that frame. Compressed.bytes expands it when the program asks. A file that does not shrink is stored as it is, a few bytes larger.

Gotchas#

A path that leaves the package, or names no file, is a compile error at the call.

Associated function compressedText#

silk
pub static fn compressedText(path: string<'static>) -> CompressedText

Returns the text of the package file at path, kept compressed in the program.

Details#

The build checks that the file is UTF-8 and compresses it as one Zstandard frame, and the program holds only that frame. CompressedText.text expands it when the program asks.

Gotchas#

A file that is not valid UTF-8 is a compile error at the call, as is a path that leaves the package or names no file.

Compressed#

silk
pub struct Compressed

A package file the program holds compressed, from Source.compressed.

Details#

The value is a borrow of a constant of the program, so it copies freely and lives as long as the program does. Nothing is expanded until Compressed.bytes runs, and each call expands the file again: keep the result when the program needs it more than once.

Implementation Copy for Compressed#

silk
impl Copy for Compressed

Implementation Compressed#

silk
impl Compressed

Method length#

silk
pub fn length(self: &Self) -> usize

Returns how many bytes the file holds once expanded.

Method compressedLength#

silk
pub fn compressedLength(self: &Self) -> usize

Returns how many bytes the program holds for the file.

Method bytes#

silk
pub effect fn bytes(self: &Self) -> Bytes ! OutOfMemoryError ? &mut Allocator

Expands the file and returns its bytes, exactly as the build read them.

Details#

Besides the result, it allocates a decoder for the length of the call: a window as large as the file, up to 4 MiB, and about 400 KiB of buffers and tables.

Errors#

  • OutOfMemoryError if the Allocator cannot supply the result or the decoder.

CompressedText#

silk
pub struct CompressedText

A UTF-8 package file the program holds compressed, from Source.compressedText.

Details#

The value is a borrow of a constant of the program, so it copies freely and lives as long as the program does. Nothing is expanded until CompressedText.text runs, and each call expands the file again: keep the result when the program needs it more than once.

Implementation Copy for CompressedText#

silk
impl Copy for CompressedText

Implementation CompressedText#

silk
impl CompressedText

Method length#

silk
pub fn length(self: &Self) -> usize

Returns how many bytes of UTF-8 the text holds once expanded.

Method compressedLength#

silk
pub fn compressedLength(self: &Self) -> usize

Returns how many bytes the program holds for the text.

Method text#

silk
pub effect fn text(self: &Self) -> String ! OutOfMemoryError ? &mut Allocator

Expands the file and returns its text.

Details#

The build already checked that the file is UTF-8, so the text is not checked again. Besides the result, it allocates a decoder for the length of the call: a window as large as the file, up to 4 MiB, and about 400 KiB of buffers and tables.

Errors#

  • OutOfMemoryError if the Allocator cannot supply the result or the decoder.

Category: Core.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page