docs

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

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

#0.5. Notation

本頁內容

The notational conventions of this specification follow refs-lang 0.8 Notation as is. This chapter defines the additional conventions that apply only to the standard library specification.

#0.5.1. Trait signature notation

Each trait spec begins with a signature block that gathers all methods, associated types and supertraits in one place.

nabi
trait Add<Rhs = Self> {
    type Output;
    #[infix("+")]
    fn add(self, rhs: Rhs) -> Output;
}

This form is identical to how the standard library is actually written, so users can use the same format in their own code.

#0.5.2. Method signature tables

A trait defining several methods is also summarized in table form, as follows.

MethodSignatureMeaning
addfn(self, rhs: Rhs) -> OutputArithmetic addition
subfn(self, rhs: Rhs) -> OutputArithmetic subtraction

#0.5.3. Trait implementation matrix

At the end of each trait spec, a table states which standard library types implement the trait. The columns are only the type and the binding mechanism; implementation file paths are left outside the spec.

markdown
## X.Y.Z. Trait implementation matrix

| Type  | binding                          | Notes                          |
|-------|----------------------------------|--------------------------------|
| `i32` | `#[intrinsic("nabi.add.i32")]`    | Direct to compiler PrimOp      |
| `i64` | `#[intrinsic("nabi.add.i64")]`    |                                |
| `f32` | `#[intrinsic("nabi.add.f32")]`    |                                |
| `f64` | `#[intrinsic("nabi.add.f64")]`    |                                |
| `Int` | (no intrinsic, arbitrary precision) | Defined by the standard library |

#0.5.4. Semantics

The meaning of each method is written out in prose, covering not only form and examples but also limits and error cases.

markdown
### `Add::add(self, rhs)`

Arithmetic addition. Returns the sum of the two values.

- `i32 + i32`: panics on overflow (checks on in every profile; wraps under `-C overflow-checks=no`)
- `f64 + f64`: IEEE 754 addition
- `Int + Int`: arbitrary precision; never overflows

Overflow handling follows the fixed-width arithmetic rules of [2.1 Numeric types](../200-core/201-numbers.md).
Saturating / checked / wrapping variants are exposed as separate methods (`saturating_add`, etc.).

#0.5.5. Examples

Each trait and method is accompanied by one or more working examples. Examples assume the implicit prelude import, use core::prelude::*;, is in effect, so they must be code that runs as is without any imports.

nabi
let a: i32 = 5;
let b: i32 = 3;
let c = a + b;        //→ 8 (i32)

let x: f64 = 1.5;
let y: f64 = 2.5;
let z = x + y;        //→ 4.0 (f64)

#0.5.6. Cross-reference

References to other documents use one of two forms depending on location. Within this specification, write [2.7 iter](../200-core/207-iter.md) or refer informally to "refs-standard-libraries 2.7"; references to refs-lang follow the form [refs-lang 6.4](../../refs-lang/006-meta-system/604-lang-magic.md).

#0.5.7. Separation of specification and implementation

This document defines only the specification. The actual implementations of the compiler and the standard libraries are maintained in separate repositories; in case of conflict with this specification, this specification takes precedence.

#0.5.8. The try_ prefix

A try_ name marks one of two forms, and only these:

  1. A non-blocking attempt: it does now what the plain form waits for, and reports that it could not (Mutex::try_lock, Receiver::try_recv, Sender::try_send).
  2. A form that passes on the throw of the closure or future it takes (Iterator::try_fold, Iterator::try_for_each, par::try_scope, task::try_block_on).

A throwing form of a trait method that cannot throw keeps the try_ name too: TryFrom::try_from, TryInto::try_into, File::try_clone. No other function takes the prefix: a function that fails throws under its plain name (0.5.9), so there is no try_ twin of it. try_ is a prefix only; no name ends in _try.

#0.5.9. Error model

Each function reports failure through exactly one channel, chosen by what the failure is:

