Box<T> structv0.1.0
pub struct Box<T> {
// private fields
}
Move-only: its copy operation is prohibited and ~Box releases its allocation. Transfer one with move; the
source is then moved-out and reading it is rejected while compiling rather than at run time.
The storage is released when the box is destroyed, at whichever scope exit, return, break, continue or
overwriting assignment the value's life ends at.
Type parameters
| Name | Description |
|---|---|
T | the type the box owns one of |
Constructors
Box
pub func Box(allocator: Allocator, value: T) -> Box<T>
Puts value on the heap and hands back the box that owns it.
allocator is borrowed for the call and kept in the box. value is consumed and the box owns it.
Terminates the program when the allocator refuses; TryCreate reports the refusal instead.
Runs in whatever time the allocator takes, and allocates exactly one block.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | T | the value to move into it |
Returns: the box
Associated functions
TryCreate
pub func TryCreate(
allocator: Allocator,
value: T
) -> Box<T> throws AllocError
Puts value on the heap and hands back the box that owns it, or throws why the allocator refused.
allocator is borrowed for the call and kept in the box. value is consumed: on success the box owns it,
and on failure it is destroyed rather than leaked.
Runs in whatever time the allocator takes, and allocates exactly one block.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | T | the value to move into it |
Returns: the box, or throws why the allocator refused
Methods
Get
pub func Get(&self) -> &T?
The value this box holds, borrowed where it sits, or none for a box whose value has been taken out.
self stays borrowed while the reference is in use, so the box cannot be emptied, moved or destroyed under it.
Cannot fail. Runs in constant time and allocates nothing.
Returns: a reference to the held value, or none when the box is empty
GetMutable
pub func GetMutable(&var self) -> &var T?
The value this box holds, borrowed to be written where it sits, or none for a box whose value has been
taken out.
self stays borrowed exclusively while the reference is in use.
Cannot fail. Runs in constant time and allocates nothing.
Returns: a writable reference to the held value, or none when the box is empty
IsEmpty
pub func IsEmpty(&self) -> bool
Whether this box still holds a value.
self is borrowed. False only for a box whose value Take has moved out, which is a box that owns
nothing and releases nothing.
Cannot fail. Runs in constant time and allocates nothing.
Returns: whether the box holds nothing
Take
pub func Take(&var self) -> T?
Moves the value out of the box, leaving the box empty, and releases the storage.
self is borrowed mutably and is left empty. Reports none only for a box that is already empty.
The storage goes back to the allocator here rather than at the box's destruction, so a caller taking the
value out does not have to keep the box alive afterwards.
Runs in whatever time the allocator takes.
Returns: the held value, or none when the box is empty
Destructor
func ~Box(&var self)
Destroys the value and releases its storage, if the box still holds any. Runs when the box's life ends, and does nothing for a box whose value was taken out. A box that was moved away is not destroyed where it used to live, so the storage is released exactly once.