docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

#1. Bare tier

On this page

The bare tier (bare) defines the standard library items that are low-level or dangerous. Tools that operate outside safe abstractions, such as raw pointers, transmute, raw allocation, GC pinning and FFI primitives, are all gathered here.

#Dependencies

Upward, it depends only on the symbols the runtime exposes through the runtime ABI extern "nabi". Downward, core, std and user code are its dependents, but only through explicit imports.

#User import policy

nabi
use bare::ptr::CPtr;                             // explicit import required (danger signal)
use bare::alloc::{alloc, Layout};

let layout = try! Layout::array::<u8>(1024);
let p: CPtr<u8> = unsafe { alloc(layout) };    // null on failure

The implicitly applied prelude does not include unsafe items. bare::* therefore requires a separate explicit import, which makes visible in the code the point where the user enters dangerous territory.

#Module contents

#Pointer family (5 kinds)

#Memory / conversion

#GC integration

#FFI

#Core design principles

#1. Strict provenance - adopting the Rust 2024 model

Ptr<T> is an (address, provenance) pair, not a plain integer. A usize ↔ Ptr<T> round-trip therefore loses provenance and is allowed only through explicit escape hatches.

nabi
let p: Ptr<i32> = ...;
let addr: usize = p.addr();              // safe: address only
let q: Ptr<i32> = p.with_addr(addr + 4); // safe: provenance kept, only the address changes
let r: Ptr<i32> = unsafe { bare::ptr::from_exposed_provenance(addr) };  // unsafe: fresh provenance

as casts can never be used on pointers. The authority for this direction is Ralf Jung "Pointers Are Complicated", and it is isomorphic to the model stabilized in Rust 1.84 (Dec 2024).

#2. Arity is type-encoded - the Zig model

TypeMeaningC equivalent
Ptr<T>Exactly one TT* (single)
ConstPtr<T>Exactly one const Tconst T*
ManyPtr<T>N contiguous Ts (length unknown, C-compatible)T* (array)
Slice<T>N contiguous Ts (length included)(ptr, len)
CPtr<T>C-compatible nullable pointerT* (nullable)

Slice<T> is a safe view defined in 2.9 collections and does not belong to the bare tier. This chapter covers only the raw paths between a Slice and pointers: ManyPtr::slice, and a pin for the data pointer of a list.

#3. GC + raw pointer - two separate type levels

TypeMeaningWhen the GC moves the object
GcPtr<T>Reference to an object on the GC heap (usually T)Updated automatically
Ptr<T>raw pointer (unmanaged or pinned)Invalid

This distinction follows Go 1.21's runtime.Pinner model. A raw Ptr<T> is valid only within a GC pin, so using it after the pin is released is UB.

nabi
let obj: List<i32> = [1, 2, 3];                // GcPtr (automatic)
using pin = bare::gc::pin(obj) {          // pin begins
    let raw: Ptr<i32> = pin.as_data_ptr();
    ffi_function(raw, obj.len());
}                                              // pin released, raw invalid

#4. Casts only as explicitly named operations

The pitfalls of as casts on pointers in C and Rust are the biggest hazard. Nabi therefore exposes every unsafe cast only as an explicitly named function.

C/Rust notationNabi notationMeaning
ptr as *Ubare::ptr_cast<U>(p)Type change only (provenance kept)
ptr as *Alignedbare::align_cast(p)Alignment up-cast (none when misaligned)
*const → *mutbare::const_cast(p)Removes const
transmute<A,B>bare::reinterpret<A:Pod, B:Pod>(a)Bit reinterpretation (Pod enforced)

#5. RAII via using block - no reliance on finalizers

Every GC language has learned that finalizers are unreliable in a GC environment. External resources such as files, sockets and mutexes are therefore released deterministically, never by the GC: a Drop type is affine, the compiler releases it where its last owner leaves scope (on every exit) or at the end of its using block, and a resource whose close can fail must be closed explicitly. The GC never calls drop and there are no finalizers (stdlib 2.13.4).

nabi
use bare::ptr::{ConstPtr, CPtr};

extern "C" {
    fn fopen(path: ConstPtr<c_char>, mode: ConstPtr<c_char>) -> CPtr<u8>;
    fn fclose(f: CPtr<u8>) -> i32;
}

// a user-written wrapper: Drop closes the C handle
struct File { raw: CPtr<u8>; }

impl Drop for File {
    fn drop(ref self) { unsafe { fclose(self.raw) }; }
}

using f = File { raw: opened } {   // opened: non-null CPtr<u8> from fopen
    // ... use f.raw ...
}     // ← _guaranteed_ fclose at end of scope (RAII via Drop)

// ordinary GC objects need no using; the GC reclaims them
let xs: List<i32> = [1, 2, 3];     // GC automatic

The authority is the Drop trait of 2.13 marker; the syntax authority for the using block is refs-lang 3.17 using expression.

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