FailureChannelExamples
An ordinary "no answer" with one cause-> T | noneList::get, Iterator::find, Path::parent, Path::strip_prefix, checked_*, char::from_u32, NonZero::new
Caused by the input, the environment or I/O, or with several causes the caller may tell apartthrows (E)FromStr::from_str, str::from_utf8, Int::parse_radix, FormatSpec::parse, ClientConfig::new, the fs and net operations
A caller bug the caller can check cheaply firstpanic, #[track_caller]an operator index out of range, a radix outside 2..=36, a zero chunk size
  1. No twins. No two functions differ only in the failure channel. try? f() is the T | none form of every throwing call (refs-lang 3.14.3), so the library has no _or_none twin of a throwing function and no panicking twin of one.
  2. No sentinels. A result never encodes failure in a value of its own type (0, -1, ""): it is T | none, or the function throws.
  3. Closing throws. close() of an OS resource (a file, a socket, a TLS connection) throws what the OS reports. Drop never throws: a value released by Drop drops the error.
  4. An operator and its named method may differ: xs[i] panics where xs.get(i) returns none.
  5. An operator cannot throw (2.3): a fallible operator returns a Result (Finite<F> + Finite<F> is Result<Finite<F>, OverflowError>).

#0.5.10. Error types

Every error type in the standard libraries follows these rules:

  1. Private fields. An error is built by a constructor (new(..), or a named one such as IoError::from_os_code), never by a struct literal, and read through getters named after the fact (kind(), position(), path(), os_code()). An error that only the library raises (LayoutError, NulError) may have no public constructor. An error that is an enum of causes (FromBytesWithNulError, VarError) carries its data in variant fields, read by pattern. Every error type has message() -> str: what went wrong, without the context its Display may add.
  2. Trait set. Every error type implements Display, Debug, Error, PartialEq, Eq and Clone. An error that carries a value of a type parameter (SendError<T>, TrySendError<T>) implements the first three.
  3. Kinds. A kind getter returns an enum (IoErrorKind, TlsErrorKind, JoinErrorKind), never a str or an integer, and the enum is #[non_exhaustive] (DecodeErrorKind too). An OS error code is os_code() -> i32 | none, none when the error did not come from the OS.
  4. Unit structs. An error with no fields is a unit struct (struct TimeoutError;), written TimeoutError, not TimeoutError {}.
  5. One error per failure space. core keeps the shared errors (ValueError, ParseError, OverflowError, ConversionError, TypeError, Utf8Error) and the errors of its own failure spaces (ZeroError, DecodeError, FmtError, CancellationError, TimeoutError); a std module has one error type per failure space (IoError, TlsError, NabionError; env has VarError for reading a variable and JoinPathsError for join_paths; process has SignalError for installing a handler beside IoError). Two errors never cover the same failure. Every failed UTF-8 check throws Utf8Error.
  6. Messages. A message starts in lower case (proper nouns and type names excepted), with no trailing period and no error: prefix. An operator's panic uses Rust's text (index 5 out of bounds for length 3). A method's panic starts Type::method: (a free function's module::fn: ), then the problem, then ; <what to write instead> when there is one: i32::isqrt: the number is negative; use checked_isqrtto getnone instead. A public function that panics on a bad argument is #[track_caller].

#0.5.11. Naming

Every name in the standard library follows this table. A name that breaks it is a defect of the library, not a precedent.

