local
import std::thread::local; · source
Types
ThreadLocal<T>
type struct ThreadLocal<T> {
key: u32;
init: () -> T;
static new(init: () -> T) -> ThreadLocal<T>;
get(&this) -> T*;
clear(&this) -> void;
}
Each thread that touches a ThreadLocal<T> lazily gets its own heap-allocated T on first access; later accesses from the same thread return the same slot. Threads never see each other's values.
import std::thread::local;
mut buf: ThreadLocal<String> = ThreadLocal::<String>::new(() -> String { return String::new(); });
mut mine: String* = buf.get();
It is built on pthread_key_create with pthread_setspecific and pthread_getspecific; the per-thread value is heap-boxed and the void* TLS slot holds the box pointer. First access on a thread allocates and runs the init function pointer.
Cleanup is manual in v1.
pthread_key_createaccepts a destructor that runs at thread exit, and v1 passes null for it — so per-thread allocations are not freed automatically. Callclear()from each thread before it terminates, or accept the leak. For daemon-style threads that live as long as the process this is immaterial; short-lived workers should clear at exit. Wiring a per-Tmonomorphized destructor through the generic pipeline is what removes the limitation.
Properties
| Property | Type |
|---|---|
key | u32 |
Constructors
static new(init: () -> T) -> ThreadLocal<T>;
static new(init: () -> T) -> ThreadLocal<T>;Construct a ThreadLocal<T> whose per-thread value is produced by calling init on first access from each thread. Panics on pthread key-creation failure (typically OOM in the kernel-side key table; rare).
Methods
init: () -> T;
get(&this) -> T*;
get(&this) -> T*;Get a raw pointer to the current thread's value, initializing it via init on first access. The pointer is stable for the lifetime of the thread (or until clear() is called by the same thread).
clear(&this) -> void;
clear(&this) -> void;Free the current thread's value if it has been initialized. Subsequent get() from this thread will re-run init. Other threads' values are untouched.
Trait implementations
implement<T> trait Drop for struct ThreadLocal<T>
Dropping the ThreadLocal deletes the key. It does not free other threads' slots — see the note above.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct ThreadLocal<T> {
key: u32;
}type parameter TThreadLocal.key: u32ThreadLocal::init: () -> Ttype parameter Tstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static ThreadLocal::new(init: () -> T) -> ThreadLocal<T>Construct a ThreadLocal<T> whose per-thread value is produced by calling init on first access from each thread. Panics on pthread key-creation failure (typically OOM in the kernel-side key table; rare).
(parameter) init: () -> Ttype struct ThreadLocal<T> {
key: u32;
}ThreadLocal::get(&this) -> T*Get a raw pointer to the current thread's value, initializing it via init on first access. The pointer is stable for the lifetime of the thread (or until clear() is called by the same thread).
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
ThreadLocal::clear(&this) -> voidFree the current thread's value if it has been initialized. Subsequent get() from this thread will re-run init. Other threads' values are untouched.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut buf: ThreadLocal<String>type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringstatic ThreadLocal::new(init: () -> T) -> ThreadLocal<T>Construct a ThreadLocal<T> whose per-thread value is produced by calling init on first access from each thread. Panics on pthread key-creation failure (typically OOM in the kernel-side key table; rare).
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
static String::new() -> StringEmpty string. No heap storage until the first push_* / reserve.
mut mine: String*mut buf: ThreadLocal<String>ThreadLocal::get(&this) -> T*Get a raw pointer to the current thread's value, initializing it via init on first access. The pointer is stable for the lifetime of the thread (or until clear() is called by the same thread).
ThreadLocal::init: () -> TThreadLocal::clear(&this) -> voidFree the current thread's value if it has been initialized. Subsequent get() from this thread will re-run init. Other threads' values are untouched.
ThreadLocal.key: u32implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference