rwlock
import std::sync::rwlock; · source
Types
RwLock<T, A>
type struct RwLock<T, A = GlobalAlloc> where A: Allocator {
inner: RwLockInner<T, A>*;
static new(value: T) -> RwLock<T, GlobalAlloc>;
static new_in(value: T, alloc: A) -> RwLock<T, A>;
static try_new(value: T) -> Result<RwLock<T, GlobalAlloc>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<RwLock<T, A>, AllocError>;
read(&this) -> RwLockReadGuard<T, A>;
try_read(&this) -> Option<RwLockReadGuard<T, A>>;
write(&this) -> RwLockWriteGuard<T, A>;
try_write(&this) -> Option<RwLockWriteGuard<T, A>>;
}
Many concurrent readers or one exclusive writer. Reach for it over a Mutex when the value is read often and written rarely, since the read path does not serialize. Multiple read guards may coexist; a write guard excludes everything else.
Properties
| Property | Type |
|---|---|
inner | RwLockInner<T, A>* |
Constructors
static new(value: T) -> RwLock<T, GlobalAlloc>;
static new_in(value: T, alloc: A) -> RwLock<T, A>;
static try_new(value: T) -> Result<RwLock<T, GlobalAlloc>, AllocError>;
static try_new_in(value: T, alloc: A) -> Result<RwLock<T, A>, AllocError>;
Methods
read(&this) -> RwLockReadGuard<T, A>;
read(&this) -> RwLockReadGuard<T, A>;Acquire a shared read lock, blocking until available.
try_read(&this) -> Option<RwLockReadGuard<T, A>>;
write(&this) -> RwLockWriteGuard<T, A>;
write(&this) -> RwLockWriteGuard<T, A>;Acquire the exclusive write lock, blocking until no other readers or writers hold it.
try_write(&this) -> Option<RwLockWriteGuard<T, A>>;
The guards
type struct RwLockReadGuard<T, A> {
inner: RwLockInner<T, A>*;
as_ptr(&this) -> T*;
}
type struct RwLockWriteGuard<T, A> {
inner: RwLockInner<T, A>*;
as_ptr(&this) -> T*;
}
Both dereference through as_ptr() and release the lock when dropped. Multiple read guards may coexist; a write guard excludes everything else.
RwLockReadGuard
Properties
| Property | Type |
|---|---|
inner | RwLockInner<T, A>* |
Methods
####### as_ptr(&this) -> T*;
RwLockWriteGuard
Properties
| Property | Type |
|---|---|
inner | RwLockInner<T, A>* |
Methods
####### as_ptr(&this) -> T*;
Trait implementations
RwLock
implement<T, A> trait Drop for struct RwLock<T, A> where A: Allocator
RwLockReadGuard
implement<T, A> trait Drop for struct RwLockReadGuard<T, A>
RwLockWriteGuard
implement<T, A> trait Drop for struct RwLockWriteGuard<T, A>
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 RwLock<T, A = GlobalAlloc> where A: Allocator {
inner: RwLockInner<T, A>*;
}RwLock<T>: read/write reader-writer lock around a value.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorRwLock.inner: RwLockInner<T, A>*type struct RwLockInner<T, A> {
pthread_buf: [u8;
56];
value: T;
alloc: A;
}type 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 RwLock::new(value: T) -> RwLock<T, GlobalAlloc>(parameter) value: Ttype struct RwLock<T, A = GlobalAlloc> where A: Allocator {
inner: RwLockInner<T, A>*;
}RwLock<T>: read/write reader-writer lock around a value.
std::sync::rwlockstatic RwLock::new_in(value: T, alloc: A) -> RwLock<T, A>(parameter) alloc: Astatic RwLock::try_new(value: T) -> Result<RwLock<T, GlobalAlloc>, AllocError>type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}static RwLock::try_new_in(value: T, alloc: A) -> Result<RwLock<T, A>, AllocError>RwLock::read(&this) -> RwLockReadGuard<T, A>Acquire a shared read lock, blocking until available.
&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.
type struct RwLockReadGuard<T, A> {
inner: RwLockInner<T, A>*;
}Shared-read guard. Many of these may exist concurrently on the same RwLock. Calling .as_ptr() yields a pointer that should be treated as &T (read-only); writing through it is a logic error the type system doesn't catch (Cryo has no borrow checker).
RwLock::try_read(&this) -> Option<RwLockReadGuard<T, A>>RwLock::write(&this) -> RwLockWriteGuard<T, A>Acquire the exclusive write lock, blocking until no other readers or writers hold it.
type struct RwLockWriteGuard<T, A> {
inner: RwLockInner<T, A>*;
}Exclusive write guard. At most one of these exists on a given RwLock, and no RwLockReadGuard can co-exist with it.
RwLock::try_write(&this) -> Option<RwLockWriteGuard<T, A>>type struct RwLockReadGuard<T, A> {
inner: RwLockInner<T, A>*;
}Shared-read guard. Many of these may exist concurrently on the same RwLock. Calling .as_ptr() yields a pointer that should be treated as &T (read-only); writing through it is a logic error the type system doesn't catch (Cryo has no borrow checker).
type parameter ARwLockReadGuard.inner: RwLockInner<T, A>*type parameter Ttype parameter ARwLockReadGuard::as_ptr(&this) -> T*type struct RwLockWriteGuard<T, A> {
inner: RwLockInner<T, A>*;
}Exclusive write guard. At most one of these exists on a given RwLock, and no RwLockReadGuard can co-exist with it.
RwLockWriteGuard.inner: RwLockInner<T, A>*type parameter Ttype parameter ARwLockWriteGuard::as_ptr(&this) -> T*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).
type struct Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
}Mutex<T>: mutual-exclusion lock around a value.
std::sync::mutexRwLockReadGuard.inner: RwLockInner<T, A>*static RwLock::new(value: T) -> RwLock<T, GlobalAlloc>static RwLock::new_in(value: T, alloc: A) -> RwLock<T, A>static RwLock::try_new(value: T) -> Result<RwLock<T, GlobalAlloc>, AllocError>static RwLock::try_new_in(value: T, alloc: A) -> Result<RwLock<T, A>, AllocError>RwLock::read(&this) -> RwLockReadGuard<T, A>Acquire a shared read lock, blocking until available.
std::sync::rwlockRwLock::try_read(&this) -> Option<RwLockReadGuard<T, A>>RwLock::write(&this) -> RwLockWriteGuard<T, A>Acquire the exclusive write lock, blocking until no other readers or writers hold it.
std::sync::rwlockRwLock::try_write(&this) -> Option<RwLockWriteGuard<T, A>>RwLockReadGuard::as_ptr(&this) -> T*implement 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