KindRule
constructorsnew (required arguments), with_<x> (an extra argument), from_<src> (a conversion, Formatter::from_sink), empty() / zeroed() for special values; a unit constructor is named after its unit (Duration::secs(n), ms, ns); List::filled(n, x) (n copies) and BinaryHeap::by_ordering(cmp) are named exceptions; never to_<x>
conversionsas_ a free view, to_ a new owned value (costly), into_ consumes; str is the string type, so to_str, read_to_str, from_str, never to_string
numeric conversionslossless and checked ones go through From / TryFrom / SaturatingFrom / WrappingFrom; an inherent to_<num> only for a lossy rounding (to_f64), and on Int / Float the checked to_<num>() -> T | none beside TryFrom (2.1.2); Ipv4Addr::to_bits / from_bits stay beside From<Ipv4Addr> for u32 and From<u32> for Ipv4Addr, as in Rust
gettersno get_ prefix; get only for a keyed or indexed lookup, or the value of a wrapper (NonZero::get); the one named exception is a guard's get_clone(), a deep copy beside its scoped get() (2.16)
predicatesis_ / has_, or Rust's established word (success, readonly)
settersset_<x>(ref self, ..) returns unit; a handle (a file, socket, cursor or reader) changes OS or shared state, so its setter takes self (File::set_len, TcpStream::set_nodelay, Cursor::set_position); a builder uses the bare field name and returns Self
sizeslen = element or byte count of a container; <x>_len = a length property; count = something counted (count_ones); size = memory size
add / removepush / pop at the ends, insert / remove keyed or indexed, extend many, clear, retain, take moves out
iterationiter() returns <Type>Iter (BinaryHeap::iter returns HeapIter); any other iterator method returns its own name in CamelCase, in the defining module (below), except Iterator::inspect, which returns InspectIter because Inspect is the reflection trait (2.12), and the owning stream adapters (into_lines, into_bytes, into_split, into_take, their _async forms, into_incoming), which return io::Owned<V, R> around the view's type (2.23.1)
reflectionthe plain form is the type's answer (T::type_name(), T::size()), the _of_val form the value's answer at run time (x.type_name_of_val(), x.size_of_val(), x.field_names_of_val())
async<name>_async is the twin of a blocking <name>; never an async_ prefix
abbreviationsspelled out, except the fixed set: len, fd, io, utf8, ns / us / ms / secs / mins, tls, der / pem, mmap, pid, alpn, ocsp, dns, and the short forms Rust established: addr, dir, temp, exe, env, var / vars, args, arg, ty, os, std (Stdio), ptr, buf, recv, ttl, subsec, and the math names abs, rem, ln, exp, cbrt, min / max
constantsSCREAMING_SNAKE; no function twin of a constant (f64::INFINITY)
modulesa singular noun or area (fs, net, iter); collections and ops stay plural

Iterator types. str: chars() -> Chars, bytes() -> Bytes, char_indices() -> CharIndices, lines() -> Lines, split() -> Split, splitn() -> SplitN, rsplit() -> RSplit, rsplitn() -> RSplitN, split_whitespace() -> SplitWhitespace, match_indices() -> MatchIndices. Dict: keys() -> Keys, values() -> Values. List / Slice: windows() -> slice::Windows, chunks() -> slice::Chunks (beside the adapters iter::Windows / iter::Chunks). Receiver::iter() -> ReceiverIter. A leaf name that two modules share is written with its module (slice::Windows, str::Bytes).

#0.5.12. Trait coverage, #[non_exhaustive] and #[sealed]

Every public type of the standard library implements the traits of its class:

Type classImplements
every public typeDebug
value types (addresses, paths, ids, times, kinds, algorithms, ranges, specs)Clone, PartialEq; Eq when equality is total; Hash when Eq; PartialOrd / Ord when the values are ordered; Copy when small
types with an obvious empty or zero valueDefault
text-like typesDisplay

Debug output by kind:

  • a value prints its contents: 0..3, 0..=3, Start(3), Permissions { mode: 0o644, readonly: false }; an address prints its text, as Display does (127.0.0.1:80, 3.2.4); a StrBuf prints as its text, quoted;
  • a handle prints its type and what names it: File { path: "a.txt" }, Child { pid: 42 }, Thread { id: ThreadId(3), name: "worker" }; a handle with nothing cheap to show prints its type name (TcpStream, Mutex);
  • an iterator, stream or future prints its type name (Map, Chars, AsyncFuture);
  • a value holding a secret never prints it: it prints its type and only its public parameters, in one form, Type { field: value, .. }: x25519::PrivateKey { .. }, Hmac { .. }, DecapsulationKey768 { .. }, rsa::PrivateKey { bits: 2048, .. }, Secret { len: 32, .. }.

Value traits of the library types used as Dict keys and Set elements, compared and ordered:

