#2.13. marker - Clone / Copy / Drop
On this page
- 2.13.1. Memory model - Clone/Drop in a language with a GC
- 2.13.2. Clone - deep copy
- Meaning of Clone
- No automatic calls - clone_trait lang slot
- Default bodies
- #[derive(...)]
- 2.13.3. Copy - marker
- impl policy
- 2.13.4. Drop - cleanup on release
- Ownership
- When called
- Relation to the GC
- Drop implementation example
- Restrictions on Drop
- derive policy
- 2.13.5. lang slot mapping
- 2.13.6. Trait implementation matrix
- 2.13.7. Comparison with other languages
- 2.13.8. NoneSentinel - the type of the none literal
- 2.13.9. Send / Sync - thread-boundary markers
- The hand-off machinery
- 2.13.10. Default - default values
- 2.13.11. Derivable traits
Duplicating values and cleaning up resources that are no longer needed are unavoidable concerns in any language. The core::marker module expresses these two concerns with three traits. Clone defines how to make an independent copy of a value; Copy is a marker stating that a plain bit copy suffices for a type; and Drop defines cleanup code that must run when a value leaves scope. The module also holds NoneSentinel, the library name of the none literal's type (2.13.8).
Nabi's use of a GC directly affects the meaning of these traits. The GC is responsible for reclaiming memory, so Drop is not tied to memory deallocation. Drop is limited to finalization work that cleans up resources living outside the language, such as file handles and sockets. This places Nabi's marker traits apart from both Rust and Java/Python.
#2.13.1. Memory model - Clone/Drop in a language with a GC
In Nabi's runtime, the Immix + Sticky Mark-Bit Generational GC manages the lifetime of heap objects automatically. The user does not decide when an object disappears from memory. Cleanup of external resources, however, must happen deterministically, so Nabi's Drop operates as a mechanism separate from the GC. Compared with Rust:
| Aspect | Rust (no GC) | Nabi (GC) |
|---|---|---|
| Memory reclamation | Drop is responsible | GC automatically; Drop does not |
Meaning of Drop::drop | release resource + free memory | external resource cleanup only (file handle, socket, ...) |
| When called | deterministic (scope exit) | deterministic: where the last owner leaves scope, or at the end of a using block (unrelated to GC collection) |
| Effect if omitted | resource leak + memory leak | resource leak only (the GC eventually reclaims memory) |
Note
A Drop value is affine: the compiler calls Drop::drop where its last owner leaves scope, or at the end of the using block that binds it (refs-lang 3.17 using); a type whose close can fail must be closed explicitly (2.13.4). The GC never calls drop.
Because memory reclamation and resource cleanup are separate, ordinary objects need not care about Drop (the GC reclaims them), and only types that handle external resources implement Drop to obtain a deterministic cleanup point.
#2.13.2. Clone - deep copy
Clone::clone is the method called when an independent copy of a value is needed. Its signature is simple:
#[lang("clone_trait")]
trait Clone {
fn clone(self) -> Self;
}
clone performs a deep copy. Even if the value holds references internally, the data they point to is copied too, so the two sides can be modified independently afterwards. Shared data that the GC guarantees to be immutable may still be shared as is.
#Meaning of Clone
The following example shows Clone in its most direct form.
let a: List<i32> = [1, 2, 3];
let b = a.clone(); // new List, with a new internal buffer
b.push(4); // a is unaffected
a; //→ [1, 2, 3]
b; //→ [1, 2, 3, 4]
"Deep" here means precisely this: Clone preserves logical equality while guaranteeing mutation independence between the values. Two properties follow. Immediately after let b = a.clone();, a == b holds if PartialEq is defined. After that, no change to a is reflected in b, and vice versa.
#No automatic calls - clone_trait lang slot
In some languages, writing let y = x; makes the compiler call clone automatically. Nabi chooses the explicit side here.
let xs: List<i32> = [1, 2, 3];
let ys = xs; // GC heap object → reference shared (no clone)
let y = x; shares the reference for a GC heap object and performs a bit copy for a Copy type. Clone::clone is therefore never dispatched automatically; it runs only when the user calls it explicitly. For this reason Clone is registered not as a method slot but as the trait slot clone_trait, and the compiler looks this slot up only in the #[derive(Clone)] expansion.
#Default bodies
The clone of a primitive type amounts to a bit copy, so it maps directly to an intrinsic; the clone of a collection allocates a new internal buffer and clones the elements recursively.
impl Clone for i32 {
#[intrinsic("nabi.clone.copy")] // bitwise copy
fn clone(self) -> i32;
}
impl<T: Clone> Clone for List<T> {
fn clone(self) -> List<T> {
let r: List<T> = [];
for x in self {
r.push(x.clone());
}
r
}
}
Cloning a GC object always allocates a new buffer and clones the elements recursively.
##[derive(...)]
For most user-defined structs whose fields are all Clone, there is no reason to write the body by hand. #[derive(Clone)] does it instead.
#[derive(Clone)]
struct Config {
name: str;
timeout: u32;
retries: u32;
}
let c1 = Config { name: "default", timeout: 30, retries: 3 };
let c2 = c1.clone(); // every field cloned recursively
The derived body calls Clone::clone on every field and returns the results in the same structure. Every field must therefore implement Clone, recursively.
#2.13.3. Copy - marker
trait Copy: Clone {
// marker: no methods
}
In Nabi every value is copied by value. For collections and GC objects the handle is copied and points to the same object; primitive values, str (an immutable (ptr, len)), structs, enums and tuples are copied themselves (refs-lang 2.3.8). Copy is therefore only a marker and does not change the meaning of assignment or argument passing. Copy is used only in generic bounds (T: Copy) to select types for which "a bit copy is an independent copy".
#impl policy
| Type | Clone | Copy | Notes |
|---|---|---|---|
i*, u*, f* | ✓ | ✓ | primitive numbers |
bool, char, c_char | ✓ | ✓ | |
str | ✓ | ✓ | immutable (ptr, len) value |
int, float | ✓ | ✓ | immutable value; a heap BigInt is only shared |
List<T>, Dict<K,V>, Set<T> | ✓ (T:Clone) | ✗ | copying the handle shares, not an independent copy |
| tuples | ✓ (all) | ✓ (if all Copy) | per component |
Range<T> | ✓ (T:Clone) | ✓ (T:Copy) | when both start/end are Copy |
| user struct / enum | derive | derive | when every field implements the same trait |
A type that implements Drop cannot be Copy.
#[derive(Clone, Copy)]
struct Vec3 { x: f64; y: f64; z: f64; }
Important
Deriving Copy is allowed only when Clone is implemented as well, and only when every field is Copy.
#2.13.4. Drop - cleanup on release
A file that was opened must be closed; a mutex that was locked must be unlocked. Such external resource cleanup cannot be left to the GC. Holding a resource until an unpredictable sweep could exhaust file descriptors or leave other code stalled on a lock that was never released. The Drop trait and the using expression solve this.
trait Drop {
#[lang("drop_method")]
fn drop(ref self);
}
#Ownership
A value whose type implements Drop is affine: it has one owner, and it is released once. A struct, tuple, enum or sum that holds one is affine too.
let b = amoves the value:bowns it, and usingaafterwards is a compile error (E0133). Storing it into a struct, a tuple, an enum or a container, capturing it in a closure or anasyncblock, and returning it move it the same way. A value of a variant that holds no such value (Stdio::Piped) holds nothing to move.- A call takes the value only when the callee keeps it: stores it, returns it, closes it, or passes it to a call that keeps it. Any other call borrows it, and the caller still owns it. An
async fncall that is awaited at once is such a call too: the argument comes back to the caller when theawaitends, unless the function's body keeps it. What comes back is the caller's own value, not the callee's copy: a stream the callee walked is where it was before the call (return it from the callee to go on from where it stopped). A call through a function value or a closure has no body to look into, so it takes the value. - An element seen while iterating a container (
for x in list) or read from one stays the container's. clone()(try_clone()for aFile) makes an independent value with its own owner.- A stream adapter (
r.lines(),r.take(n),l.incoming()) is a scoped view of the stream, not a second owner: it stays in the owner's scope (E0139, 2.23.1); theinto_forms (r.into_lines()) take the stream instead. - A part moves out of a struct only through
marker::replace(ref s.f, v)ormarker::take(ref s.f), which leave a new value in its place. unsafe { marker::share(x) }makes a second handle the compiler does not follow: it neither moves nor releasesx. Using it after the owner released the value is the program's to rule out.
// core::marker
pub unsafe fn share<T>(x: T) -> T;
#[discardable]
pub fn replace<T>(ref dest: T, value: T) -> T; // puts `value` in `dest`, returns the old value
#[discardable]
pub fn take<T: Default>(ref dest: T) -> T; // replace(ref dest, T::default())
let (tx, rx) = channel::<i32>();
let other = tx; // moves: `tx` is gone
try tx.send(1); // error: `tx` was moved at line 2, and is used here
let copy = other.clone(); // a second sender with its own owner
#When called
The compiler releases a value where its owner leaves scope, on every exit (normal completion, return, break, error propagation), and when a task finishes or is cancelled, for what its async block captured. using releases its value at the end of its block (refs-lang 3.17 using). A value that moved away is released by its new owner, not here. A let that redeclares the owner's name (refs-lang 5.1.4) neither moves nor releases the old value: it is released where the old binding leaves scope, and one that must be closed is closed or moved before the redeclaration.
A type whose close can fail implements io::Close (fn close(self) throws (IoError)): File, a BufWriter or LineWriter over one, and any struct holding one. The compiler does not close such a value for you, since the close's error would be lost: close it with try x.close() or bind it with using. Leaving its scope open on some path is a compile error, and so is a temporary made only for a call that borrows it ((try File::open(p)).read_to_str(), io::BufReader::new(try File::open(p)).lines()): nothing owns it after the call, so nothing closes it (E0134); bind it with using (using f = try File::open(p) { try f.read_to_str() }). A path that leaves with an error releases it (the close's own error is dropped there). try? drops the error it catches, so try? on a call whose error can hold such a value (SendError<File> hands back the unsent value) is a compile error: catch the error with try .. catch, take the value out and close it. A failed try! panics, so the error it holds needs no close. A sum or enum holding such a value is closed per member: a match arm that sees it closes it, and an arm that sees only members the compiler releases (a Sender) owes no close. The member a pattern narrows a sum to (if let x: Pair = p, a match arm, let .. else) is followed part by part, as a struct is: closing x.a leaves x.b open, and closing x.a again, or using p whole afterwards, is an error.
- It is called on normal completion,
return,breakand error propagation alike. - Multiple bindings are dropped in reverse declaration order (LIFO).
- A panic ends the process, so
dropis not called. - Drop glue: after the value's own
drop, fields that haveDropare cleaned up recursively in reverse declaration order. The same applies to tuple elements and to the payload currently held by an enum / sum type.
fn process(path: str) throws (IoError) {
using lock = MUTEX.lock(), file = try File::open(path) {
do_work(file);
} // ← on normal exit or error: file.drop() then lock.drop()
}
{
let f = try! File::open("data.txt");
} // ← error: `f` holds a `File` that is never closed
Binding a type that does not implement Drop with using is a compile error stating that Drop is required.
#Relation to the GC
The GC never calls drop, and there are no finalizers: the release points are the ones above, fixed at compile time.
#Drop implementation example
The most typical implementation is a wrapper around an OS resource.
extern "C" { fn close(fd: i32) -> i32; }
struct Fd {
handle: i32;
}
impl Drop for Fd {
fn drop(ref self) {
unsafe { close(self.handle); } // OS file descriptor close; its result is ignored
}
}
Fd's close result is ignored, so the compiler releases it implicitly. A type whose close can fail (File) also implements io::Close and is closed explicitly, as above.
#Restrictions on Drop
Drop comes with several restrictions. First, a type that implements Drop cannot be Copy: multiplying copies freely by bit copy while guaranteeing deterministic cleanup is a contradiction.
#[derive(Copy)] // ERROR - conflicts with Drop
struct File { handle: i32; }
impl Drop for File { ... }
Second, calling drop directly is a compile error. Correctness is guaranteed only if it is called exactly once, at release.
let f = File::open(path);
f.drop(); // ERROR: calling Drop::drop directly is forbidden
To close a resource early, expose a separate method such as File::close().
The only exception is unsafe code inside bare. For example, unsafe fn drop_in_place (1.3 mem) is a function for manual memory management. It runs the drop glue: the type's own drop, then its fields' in reverse declaration order, as above.
Also, drop has no throws signature, so its body cannot throw an error. A panic inside drop ends the process like any other panic.
impl Drop for X {
fn drop(ref self) {
if some_cond { throw "oops" } // ERROR: drop is not throws
}
}
#derive policy
Unlike Clone, Drop cannot be derived. After a value's own drop finishes, at whichever release point, the compiler calls drop on that struct's Drop fields in reverse declaration order. A user-written Drop body therefore handles only "what needs cleaning up beyond the fields".
struct Container {
a: File;
b: Socket;
}
// when a Container is released, release does b.drop() → a.drop()
// (because the fields themselves implement Drop)
impl Drop for Container {
fn drop(ref self) {
log("Container released");
// field drops are called automatically by the compiler _after this method returns_
}
}
#2.13.5. lang slot mapping
This module connects to the compiler through four lang slots. The automatic call points follow the policies described above.
| slot | Attached to | Automatic call point |
|---|---|---|
clone_trait | Clone trait | none; used in the #[derive(Clone)] expansion |
drop_method | Drop::drop method | every release: the last owner leaving scope, task end, end of a using block (and drop glue) |
send_trait | Send trait (2.13.9) | implemented automatically, thread-boundary checks |
sync_trait | Sync trait (2.13.9) | implemented automatically, thread-boundary checks |
For details see sections 0.4.2 / 0.4.3 / 0.4.5 of the 0.4 lang slot table.
#2.13.6. Trait implementation matrix
How the main standard library types relate to the three traits:
| Type | Clone | Copy | Drop | Behavior |
|---|---|---|---|---|
i*, u*, f* | ✓ | ✓ | ✗ | nabi.clone.copy (bitwise) |
bool, char, c_char | ✓ | ✓ | ✗ | bitwise |
str | ✓ | ✓ | ✗ | copies the immutable (ptr, len) value |
List<T>, Dict<K,V>, Set<T> | ✓ (T:Clone) | ✗ | ✗ | recursive clone |
| tuples | ✓ (all) | ✓ (all Copy) | ✗ | per component |
Range<T> | ✓ (T:Clone) | ✓ (T:Copy) | ✗ | per field (start, end) |
File | ✗ | ✗ | ✓ (plus io::Close) | closes the OS handle; closed explicitly (2.13.4) |
MutexGuard<T> | ✗ | ✗ | ✓ (on release) | unlocks the mutex |
channel::Sender<T> | ✓ | ✗ | ✓ | calls close(); released implicitly |
The principle behind the table is simple. Every primitive type and the collections above implement Clone, and only those for which a bit copy is semantically correct are Copy. The collections are Clone but not Copy, because a bit copy of a handle does not preserve the meaning of a deep copy. str is Copy: it is immutable, so copying its (ptr, len) is an independent copy. Drop is implemented only by wrappers around external resources such as File, Socket and MutexGuard. These types usually do not implement Clone, to prevent duplicate ownership of the resource.
#2.13.7. Comparison with other languages
Comparing Nabi's model with existing languages makes the design intent clearer.
| Aspect | Rust | Java/Python (GC) | Nabi (GC + Drop) |
|---|---|---|---|
| Memory reclamation | Drop is responsible | GC | GC |
| Deterministic finalize | Drop (compiler-guaranteed) | none (finalizers nondeterministic) | Drop (scope-bound) |
| Meaning of Copy | bitwise + !Drop enforced | none (every object is a ref) | bitwise + !Drop enforced |
| Meaning of Clone | deep copy | clone() (Object.clone) | deep copy |
| derive | #[derive(Clone, Copy)] | Cloneable interface | #[derive(Clone, Copy)] |
What sets Nabi apart is the coexistence of a GC with a deterministic Drop. Because the GC handles memory reclamation, Drop does not mean memory deallocation, so ordinary objects never need to implement Drop. Only types that need external resource cleanup implement Drop; the compiler releases them where their last owner leaves scope, and users close the ones whose close can fail with close() or using.
This model avoids both the nondeterminism of Java/Python finalizers and the costs Rust incurs from having no GC.
#2.13.8. NoneSentinel - the type of the none literal
The none literal of refs-lang 1.9.2 has the compiler's built-in none type, a zero-sized type with a single value. core::marker gives that type its library name NoneSentinel with a type alias; no lang slot is involved.
pub type NoneSentinel = none;
Users rarely write this name directly. In sum type notation such as T | none (refs-lang 2.6), the none position is this type; the none literal, the none type notation and NoneSentinel all name the same built-in type.
#2.13.9. Send / Sync - thread-boundary markers
#[lang("send_trait")]
trait Send { } // safe to move to another thread
#[lang("sync_trait")]
trait Sync { } // safe to read from multiple threads through shared references
Both traits are automatic marker traits with no methods; the compiler implements them structurally.
Primitive numbers,
bool,char,c_char,strandnoneareSend + Sync.A
struct,enum,union, tuple, array or sum type isSendif all its fields (components) areSend, andSyncif all areSync.Sendfor a generic container followsTby the same rule.Collections (
List,Dict,Set,StrBuf,Deque,BinaryHeap,Slice,SliceMut) areSendand!Sync: they share a mutable handle. Across threads they are shared read-only by freezing them (FrozenList,FrozenDict,FrozenSet,Send + Syncwhen their elements are, 2.9.7b), written in disjoint parts throughchunks_mutviews given topar(2.19.1), or shared throughMutex/RwLock/ channels.Handing a value over (
Mutex::new(xs),RwLock::new(xs),tx.send(xs)/try_send/send_async,task::spawn_blocking_with_arg(xs, f), a closure's[move xs]capture,xs.freeze()) leaves the sender's handle unusable: the new owner gets a copy (see the run-time copy below), and the sender may not use what it handed over. A handle taken out of a guard (get), given to it (set) or reached byupdate's closure still points at the guarded object after the guard'susingblock ends. Using such a value afterwards, on any path, is a compile error (E0105for a handed-over value,E0106for one taken out of a guard; refs-lang 9.9.4). The value is followed through:- copies (
let ys = xs, a struct field or tuple element, a field assignment); - the fields it is passed by (
Mutex::new(s.items)); - the objects that hold it (
all.push(xs), a closure that captures it), both ways: handing overxsleavesallunusable too; - the elements of a collection. What is stored into a collection and what is read out of it (
xs[i],get, iteration, a slice of it) are one value, so handing over either leaves the other unusable (let x = xs[0]; tx.send(xs); x.len(),tx.send(xs[0]); xs[1].len()). Onlylen,is_empty,capacity,clear,truncate,reserve,shrink_to_fit,pushandinsert(andDeque'spush_back/push_front) stay allowed on a collection whose elements were handed over;clearempties it, so it is fully usable again. A collection copied from another (extend,+=,collect,par::filter) shares the source's elements. An element taken out withpoporremove(List,Deque,Dict,BinaryHeap) has left the collection: handing it over leaves the collection usable, unless the collection may hold that object twice: one value stored twice, two stored values that share an object, or elements of one collection stored into another in a loop (elements are not told apart by position); - the arguments of a direct call that hands something over: passing one object through two arguments, when the callee hands it over through one and uses it through the other (
f(tx, a, a),h.go(tx, h.items)wheregousesself.items, a list it pops from and sends plus one of its elements), is a compile error. Two elements of one collection are taken to be distinct (f(tx, rows[0], rows[1])is accepted); - a
refparameter. When the callee hands a part of it over and then gives that part a new value on every path that hands it over (self.buf = []aftertx.send(self.buf)), the caller's variable gets the new value back and stays usable; other handles to the old value do not. A by-value parameter is a copy: giving its field a new value does not reach the caller, which still holds what was handed over; - calls. A function that hands a parameter over, returns it or a part of it, stores it into another parameter's object or a
refparameter, captures it into the closure it returns, or returns what a guard parameter holds, does so for its caller too, also across a prebuilt library (refs-lang 9.4.7).
A call through a function value is taken to reach every function used as a value with its parameter types. Conditions are followed where they test a value the compiler knows on each path: a
boolor a sum or enum variant set by a constant or copied (let sent = false; if c { tx.send(xs); sent = true; } if !sent { xs.len() }), and how a call ended. A function that hands a parameter over only when it returns without an error, or a value other thannone/false, does not hand it over on its caller'scatch,try?,??,elseorifpath for the other outcome; the same holds for thecatchoftry awaiton anasync fn. Other conditions are not followed: a value handed over on one path is gone where that path joins others.To keep using a value, hand over a
clone()(an independent value), or give the variable or field a new value first (for a struct copy, only that field); asetreleases what an earliergetreturned. Uses ofSyncvalues, reading a value's tag (x is none,match x), and uses insideunsafe(refs-lang 4.15.2) are exempt. Plain captures of a spawned closure need no check: the closure shares them with the code that made it, so they must beSend + Sync. A[move x]capture handsxto the closure (refs-lang 4.6.11) and needs onlySend. Passing a closure that owns one that is notSync(or afn(..) + Sendvalue not markedSync, which may own one) to a parameter typedfn(..) + SendwithoutSynchands it over, since the callee may run it on another thread (thread::spawn,task::spawn_blocking,par::join,par::Scope::spawn,par::TryScope::spawn, ...); using it afterwards, however it travelled (twice in one call, through a function that returns its argument, out of a list or a tuple), is a compile error (E0133). Anasync [move ..]closure and the futures its calls make are one owner: handing one over takes the others along (refs-lang 4.6.11).task::spawnhands its future over: the task's frame gets copies of what the future captured, and the future and what it captured are gone for the spawner, as after a send (2.18.1).Also compile errors: handing over a value read out of a
Syncobject that is not itselfSync(a field of a user'sunsafe impl Synctype), which other code reaches without the hand-off (hand over aclone()instead); athread::local's value, lent to awithclosure (3.4.4); values a channel'srecv, a task's result or a lock's guard give out are the receiver's and are not covered by this rule; handing over what a guard'sgetreturned while the guard still holds it (untilsetgives the lock a new value), or passing it to a callee that keeps it (get_clone()is the copy to keep); returning or throwing it from inside theusingblock, yielding it there (yield g.get()in a generator), making it the block's own value (let x = using g = m.lock() { g.get() },f(using g = m.lock() { g.get() })), whatever form carries it out (a loop'sbreakvalue, a labelled block, anif/matcharm, acatch, a closure's result), or giving it to a coroutine made inside the block and run after it (anasync fn's future, a generator:fut = grow(g.get()[0])): the block's end releases the lock before the value is used. A guard is good only inside itsusingblock: a copy of it (pushed into a collection, captured by a closure, stored by a callee, bound by a secondusing, held by a coroutine: anasync fn's or generator's argument, anasyncblock's capture) used after that block ends is a compile error; guards are!Sync(2.16.3).par::for_each,par::mapandpar::filtergive each item to a worker, so two items must not reach one collection that is notSync. Each call checks this at run time (2.19.1): if two items reach one collection, the items run one at a time on the calling thread, with a warning once per call site. Slices only read, so items may share a list through slices of it, as long as no item holds the list itself; writable views (SliceMut) may share it too when no other item's view overlaps a part one of them writes.What remains outside these rules: code inside
unsafe; a call through a function value or a closure, which skips the argument check above; and a generator that yields one object more than once, collected into a collection whose elements are then handed out one by one. Such a collection is not seen as holding the object twice.At run time every hand-off also copies, so on the routes above leave out (and in code inside
unsafe) no collection the handed value reaches by value ends up shared between the sender and the new owner (the exceptions are listed below).Mutex::new,RwLock::new, a guard'ssetandupdate,send/try_send/send_async,task::spawn_blocking_with_arg's value, a[move x]capture (when the closure is made), and the result of a task, a thread or aparscope job (and atry_scopeerror) give the new owner a copy of every collection the value reaches: through the parts of structs, tuples, arrays, enums and sums and through the elements of collections, to any depth, with a work list rather than stack per level. A collection reached more than once is copied once, so the copy keeps the value's sharing and its cycles. Copied areList,Dict,Deque,StrBuf,Sliceand the collections built on them, andGcPtr(1.6.3). A coroutine future (AsyncFuture,TryAsyncFuture) gets a frame of its own, holding copies of what it captured made in the same copy; one that has started or finished, or whose frame (or a captured future's) has a[ref x]capture, cannot be handed over, and the hand-off panics (2.18.1). ATaskis not copied, but once it is handed over eachawaitof it gets its own copy of the result (2.18.2). A list and every view of its buffer keep sharing one copied buffer; a view of astr's bytes copies only those bytes (they cannot change).updatekeeps what the guard already holds: only what the closure brings in is copied. The sender's handles are left as they were: one the rules above missed names an object no other thread uses. A send that throws carries back an equal value (the caller's or its copy). The copy takes time and memory in proportion to what it copies; a run of plain values (no GC reference) is one memory copy. The toolchain-internalcore::marker::hand_over(x)returns the same copy.xs.freeze()does not copy: the frozen collection takes the storage over and leavesxsempty, and a list that had views gives a copy (2.9.7b). Left shared:Syncparts (frozen collections among them), values of types with anunsafe impl Send(they manage their own sharing: tasks, files, sockets), and generators and streams (notSend: they are never handed over).Mutex::new_unshared(2.16.3) does not copy. A handle taken out of a guard (get,update's closure argument) is not copied at run time: the rules above follow it, and the guard itself, out of theusingblock through every copy, areturn, ayield, the block's value and the frames of coroutines made inside it.- copies (
The following are excluded:
Cell<T>, raw pointers (Ptr<T>,ConstPtr<T>,CPtr<T>), bareanyandany Trwhose bound lacksSend/Sync(anany TrisSendonly when its bound has bothSendandSync: copies of ananyshare its box, as a closure's plain captures are shared, so no collection may sit inside one that crosses threads), function types without a bound, and closures with a[ref]capture (refs-lang 4.6.9). Other exceptions are specified in each type's chapter.A function type has these traits only when the bound is written, as in
fn(..) -> R + Send(refs-lang 4.4).+ Sendalone does not make itSync: the closure behind it may own a[move x]capture that is notSync. A function value several threads call (astatic, apar::*closure) is+ Send + Sync.
Types for which the automatic implementation is wrong declare it directly. A type that holds a raw pointer internally but wraps it thread-safely opts in with unsafe impl (safety is the author's responsibility); a type that is structurally auto-implemented but not thread-safe opts out with impl !Send. The exclusion of Cell<T> is declared this way.
struct Handle { raw: Ptr<u8>; }
unsafe impl Send for Handle { }
unsafe impl Sync for Handle { }
impl<T> !Send for Cell<T> { }
impl<T> !Sync for Cell<T> { }
impl<T> !Send for Ptr<T> { }
impl<T> !Sync for Ptr<T> { }
impl<T> !Send for CPtr<T> { }
impl<T> !Sync for CPtr<T> { }
APIs that pass values to threads or tasks (thread::spawn, task::spawn, par::*, channel) constrain their arguments with these two traits. The policy for synchronization primitives follows 2.16 sync.
#The hand-off machinery
#[internal]
pub fn hand_over<T>(x: T) -> T;
hand_over(x) returns what a hand-off gives the new owner: x with a copy of every collection it reaches, or x itself when it reaches none. It is #[internal]: core and std call it, and a use from any other package is an error (0.4.4).
pub struct Handoff<T> { /* private fields */ }
impl<T> Handoff<T> {
#[internal]
pub unsafe fn new(value: T) -> Handoff<T>;
pub fn get(self) -> T;
}
Handoff<T> carries a value between threads under a protocol of the caller's (a join, a lock, a channel hand-off) that keeps two threads from using it at once: it is Send + Sync whenever T is Send, even when T is not Sync. new is unsafe and #[internal]; get returns the value. Other code moves the value into a closure ([move x]) or passes it to task::spawn_blocking_with_arg.
Transfer and Memo are pub(pkg): the hand-off copy reaches the standard collections in other modules of core through them, and no other package sees them.
pub(pkg) trait Transfer {
fn transfer_copy(self, m: Memo) -> Self;
fn transfer_keep(self, m: Memo) -> Self;
fn transfer_visit(self, m: Memo) -> Self;
}
pub(pkg) struct Memo { /* private fields */ }
The compiler calls Transfer only for its fixed list of standard handle types; an impl on any other type is ignored. Besides the collections and futures, the list holds crypto::Secret (2.21) and the secret words the crypto algorithms keep: a hand-off gives the new owner its own copy in secret storage, and par runs items that reach one Secret one at a time, as for a collection. Memo records the copies one hand-off has made, by original object, and the copied buffers still to fix. Its methods and the transfer* intrinsics beside it are pub(pkg) as well.
#2.13.10. Default - default values
trait Default {
fn default() -> Self;
}
| Type | default() |
|---|---|
integers, int | 0 |
floating-point, float | 0.0 |
bool | false |
char | '\0' |
str | "" |
collections such as List, Dict, Set | empty collection |
T | none | none |
Default can be derived; it builds the value from each field's declared default (refs-lang 2.3.3), and from the field type's default() for a field without one: #[derive(Default)] struct Cfg { host: str = "localhost"; port: i32; } gives Cfg { host: "localhost", port: 0 }.
#2.13.11. Derivable traits
The built-in traits that #[derive(...)] (refs-lang 6.2.1) can produce are only these:
| Trait | Generated implementation | Requirement |
|---|---|---|
Clone | per-field clone | every field Clone |
Copy | marker | Clone also derived/implemented, not Drop |
Debug | field names and values in f"{x:?}" form | every field Debug |
Default | per field: its declared default, else default() | every field without a default Default |
PartialEq | AND of per-field == | every field PartialEq |
Eq | marker | PartialEq also derived/implemented |
PartialOrd | lexicographic comparison in field declaration order | every field PartialOrd, PartialEq |
Ord | lexicographic comparison in field declaration order | Eq, PartialOrd also derived/implemented |
Hash | each field's hash in declaration order; an enum writes its variant index first (writes unspecified) | every field Hash |
Error | Error with its default source() (2.14) | Display and Debug implemented or derived |
Displaycannot be derived;#[display("..")]writes it from an f-string (2.10.14).- Deriving
Eq/Ordwithout its supertrait is a compile error. - Applying
#[derive]to aunionis a compile error.
Found a mistake? Edit this page on GitHub. Last updated