docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#2. Core tier

本頁內容

The core tier (core) is the safe standard library: the practical surface of Nabi that builds on every target. Because about 90% of user code uses items from this tier, it is the layer that determines what Nabi looks like in everyday programming.

#Location

The specification documents live at:

  • spec: spec/en/refs-standard-libraries/200-core/

#Dependencies

Upward, the core tier depends on bare for internal use; downward, std and user code depend on it. The core tier therefore sits at the center of the safe region, between bare and std.

#User import policy

use core::prelude::*; is applied implicitly: the compiler prepends it to every user file. The prelude's names (List, Dict, Display, Iterator, Mutex, task, ...) are usable without an explicit import; the rest of core goes through a prelude module (ops::Add, task::sleep) or a use.

Narrow explicit imports are also allowed where needed.

nabi
use core::collections::List;           // narrow, explicit
use core::ops::{Add, Sub};           // operator traits are not in the prelude

Note

The prelude's list is normative and holds no enum variants: a bare variant resolves from the expected type (refs-lang 2.4.8).

The full policy is covered in 0.2 Import policy.

#Module index

The modules provided by the core tier, grouped by area. Each entry ends with the core modules it specifies.

#Numbers / niche

#Operator traits

#iter / stream

#Collections

#Formatting / conversion

#Meta / markers

#Errors / strings

#Concurrency / async

#Cryptography

#Encoding

#I/O

#SIMD

#Benchmarks / optimizer hints

  • bench - Bench, the handle a #[bench] fn receives. The measurement is specified in refs-lang 9.9.11.

    nabi
    pub struct Bench { /* private fields */ }   // Debug: `Bench { name: "parse_1kb", .. }`
    impl Bench {
        pub fn iter<T>(ref self, f: fn() -> T);  // calibrate, warm up, sample and report f
        pub fn set_bytes(ref self, n: i64);      // bytes per call: adds a throughput column
        pub fn set_items(ref self, n: i64);      // items per call: adds a throughput column
    }
    
  • hint - black_box<T>(x: T) -> T: returns x unchanged, but the optimizer cannot see through it, so neither the computation that produced x nor a use of the result is folded away. Benchmarks wrap the inputs and result of a timed region with it.

Note

Portable SIMD vectors are in the core tier (2.24). Scientific computing areas such as tensors, automatic differentiation and GPU are not part of the core tier; they are split out into external libraries. External libraries reach the compiler's backend through the backend interface, planned for a later release.

#Core design principles

The design of the core tier follows four axes.

First, traits come first. Every semantic decision is made by trait method dispatch: + resolves to Add::add, and [i] indexing resolves to Index::index.

Second, conversions are explicit. A conversion that may lose information, such as i64 → i32, is rejected with a compile or runtime error, and policies such as saturate or wrap must be stated per operation (2.11).

Third, the tier aims to be GC-friendly. Collections and concurrency objects are managed by the GC. A Drop resource is affine: it is released where its last owner leaves scope or at the end of its using block (2.13.4).

Finally, target independence is guaranteed. Every item behaves identically on every supported target, and target-dependent items are split out into std.

Found a mistake? Edit this page on GitHub. Last updated