TypeTraits
PathClone, Eq, Hash, PartialOrd, Ord (by the text), Default (empty), Display (its text)
Ipv4Addr, Ipv6AddrClone, Copy, Eq, Hash, PartialOrd, Ord
IpAddrClone, Copy, PartialEq, Eq, Hash, Debug
Domain, SocketType, Protocol, AccessClone, Copy, PartialEq, Eq, Hash, Debug
SocketAddr, SocketAddrV4, SocketAddrV6Clone, Copy, PartialEq, Eq, Hash, Debug
OrderingClone, Copy, Hash, PartialOrd, Ord (Less < Equal < Greater)
IoErrorKindClone, Copy, Eq, Hash, Display (not found)
TlsErrorKind, CipherSuite, AeadAlgorithm, EcCurve, HashAlgorithmHash
Signal, FileType, JoinErrorKindClone, Copy, Eq, Hash
Component, RawStr, RawPathClone, Eq, Hash
SeekFrom, Shutdown, Alignment, Sign, LoadOrdering, StoreOrdering, UpdateOrderingClone, Copy, PartialEq, Eq, Hash, Debug
ThreadIdClone, Copy, Eq, Hash, Debug
Permissions, Layout, ExitStatusClone, Copy, PartialEq, Eq, Hash
CStr, CString, Certificate (by its DER bytes), FormatSpecClone, PartialEq, Eq, Hash
RangeDebug, Hash
RangeInclusive, RangeFrom, RangeTo, RangeToInclusive, RangeFullClone, PartialEq, Eq, Hash, Debug
Duration (ZERO), StrBuf (empty), FormatSpec (FormatSpec::new())Default

#[non_exhaustive] (refs-lang 6.2.10) marks every public enum that can gain variants and every options record that can gain fields. A match on one outside its package needs a _ arm, and a struct pattern needs ..:

  • enums: IoErrorKind, TlsErrorKind, CipherSuite, AeadAlgorithm, EcCurve, HashAlgorithm, DecodeErrorKind, base64::Alphabet, nabion::Value, Stdio, FileType, Signal, JoinErrorKind, fs::Access, net::Domain, net::SocketType;
  • records: tls::ClientConfig, tls::ServerConfig, FieldInfo, MethodInfo, fs::OpenFlags, fs::CreateFlags, os::unix::fs::OsOpenFlags / OsCreateFlags, os::windows::fs::OsOpenFlags / OsCreateFlags / CreateFileParams.

Closed enums stay exhaustive: Ordering, Poll, Result, SeekFrom, Shutdown, Alignment, Sign, IpAddr, SocketAddr, Component, LoadOrdering, StoreOrdering, UpdateOrdering, hex::Case, base64::Padding, VarError, FromBytesWithNulError, SendError, TrySendError. Together the two lists name every public enum.

#[sealed] (refs-lang 6.2) marks the traits whose implementations the library relies on for soundness or meaning; only the defining package implements them, and bounds and calls are unaffected: AtomicPrimitive (2.16), NicheInt, NicheFloat (2.2), Pinnable (1.6). Step (2.7) stays open for user range types. Pod (1.3) is not sealed (#[derive(Pod)] checks a user struct), and bool and char are not Pod.

#0.5.13. Async twins

x_async is the async twin of the blocking x: the same arguments and the same errors, awaited (try await s.x_async(..)) and suspending only the task. A twin takes ref self where x works on its receiver in place, and self where x consumes it (close_async).

Stream operations get their twins from traits, not from methods of each type: a type that implements Read, BufRead, Write, Seek or Close gets read_async, fill_buf_async, write_async, flush_async, seek_async or close_async by implementing AsyncRead, AsyncBufRead, AsyncWrite, AsyncSeek or AsyncClose (2.23.5). Only operations that are not stream I/O are inherent _async methods: opening and connecting (open_async, connect_async), accept_async, shutdown_async, datagram and fd passing calls, TLS control and process waits (wait_async, output_async, status_async).

TypeAsync traitsInherent twins
FileAsyncRead, AsyncWrite, AsyncSeek, AsyncCloseopen_async and the other open functions, sync_all_async, set_len_async, metadata_async, lock_exclusive_async, lock_shared_async (unlock never waits)
TcpStreamAsyncRead, AsyncWrite, AsyncCloseconnect_async, shutdown_async
TcpListenerAsyncCloseaccept_async
UdpSocketAsyncCloseconnect_async, send_async, recv_async, send_to_async, recv_from_async
SocketAsyncRead, AsyncWrite, AsyncCloseconnect_async, accept_async, shutdown_async, send_to_async, recv_from_async
UnixStreamAsyncRead, AsyncWrite, AsyncCloseconnect_async, shutdown_async, send_with_fd_async, recv_with_fd_async
UnixListenerAsyncCloseaccept_async
UnixDatagramAsyncCloseconnect_async, send_async, recv_async, send_with_fd_async, recv_with_fd_async
tls::Client, tls::ServerAsyncRead, AsyncWrite, AsyncClosenew_async, with_config_async, close_notify_async, key_update_async
net::ReadHalf<T>AsyncRead, AsyncCloseshutdown_async
net::WriteHalf<T>AsyncWrite, AsyncCloseshutdown_async, key_update_async (TLS)
ChildStdinAsyncWrite, AsyncClose
ChildStdout, ChildStderrAsyncRead, AsyncClose
Childwait_async
Commandoutput_async, status_async
BufReader, BufWriter, LineWriter, Cursoras their inner stream (2.23.6)
io::Owned<V, R>, io::Chain<A, B>AsyncClose as their readers (R; A and B) (2.23.5)

A Sender's close is not I/O and has no twin. A File or Dir twin takes ref self; only close_async consumes the handle.

#0.5.14. Docs and attributes

Every public item of the standard libraries has a /// doc comment: each function, type, trait, trait method and inherent method, const fn and async fn ones and those behind a cfg included. A doc says what the item does and what it returns; a function that throws, returns none or panics says when, and an unsafe function says what its caller must uphold. The library's own check (nabic check --check-library) denies missing_docs (refs-lang 6.2.9).

A function whose result has an order states it:

ItemOrder
Dict, Set and their iteratorsinsertion order
Set::union, Set::symmetric_differencethe members from self in its order, then those only in other in its order
Set::intersection, Set::differenceself's order
fs::walkdepth first in pre-order (a directory before its entries), each directory's entries sorted by name
net::resolvethe system resolver's order, its preferred address first
env::vars, env::vars_osthe order the OS keeps the environment in; not sorted
task::racethe first task to finish wins; tasks found finished at the same wake-up go to the lowest index; the others are cancelled
task::selectpolled in list order; a tie goes to the lower index

#[must_use] (refs-lang 6.2.11) marks these, each type with a message that says how to use the value:

KindItems
Iteratorsthose of str (Chars, CharIndices, Split, Lines, ...), List (ListIter), Slice (SliceIter, Windows, Chunks), SliceMut (SliceMutIter), Dict (Keys, Values, DictIter), Set (SetIter), Deque (DequeIter), BinaryHeap (HeapIter) and Receiver (ReceiverIter); the iter adapters and the TryIterator adapters (TryMap, MapErr, Results, ...); GenIter, TryGenIter; env::Vars, env::VarsOs; fs::ReadDir, fs::Walk; io::Lines, io::Split, io::Bytes; net::Incoming; path::Components, path::Ancestors
StreamsAsyncStream, TryAsyncStream; the stream adapters and combinators and the TryStream adapters; io::BytesStream, io::LinesStream; fs::ReadDirStream
Futuresthe value of an async fn call or async block; LockFuture, ReadFuture, WriteFuture, CondvarWait, RecvFuture, SendFuture, task::Join, SelectFuture
Builders and resultsprocess::Command, thread::Builder, thread::JoinHandle, Result
Functionsthe value methods of str and Path; every Duration function and the arithmetic of Instant, SystemTime and BootTime (duration_since, checked_add, checked_sub); the pure terminal methods of Iterator, DoubleEndedIterator and TryIterator (find, rfind, min, max, min_by_key, max_by_key, reduce, last, position); Set::union, intersection, difference, symmetric_difference; every method of the integer and float primitives, Int and Float

A Task, what spawn and spawn_blocking return, is not #[must_use]: the work runs whether or not it is awaited. A guard needs no mark: dropping a MutexGuard in a statement is an error. The #[discardable] functions are those refs-lang 6.2.11 lists.

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